Terraform Instructor Day3 1
Terraform Instructor Day3 1
Terraform configurations are typically organized into files and directories. Here's a common
structure:
my-terraform-project/
├── main.tf # Core resources
├── variables.tf # Input variable definitions
├── outputs.tf # Output values
├── provider.tf # Provider configurations
├── terraform.tfvars # Input variable values
├── backend.tf # Remote backend configuration (optional)
├── modules/ # Reusable modules
│ ├── vpc/
│ │ ├── main.tf # VPC-specific resources
│ │ ├── variables.tf
│ │ ├── outputs.tf
│ └── ec2/
│ ├── main.tf # EC2-specific resources
│ ├── variables.tf
│ ├── outputs.tf
└── .terraform/ # Terraform cache and state (auto-generated)
Key Files:
main.tf: The main configuration file where resources are defined (you’re already using
this).
variables.tf: Defines input variables for your Terraform configuration (e.g., region,
instance types).
outputs.tf: Declares the outputs that Terraform will print after applying (e.g., VPC IDs,
instance IPs).
terraform.tfvars: Specifies values for variables. This file is used to override defaults
defined in variables.tf.
provider.tf: Contains the provider block(s) to configure the cloud platform (e.g., AWS,
Azure).
backend.tf: Configures the Terraform backend (e.g., S3 for remote state storage).
modules/: Used to define reusable infrastructure components (e.g., a module for VPCs,
EC2 instances, RDS, etc.).
2. Organizing Code
b. Use Modules
Modules are reusable pieces of Terraform code that encapsulate resources for a specific purpose
(e.g., a VPC module, an EC2 module). For example:
bash
Copy code
modules/
└── vpc/
├── main.tf # VPC-related resources
├── variables.tf # Input variables for the module
├── outputs.tf # Output values (e.g., VPC ID)
hcl
Copy code
module "vpc" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
environment = "dev"
}
c. Use Workspaces
Workspaces allow you to manage multiple environments (e.g., dev, staging, prod) in the same
configuration:
Commands:
o Create a workspace:
bash
Copy code
terraform workspace new dev
o Switch workspaces:
bash
Copy code
terraform workspace select prod
3. Advanced Terraform Features
Use a remote backend (e.g., AWS S3, Terraform Cloud) to store state files securely and
enable team collaboration.
Example: backend.tf
hcl
Copy code
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "state-file/terraform.tfstate"
region = "ap-southeast-2"
dynamodb_table = "terraform-locks" # Optional for state locking
}
}
b. Data Sources
Data sources allow you to query existing resources in your cloud environment.
Example: Get an existing VPC by name:
hcl
Copy code
data "aws_vpc" "default" {
filter {
name = "tag:Name"
values = ["default-vpc"]
}
}
output "default_vpc_id" {
value = data.aws_vpc.default.id
}
c. Dependency Graph
Terraform automatically builds a dependency graph between resources. You can view it
using:
bash
Copy code
terraform graph | dot -Tsvg > graph.svg
bash
Copy code
TF_LOG=DEBUG terraform apply
hcl
Copy code
variable "region" {
description = "AWS region"
default = "ap-southeast-2"
}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
hcl
Copy code
provider "aws" {
region = var.region
}
hcl
Copy code
region = "us-east-1"
instance_type = "t2.medium"
6. Outputs
hcl
Copy code
output "vpc_id" {
value = aws_vpc.my_vpc.id
}
output "public_subnet_id" {
value = aws_subnet.public_subnet.id
}
bash
Copy code
Outputs:
vpc_id = "vpc-123abc"
public_subnet_id = "subnet-456def"
Here are some fun tasks you can try to get comfortable with Terraform:
a. Multi-Region Deployment
hcl
Copy code
provider "aws" {
region = "us-east-1"
alias = "us"
}
provider "aws" {
region = "us-west-1"
alias = "west"
}
b. Dynamic Blocks
hcl
Copy code
resource "aws_security_group" "example" {
name = "example-sg"
description = "Example security group"
vpc_id = aws_vpc.my_vpc.id
dynamic "ingress" {
for_each = ["22", "80", "443"] # Ports to allow
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
}
hcl
Copy code
resource "aws_subnet" "subnets" {
count = 3
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.${count.index + 1}.0/24"
availability_zone = "ap-southeast-2a"
tags = {
Name = "Subnet-${count.index + 1}"
}
}
Next Steps
With these tips, you can take Terraform beyond the basics and build robust, reusable
infrastructure configurations.