This repository documents my journey of learning and practicing Terraform. It includes various exercises, examples, and modules to understand Infrastructure as Code (IaC) concepts and Terraform's capabilities.
The repository is divided into multiple projects and modules, each focusing on specific Terraform concepts:
-
Goal: Create a scalable infrastructure with Terraform modules.
-
Features:
-
Compute Module:
- Creates a golden AMI with Apache pre-installed using
aws_ami_from_instance. - Configures an Auto Scaling Group (ASG) with 3 instances using
aws_autoscaling_group. - Uses
local-execprovisioner to terminate temporary instances after AMI creation.
- Creates a golden AMI with Apache pre-installed using
-
Network Module:
- Sets up a VPC with public and private subnets.
- Configures an Application Load Balancer (ALB) with health checks.
- Implements security groups to restrict access to specific IP ranges and ports.
-
Key Concepts:
- Terraform modules for resource segregation.
- Dynamic tagging using
mergefunction. - High availability with ASG and ALB.
-
- Goal: Experiment with Terraform utilities and validation tools.
- Features:
- Pre-commit hooks for linting and validation (
tflint,tfsec,terraform-docs). - Validation blocks in variables to enforce input constraints.
- Example EC2 instance with encrypted root volume and metadata options.
- Pre-commit hooks for linting and validation (
- Key Concepts:
- Using
pre-commitfor code quality. - Validation blocks for input constraints.
- Provisioners like
local-execfor custom commands.
- Using
- Resources are segregated into modules (
compute,network, etc.) for better organization and reusability.
-
local-exec: Used to execute shell commands, e.g., terminating temporary EC2 instances after creating a golden AMI. -
Example:
resource "null_resource" "delete_tmp_instance" { provisioner "local-exec" { command = "aws ec2 terminate-instances --instance-ids ${aws_instance.tmp_golden_instance.id}" } }
-
Tags are dynamically generated using the
mergefunction to combine static and variable-based values. -
Example:
tags = merge( var.instance_tags, { Name = "${var.project_name}-${var.environment}-instance" } )
-
Enforce constraints on variable inputs to ensure correctness.
-
Example:
variable "ami" { validation { condition = can(regex("^ami-[a-zA-Z0-9]+$", var.ami)) error_message = "AMI ID must follow the format 'ami-xxxxxx'." } }
- Configured an Auto Scaling Group (ASG) with a Launch Template.
- Integrated an Application Load Balancer (ALB) with health checks for high availability.
-
Tools used:
-
tflint: Linting for Terraform code. -
tfsec: Security scanning for Terraform configurations. -
terraform-docs: Automatically generate module documentation. -
Setup:
pre-commit install --install-hooks pre-commit run --all-files
checkov,terrascan, andtfsecwere used to validate security and compliance.
- Used
aws_ami_from_instanceto create a reusable AMI with pre-installed software.
-
Setup:
- Install Terraform: Terraform CLI.
- Configure AWS CLI with credentials.
-
Run Terraform:
terraform init terraform plan terraform apply