DEV Community

Cover image for Day 2/30: Understanding Terraform Providers
Anil KUMAR
Anil KUMAR

Posted on

Day 2/30: Understanding Terraform Providers

Today marks Day 2 of my #30daysofAWSTerraform challenge started by Piyush Sachdeva. Today we will deep dive into the terraform providers. What are terraform providers and how exactly they will work.

Provider:

Provider is simply a plugin that translates our terraform code into a code that your cloud provider can understand. As Cloud Provider doesn't understand HCL language of Terraform, this provider acts as a bridge between our Terraform code and Cloud.

In simple, Providers are plugins that allow Terraform to interact with cloud platforms, SaaS providers, and other APIs. For AWS, we use the hashicorp/aws provider.

terraform providers for AWS

In the above official link, you can view all the services AWS Cloud will provide for the Terraform integration.

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "6.22.1"
    }
  } 
  required_version = ">= 4.0"
}

provider "aws" {
  # Configuration options
}
Enter fullscreen mode Exit fullscreen mode

Above is the sample code for a terraform provider block:

Here we are trying to provision Infra on AWS Cloud using Terraform.

Why Version Matters:

If you do not mention any version specific, then latest version will always be picked up. This can cause some issues like your configuration might not be compatible with the latest version of Terraform. So Be careful in selecting the version while provisioning the Infrastructure.

Always select the version for which you have developed and tested for Terraform Configuration.

Version Constraints:

required_version = " >= 4.0"
where >= is the operator and 4.0 is the version.
Operator usecases:

  • = -> Exact version
  • != -> Excludes the exact version
  • >, >=, <, <= -> Allow Version when comparison is true.
  • ~> 6.7.0 -> It can be 6.7.1, 6.7.2... but not 6.8.0

Creating Resources

Resources are the things which we would like to create in AWS Cloud.

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}
Enter fullscreen mode Exit fullscreen mode

Here 'main' is not the VPC name, It is the local name of the resource through which we will refer it internally within the Terraform.

If you want to create EC2 instances inside above VPC, you can internally refer that using the above name.

resource "aws_ec2_host" "test" {
  instance_type     = "c5.18xlarge"
  availability_zone = "us-west-2a"
  vpc_id= aws_vpc.main.id
}
Enter fullscreen mode Exit fullscreen mode

Terraform Commands

  1. terraform init: This command initializes the backend, provider plugins. You can see this under the folder created under .terraform/provider/..

  2. aws configure: Before trying to create resources you need to authentication and authorize within the AWS Cloud. For this Create a User with necessary permissions and then create AWS access key and security keys.

  3. terraform plan: This creates a state file which will compare terraform configuration files with actual infrastructure in the Cloud. As Infrastructure is not there initially, it will create the Infrastructure and store it in the statefile, It will be helpful for us in future terraform commands.

Conclusion:

This is all you need to get started with Terraform providers.

Below is the Youtube Video for reference: Tech Tutorials with Piyush — “Terraform AWS Provider Explanation”

Top comments (0)