Terraform
Terraform state file
- On every apply (success) a state file representing resources created is maintained by default in the
terraform.tfstate file

Accessing attributes
- syntax for resource attributes
<type>.<id/name>.<attribute>
- To play with this we can use terraform console

- If you use attribute of resource A in resource B, the creation order will be first A then B this is referred as implcit dependency
- Resource creation order can be controlled implicitly or even explicitly by using depends_on
Activity 1: I want to create a vpc with 4 subnets in AWS (contd)
- Lets try adding 4 subnets
- Lets start with one subnet
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.82.2"
}
}
}
provider "aws" {
region = "ap-south-1"
}
resource "aws_vpc" "network" {
cidr_block = "10.100.0.0/16"
tags = {
Name = "from tf"
}
}
resource "aws_subnet" "web1" {
vpc_id = aws_vpc.network.id
availability_zone = "ap-south-1a"
cidr_block = "10.100.0.0/24"
tags = {
Name = "web-1"
}
depends_on = [aws_vpc.network]
}
- Lets add three other subnets
- web-2:
- cidr: 10.100.1.0/24
- az: ap-south-1b
- db-1:
- cidr: 10.100.2.0/24
- az: ap-south-1a
- db-2:
- cidr: 10.100.3.0/24
- az: ap-south-1b
-
Refer Here for the changes
-
Lets understand the work we have done,
- This template creates a vpc with 4 subnets in mumbai region with fixed ip ranges
Terraform style guide
TFLint
- This is a linter for terraform which cross checks your terraform templates for
- For aws related templates add a specific plugin for aws checks Refer Here
- Refer Here for aws specific configuration for tflint
Versioning constraints
Using terraform recommended file structure
- Refer Here for specific rule
- Refer Here for the changes done to re arrange work across files in terraform.
Like this:
Like Loading...