Infrastructure as Code (IaC) configuration for deploying and managing AWS resources using Terraform.
This Terraform project automates the provisioning of AWS EC2 instances and related infrastructure components. It follows best practices for modular, reusable, and maintainable infrastructure code.
terraform/
├── data.tf # Data sources definitions
├── launch-instance.tf # EC2 instance configuration
├── locals.tf # Local variable definitions
├── outputs.tf # Output values
├── variables.tf # Input variables
├── terraform.tfstate # State file (managed by Terraform)
├── terraform.tfstate.backup # State backup
├── .terraform.lock.hcl # Dependency lock file
├── .gitignore # Git ignore rules
├── space.sh # Utility shell script
└── readme.md # This file
- Terraform >= 1.0 (install from terraform.io)
- AWS CLI configured with valid credentials
- AWS Account with appropriate IAM permissions
- Git for version control
terraform initThis command initializes the Terraform working directory and downloads required providers.
terraform validateVerify that your configuration is syntactically valid.
terraform plan -out=tfplanReview planned changes before applying them.
terraform apply tfplanDeploy the infrastructure to AWS.
terraform destroyRemove all managed resources.
Defines input variables for the infrastructure. Customize these values before deployment.
Defines local variables used throughout the configuration for consistency and reusability.
Data sources for querying existing AWS resources (e.g., AMI IDs, availability zones).
Main configuration file for EC2 instance provisioning with associated resources.
Output values that display important information after infrastructure deployment (e.g., instance IDs, IP addresses).
This configuration typically manages:
- EC2 Instances
- Security Groups
- VPC/Network configurations
- IAM Roles and Policies
- Storage volumes
terraform plan
terraform applyterraform apply -var="instance_type=t3.medium" -var="instance_count=2"terraform output
terraform output instance_idsterraform refresh- State Files:
terraform.tfstateandterraform.tfstate.backupstore infrastructure state - Remote State: Consider using S3/DynamoDB for team environments
- State Locking: Enable state locking to prevent concurrent modifications
Create a backend.tf file:
terraform {
backend "s3" {
bucket = "your-terraform-state-bucket"
key = "docker/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-locks"
}
}✓ Version Control: Commit .tf files but exclude state files
✓ State Management: Use remote backends for team collaboration
✓ Variable Organization: Use .tfvars files for environment-specific values
✓ Lock Files: Commit .terraform.lock.hcl for dependency consistency
✓ Code Review: Use terraform plan output for change reviews
✓ Naming Conventions: Use descriptive names for resources
✓ Documentation: Keep this README updated with infrastructure changes
✓ Monitoring: Use AWS CloudWatch and CloudTrail for resource monitoring
| Command | Purpose |
|---|---|
terraform init |
Initialize working directory |
terraform validate |
Check syntax |
terraform plan |
Preview changes |
terraform apply |
Deploy infrastructure |
terraform destroy |
Remove infrastructure |
terraform refresh |
Update state file |
terraform output |
Display outputs |
terraform state list |
List resources in state |
terraform state show <resource> |
Show resource details |
terraform fmt |
Format configuration files |
terraform force-unlock <LOCK_ID>terraform init -upgrade
rm -rf .terraform
terraform initterraform refresh
terraform plan- Never commit sensitive data (passwords, API keys) to Git
- Use AWS Secrets Manager or Parameter Store for sensitive values
- Enable state file encryption
- Implement IAM policies with least privilege principle
- Use VPC security groups to restrict access
- Regularly audit infrastructure changes
- Create a feature branch for changes
- Run
terraform fmtto format code - Run
terraform validateto check syntax - Test with
terraform planbefore merging - Update this README if infrastructure changes
- Submit pull request for review
[Add your license here]
Last Updated: March 26, 2026 Maintained By: [Your Team/Name] Status: Active