This directory contains Infrastructure as Code (IaC) configurations using Terraform.
terraform/
├── main.tf # Main configuration
├── variables.tf # Variable definitions
├── outputs.tf # Output definitions
├── providers.tf # Provider configurations
├── terraform.tfvars.example # Example variables
└── modules/ # Reusable modules
├── aws/
│ └── vpc/
└── azure/
└── resource_group/
-
Initialize Terraform:
cd terraform terraform init -
Copy and configure variables:
cp terraform.tfvars.example terraform.tfvars # Edit terraform.tfvars with your values -
Plan your infrastructure:
terraform plan
-
Apply changes:
terraform apply
Use Terraform workspaces for different environments:
# Create workspace
terraform workspace new dev
terraform workspace new staging
terraform workspace new prod
# Switch workspace
terraform workspace select dev- Always run
terraform planbeforeapply - Use version constraints for providers
- Store state files remotely (S3, Azure Storage)
- Use modules for reusable components
- Tag all resources consistently
- Never commit
.tfvarsfiles with secrets
Modules are located in the modules/ directory. Example usage:
module "vpc" {
source = "./modules/aws/vpc"
project_name = var.project_name
environment = var.environment
cidr_block = "10.0.0.0/16"
}# Format code
terraform fmt
# Validate configuration
terraform validate
# Show current state
terraform show
# List resources
terraform state list
# Import existing resource
terraform import aws_instance.example i-1234567890abcdef0
# Destroy infrastructure
terraform destroy