Terraform Basic Blocks
4 min readJul 31, 2022
Fundamental block
- Terraform Block
- For setting up terraform and download it’s related versions.
- For configuring some behaviours.
- It contains information about:- Terraform version, Provider requirement and Terraform state Figure 1.
- Provider Block
- For Interacting with API’S.
- Terraform Rely on provider to interact with remote systems.
- Declare providers in required_providers to install and use them
- Belong to Root module.
- We can also change settings of required providers like Figure 2.
- Resource Block
- Each Resource block define one or more Infrastructure Objects Figure 3.
- Resource syntax.
- Resource behaviours.
- Provisioners:- We can configure Resource post-creation actions
Terraform Block / Setting Block / Configuration Block
- Only constant values can be used.
- Can not use any variables and inbuilt functions of terraform.
- We are going to provide the required provider inside the Terraform Block.
- For example
- Required_version { } is the CLI lock so there is no version mismatch.
- Required_providers { } on terraform init download the all the required things.
- Backend { } for storing the state of configuration to store state information we add backend
Understanding terraform required_version
- ~> This will allows the rightmost digit to increment for example 2.0.7 and it can also be 2.0.9, 2.0.10.
- = This is for the exact equal mostly in the production environment.
Understanding required_providers
- This is where we give information about providers which is needed to install to interact with the API’s
Important note about required_version
- “~> 1.12” It will change the right most value and it will change to “1.13” and then it can break. So it’s good to use “~> 1.12.1” so it will stay on the major release
Terraform Providers
- When we do terraform init it contact terraform registry and it will download Terraform AWS Providers
- Terraform AWS provider → terraform apply → AWS API and then it will create AWS resources
- Providers and separate version and release cycle
- 3 things that we need about Terraform Providers
- Provider Requirements
- This is in the Terraform Block
- Example aws = { source = “”, version =“”} - Provider configuration
- This is in the Provider Block
- To configure the providers
- Example Provider “aws” { profile =“default”, region=“us-east-1”} - Dependency lock files
- file name .terraform.loc.hcl and it’s there is root folder
- Version related information is locked here - Required Providers
- For example aws = { source = “”, version =“”}
- This aws should be module specific and be unique per module
- Can choose name instead of aws likemyaws, aws1, aws2
- But preferred way of choosing local name is to use preferred local name of that provider
- Source is the primary location from where we can download. - Terraform Providers Registry
- Location is registry.terraform.io
- Can get module and providers - Providers Type
- Have Official Badge:- Maintained by HashiCorp
- Verified Badge:- made by 3rd party developer
- Community:- Made by individual maintainers
- Archived:- Older versions
Understanding the Providers Block
- In AWS configure for authentication on AWS we do have default profile so in provider block we add profile = “default”
- It will take credential from AWS Configure settings
Multiple providers configurations #important
- Multiple configurations for same provider and which one to choose per resource or per module basis.
- To support multiple regions.
- For example
- Provider “aws” {region = “eu-central-1”, alias = “eu-central-1”}
- Resource “aws_vpc” “vpc-eu-central-1” { provider = aws.eu-central-1 }
Dependency lock file
- When we do terraform init the .terraform.lock.hcl got created. It contains the version constraints and record files .terraform folder contain the provider information.
- Where we do terraform apply the terraform.tfstate get created. It is terraform configuration local database. What you create in the cloud it’s equivalent reference will be created locally.
- Dependency lock file (.terraform.lock.hcl):- After selecting a specific version of each dependency using version constraints Terraform remembers the decision it made in dependency lock file so that it can make the same decision in the future.
- Lock file track only provider dependency. For module it continue to use exact version constraint to ensure that terraform will always select the same module version.
- Checksum verification:- Terraform matches that each package it install matches at least one of the checksums it previously recorded in the lock file.
- Terraform configuration
- Provider version
- Module version
- Importance of dependency lock file
- For example AWS >= 2.0 if lock file then it will add the latest version and if there is lock file then the version could be (2.50.0) to maintain the consistency
Upgrade Terraform Provider Version
- command:- terraform init -upgrade it will upgrade the terraform provider