Terraform
What is Terraform?
Terraform is an infrastructure as code tool that defines both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share.
Why Terraform?
Terraform provides a unified, automated, and reliable way to manage infrastructure across different environments, enhancing collaboration, scalability, and cost efficiency.
Different Stages of terraform are:
terraform init - Initialize the working directory.
terraform plan - Create an execution plan.
terraform apply - Apply the changes required to reach the desired state.
terraform destroy - Destroy the infrastructure managed by Terraform.
Statefile: uses to keep track of the current state of your infrastructure.
Implementing S3 Backend for State Storage
Get hands-on experience configuring an S3 bucket as a backend for remote state storage. Understand how this setup improves collaboration and state management.
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "kaveri" {
instance_type = "t2.micro"
ami = "ami-053b0d53c279acc90" # change this
subnet_id = "subnet-019ea91ed9b5252e7" # change this
}
resource "aws_s3_bucket" "s3_bucket" {
bucket = "kaveri" # change this
}
resource "aws_dynamodb_table" "terraform_lock" {
name = "terraform-lock"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}
Introduction to Terraform Backends
Uncover the role of Terraform backends in remote state storage. Learn why they're essential for maintaining infrastructure state and configurations.
terraform {
backend "s3" {
bucket = "kaveri" # change this
key = "kaveri/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-lock"
}
}
State Locking with DynamoDB
Dive into state locking and the prevention of concurrent updates. Implement state locking using DynamoDB as a backend mechanism, ensuring state consistency.
Provisioners in Terraform
Provisioners are used to execute scripts or commands on a local or remote machine as part of the resource creation or destruction process. They are typically used for bootstrapping or configuring resources once they have been created.
Workspaces in Terraform offer a powerful way to manage multiple environments, providing isolation and consistency while leveraging a single configuration setup.