Overview
Terraform is used to provision all AWS infrastructure including ECS, ECR, Application Load Balancer, Lambda, and monitoring resources.What Terraform Creates
Compute
- ECS Fargate cluster and service
- ECR repository for Docker images
- Lambda function for scheduled crawls
Networking
- Application Load Balancer
- Target groups and listeners
- Security groups
Monitoring
- CloudWatch log groups
- CloudWatch alarms (10 alerts)
- SNS topic for notifications
Automation
- EventBridge cron schedule
- Lambda permissions
- IAM roles and policies
Configure Terraform Variables
Clone or Navigate to Terraform Directory
Copy Example Variables File
Edit Configuration
Openterraform.tfvars in your editor:
Required Variables
Fill in all the following variables with values collected from previous steps:Generate Security Keys
Generate strong random keys for API authentication:terraform.tfvars:
Build Lambda Deployment Package
The Lambda function requires dependencies packaged into a ZIP file.The Lambda package includes all Python dependencies (requests, boto3, etc.) needed to trigger the recrawl endpoint.
Initialize Terraform
Prepare Terraform to deploy infrastructure.If initialization succeeds, you’re ready to deploy!
Review Infrastructure Plan
Before deploying, review what Terraform will create:Expected Terraform Plan Output
Expected Terraform Plan Output
Review the plan carefully. Ensure VPC and subnet IDs are correct before proceeding.
Deploy Infrastructure
Apply the Terraform configuration to create all AWS resources:yes and press Enter.
Deployment Progress
Terraform will create resources in order (respecting dependencies):Network Resources (2 min)
Creates security groups, Application Load Balancer, target groups, and listeners.
Lambda & Automation (1 min)
Uploads Lambda package to S3, creates Lambda function and EventBridge schedule.
Deployment Complete
When finished, Terraform outputs important values:Infrastructure successfully deployed! Save these output values.
Retrieve Terraform Outputs
You can retrieve outputs anytime:Verify Resource Creation
Check ECS Cluster
ACTIVE
Check ECR Repository
Check Load Balancer
active
Confirm SNS Subscription
Check your email for SNS subscription confirmation:- Look for email from
AWS Notifications <[email protected]> - Subject: “AWS Notification - Subscription Confirmation”
- Click “Confirm subscription” link
Terraform State Management
Local State File
Terraform stores infrastructure state interraform.tfstate. This file is critical for managing resources.
Recommended: Remote State Backend
Configure S3 Backend for Team Collaboration
Configure S3 Backend for Team Collaboration
For production or team environments, store state remotely:
- Create S3 bucket for state:
- Enable versioning:
- Add backend configuration to
main.tf:
- Migrate state:
Common Issues & Solutions
Error: Subnet not found
Error: Subnet not found
Problem: Invalid subnet IDs in Ensure subnets exist and are in different availability zones.
terraform.tfvarsSolution: Verify subnet IDs:Error: Lambda package not found
Error: Lambda package not found
Problem:
lambda-deployment.zip missingSolution: Build Lambda package:Error: Certificate validation pending
Error: Certificate validation pending
Problem: ACM certificate waiting for DNS validationSolution: This is expected. Certificate validation happens after DNS records are added. The ALB will use HTTP (port 80) until then.
Error: Insufficient IAM permissions
Error: Insufficient IAM permissions
Problem: AWS user lacks required permissionsSolution: Attach
AdministratorAccess policy (or create custom policy with required permissions).Update Infrastructure
To modify infrastructure after initial deployment:- Edit
terraform.tfvarsor*.tffiles - Review changes:
terraform plan - Apply changes:
terraform apply
Destroy Infrastructure
To tear down the entire infrastructure:yes to confirm deletion.
Next Steps
Docker & ECS Deployment
Build Docker image and deploy to ECS Fargate