CyberKeeda In Social Media
Showing posts with label Terraform. Show all posts
Showing posts with label Terraform. Show all posts

What are terraform providers and how to use it.


Within this post, we will cover 

  • What are terraform providers
  • Documentation link for providers.
  • How to choose providers
  • How to define providers within your terraform file.
  • Providers Versions.
    • How to find terraform provider versions.
    • How to explicitly mention provider version in terraform file. 

What are terraform providers ?

Terraform support N number of providers.
When we say providers it's basically terraform supported binaries and plugins for individual provider's subcategories like for example ( aws, azure, gcp etc ).
To be specific, terraform documentation categorized providers into multiple parts, which are mainly.
  • Major Clouds
    • AWS
    • GCP
    • Azure
    • OCI
    • Digital Ocean
    • VMware
  • Clouds
    • Other Cloud providers.
  • Infrastructure Software.
  • Network
  • VCS
  • Monitor and System Management
  • Database
  • Community.
Documentation link for providers.

How to choose providers ?
Before, you start writing your first terraform file, you must choose appropriate provider to provision
desired infrastructure.
For example, incase if you want to create a VPC subnet in AWS, you must choose AWS provider and define the same within your terraform script.

Navigate to official link to know more about supported provider. : Link
Please note, there are labels which also differentiate providers authors and owners.
  • Official
    • Officially maintained and supported and tested by Hashicorp
    • Note : They can be installed directly by executing terraform init command.
  • Verified.
    • Verified modules are reviewed by Hashicorp and are actively maintained by contributors, these badges appear next after the verification by Hashicorp.
    • Note : They can't be installed directly by executing terraform init command.
  • Community
    • 3rd Party plugin and modules, not actively maintained.
    • Note : They can't be installed directly by executing terraform init command.
How to define providers within your terraform file ?
  • Create an empty file within your IDE and give it a extension of  .tf
$ touch
  • Next step is to choose format to define provider from our official terraform documentation.
    • Navigate to official provider Link
    • Select your provider as per your requirement.
      • For example, I need to create an EC2 instance, hence I must select AWS as provider.
      • Incase If I want to create a Azure Blob Container, I must select azure as my provider.
  • Once provider is selected, toggle to the Documentation from Navigation bar.

  • Within documentation. scroll to the Example Usage section and look for provider section, how it has been defined.
    • Please note before you define, providers and start executing your terraform you must have the authentication mechanism ready with you, It's very obvious if you want to provision any infrastructure on any public cloud, you must be authenticated first.
    • Every Providers has different way of authentication.
    • It's not mandatory or even discouraged to keep credentials hardcoded in a file, one work around is to define environment variables and import it during runtime.
provider "aws" {
  region     = "us-east-1"
  access_key = "AKIXXXXXXXXHB5PO7T6G"
  secret_key = "UdB1/aXJ9QgbQUSBS8BS9NWdrjr3wRbjE7hKddTD"
In case, if we want to use the export method of key, we can export keys and secret during terraform init command.

provider "aws" {}
$ export AWS_ACCESS_KEY_ID="myaccesskey"
$ export AWS_SECRET_ACCESS_KEY="myaccesssecret"
$ export AWS_DEFAULT_REGION="us-east-1"
$ terraform plan
  • Below snipped is to define Azure provider
    • Azure authentication can be done using multiple methods like Azure cli authentication, service principle and other too.
# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}

Provider Versions.

Provider sits in between terraform binary and Infrastructure provisioning, Providers are set of plugins that invokes APIs to create requested infrastructure in terraform file.

Here in above diagram, we will be creating a EC2 resource from terraform file named as

provider "aws" {
  region     = "us-east-1"
  access_key = "AKIA5BMYACCESSKEY"
  secret_key = "UdB1/MYACCESSSECRETIWIW7EH303"

resource "aws_instance" "my-ec2-instance" {
  ami           = "ami-08e4e35cccc6189f4" # us-west-1
  instance_type = "t2.micro"

   tags = {
    Name = "my-ec2"

  • Provider used here is AWS.
    • Please note under the provider section, we nowhere mentioned the version of aws provider.
    • Incase, if provider version is not explicitly mentioned, it will download the latest version available during the terraform init command.
How to find, version of providers ?

How to define provider version explicitly in terraform file ?
  • This is very useful, as this is the ideal way of using providers in production environment to avoid the adverse effect of new release to our existing infrastructure.
  • Below is the way, how we can define provider version in terraform file.
provider "aws" {
  region  = "us-east-1"
  version = "3.70.0"
  • We can also use operators to define as like any other language, use version equal to, greater than, less than like below.
    • version = "3.70.0"
    • version = "<=3.70.0"
    • version = ">=3.70.0" 

As on Terraform version greater then 0.13+, Version and Providers can be stated like below.

 terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "3.73.0"

provider "aws" {
  region     = "us-east-1"

This is all about providers in this post, there are still more to explore and apply, will keep this thread updated.

Hope, this document helps you in some way !

Read more ...
Designed By Jackuna