Terraform
Terraform
Manual Terraform
Terraform Config
It uses .tf extension
Declarative Language
State Management
Terraform supports JSON
format also
State Management
The state file (terraform.tfstate) maintains a detailed
record of the current state of managed resources
File Uploads: Uploads the index.html and error.html files to the bucket.
output "name"
Virtual Private Cloud (VPC)
Virtual Private Cloud (VPC)
Asia
Website is ready
Where to deploy?
Europe
North
Asia East
Website is ready
Where to deploy? South
REGION
North
Singapore
Mumbai
Asia East
Hyderabad
a b c
Mumbai
What is Subnets?
A subnet is a smaller, segmented part of a larger
network that isolates and organizes devices within a
specific IP address range.
c
a b
CIDR (Classless Inter-Domain Routing) is a method for allocating IP
addresses and routing Internet Protocol (IP) packets.
What happens when creating subnet?
The /24 indicates that the first 24 bits are the network portion of the address.
The remaining 8 bits are available for host addresses within the network.
Useful for obtaining dynamic data that you need for your
configurations.
Data Source TASK?
terraform.tfvars
*.auto.tfvars
export TF_VAR_key=value
Terraform Functions
Terraform Functions
max(5, 12, 9)
Terraform Functions
#value = lower(local.name)
#value = startswith(local.name, "Hello")
#value = join("-", var.list)
#value = split("-", var.string)
#value = trimspace(var.string)
#value = length(var.list)
#value = merge(var.map1, var.map2)
#value = contains(var.list, "d")
#value = max(1, 2, 3) and min(1, 2, 3)
#value = abs(var.number)
#value = toset(var.list) #to convert list into set (will remove the duplicates)
#value = tolist(var.set)
Multiple Resources using
Count
for_each
Create 2 subnets
Using count
subnet-1 subnet-2
10.0.0.0/24 10.0.1.0/24
Create 2 subnets
Create 4 ec2 instance, 2 in each subnet
ec2-1 ec2-3
subnet-1 subnet-2 ec2-4
ec2-2
Create 2 subnets
Create 2 ec2 instance, 1 in each subnet
]
The flatten function in Terraform is used to
transform a list of lists into a single, flat list.
{
roles = ["AmazonS3ReadOnlyAccess", "AmazonEC2FullAccess"]
username = "baburao"
}
Terraform Modules
Terraform Modules:
README.md file
LICENSE
Examples
Push code in GitHub
Terraform Registry
Terraform Dependency
Terraform Dependencies
Resource Lifecycle
Terraform Lifecycle Block
prevent_destroy
ignore_changes
replace_triggered_by
ignore_changes
replace_triggered_by
Validations
Terraform Validations
preconditions postconditions
Implement preconditions:
Inside the resource block, add a lifecycle block.
Add precondition blocks to ensure that the security_group
id is created
Implement postcondition:
Add another lifecycle block within the resource.
Add a postcondition block to ensure that the instance has
a public IP address after creation.
assert
check "ec2_instance_validation" {
description = "Ensure EC2 instance is using an approved AMI and instance type."
assert {
condition = var.ami_id != ""
error_message = "AMI ID must not be empty."
}
assert {
condition = contains(var.production_instance_type, var.instance_type)
error_message = "Instance type must be one of the approved types for production:
${join(", ", var.production_instance_type)}."
}
}
State Manipulation
List all resources in the state:
terraform state list
Show details of a specific resource:
terraform state show <resource_address>
Move a resource to a different address:
terraform state mv <source_address> <destination_address>
Remove a resource from the state:
terraform state rm <resource_address>
Pull the current state:
terraform state pull
Push a local state file to the remote backend:
terraform state push <state_file>
List all state commands:
terraform state
Terraform Import
terraform import is a command in Terraform
that allows you to import existing
infrastructure resources into your Terraform
state.
Use-Case
Assuming you have already created an EC2 instance.
Create a resource block in tf config (initially you can
keep it empty)
Use terraform import command
terraform import aws_instance.main ec2_id
Terraform show to inspect the imported resource.
Update the resource block accordingly.
Workspaces
Allows you to manage multiple sets of
infrastructure configurations within a single
configuration directory.
tf config
Listing Workspaces
terraform workspace list
Creating a Workspace
terraform workspace new <workspace_name>
Selecting a Workspace
terraform workspace select <workspace_name>
Deleting a Workspace
terraform workspace select default
terraform workspace delete <workspace_name>
Terraform Cloud
Terraform Cloud is a managed service provided by
HashiCorp that facilitates collaboration on
Terraform configurations.