DEV Community

Bhaskara teja Bulusu
Bhaskara teja Bulusu

Posted on • Edited on

Day-02: Terraform Provider

**

Terraform Provider

**
Terraform provider is a plugin which translates the terraform code/scripts written in HCL language to the language that is understood by the target API's which can be AWS, Azure, GCP or it can be other services such as Docker, k8s, etc

This terraform provider is initiated when we run terraform init command in the terminal.

Terraform documentation is used to write code for particular resources
the documentation varies from provider to provider.

In the terraform script, there are two versions
version = this version is the provider version maintained by provider (AWS, AZ, GCP)
required_version = this version is terraform core version maintained by Hashicorp

Why version matters?
If we don't specify the version, the latest version will be used automatically. but using the latest version may not be compatible with the configuration. So, mentioning the version is a good practice.

Version Constraints
= 1.2.3 - Exact version

= 1.2 - Greater than or equal to
<= 1.2 - Less than or equal to
~> 1.2 - Pessimistic constraint (allow patch releases)
= 1.2, < 2.0 - Range constraint

Using pessimistic constraint for versions is a best practice.

here's the example code for version

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"  // this means the terraform will use the major
                            version 6 and will also use minor versions  
                            such as 6.1, 6.2 etc if present
    }
  }
}

provider "aws" {
  region = "us-east-1"
}
Enter fullscreen mode Exit fullscreen mode

To create an s3 bucket and a vpc in aws, use the terraform documentation for aws and find the resources we want to create and modify them according to our requirements. In terraform, the keyword 'resource' followed by the type of resource and the name(internal variable) of the resource and then the configurations for the resource are written

example for an s3 bucket:

resource "aws_s3_bucket" "mys3bucket"{
    bucket = "unique_bucket_name"

    tags = {
        Name = "s3 bucket day 2"
        environment = "dev" 

   }
}
Enter fullscreen mode Exit fullscreen mode

Prerequisites before running terraform commands
Generally, to provision the resources using terraform we need to connect to our aws account. This is generally done by the below command:
aws configure

*** Make sure that the IAM user must have an access key to login via Console

Commands to run terraform scripts:

terraform init  // initializes the working directory preparing the 
                  environment for running the subsequent operations
terraform plan  // gives a blueprint of the resources that are going to 
                   be created/updated/deleted
terraform apply  // provisions the resources by taking consent from the  
                    user in the command line
terraform apply --auto-approve  // this command bypasses the confirmation  
                           step from the user and provisions the resources
terraform destroy // destroys the resources by taking consent from the 
                     user in the command line
terraform destroy --auto-approve  //this command bypasses the confirmation  
                           step from the user and deletes the resources
Enter fullscreen mode Exit fullscreen mode

Reference Video : https://youtu.be/JFiMmaktnuM?si=-vK_KhxzJfD4tKjg

@piyushsachdeva

Top comments (0)