Skip to main content

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

Open terraform.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:
Add these to your terraform.tfvars:
Keep these secrets secure! They protect your API from unauthorized access.

Build Lambda Deployment Package

The Lambda function requires dependencies packaged into a ZIP file.
1

Navigate to Backend Directory

2

Run Build Script

Expected output:
3

Copy Package to Root

Terraform expects the package in backend/ directory:
4

Verify Package

The Lambda package includes all Python dependencies (requests, boto3, etc.) needed to trigger the recrawl endpoint.

Initialize Terraform

Prepare Terraform to deploy infrastructure.
Expected output:
If initialization succeeds, you’re ready to deploy!

Review Infrastructure Plan

Before deploying, review what Terraform will create:
This shows a detailed list of resources to be created:
Review the plan carefully. Ensure VPC and subnet IDs are correct before proceeding.

Deploy Infrastructure

Apply the Terraform configuration to create all AWS resources:
Terraform will show the plan again and prompt for confirmation:
Type yes and press Enter.

Deployment Progress

Terraform will create resources in order (respecting dependencies):
1

IAM Roles & Policies (1 min)

Creates execution roles for ECS and Lambda with necessary permissions.
2

Network Resources (2 min)

Creates security groups, Application Load Balancer, target groups, and listeners.
3

Compute Resources (2 min)

Creates ECR repository, ECS cluster, task definition, and service.
4

Lambda & Automation (1 min)

Uploads Lambda package to S3, creates Lambda function and EventBridge schedule.
5

Monitoring & Alarms (2 min)

Creates CloudWatch log groups, SNS topic, and 10 metric alarms.
Total deployment time: 5-10 minutes

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

Expected status: ACTIVE

Check ECR Repository

Repository should exist but have no images yet (that’s next step).

Check Load Balancer

Expected state: active

Confirm SNS Subscription

Check your email for SNS subscription confirmation:
  1. Look for email from AWS Notifications <no-reply@sns.amazonaws.com>
  2. Subject: “AWS Notification - Subscription Confirmation”
  3. Click “Confirm subscription” link
You won’t receive CloudWatch alerts until you confirm the SNS subscription!

Terraform State Management

Local State File

Terraform stores infrastructure state in terraform.tfstate. This file is critical for managing resources.
Protect terraform.tfstate: Never commit to version control! Add to .gitignore.
For production or team environments, store state remotely:
  1. Create S3 bucket for state:
  1. Enable versioning:
  1. Add backend configuration to main.tf:
  1. Migrate state:

Common Issues & Solutions

Problem: Invalid subnet IDs in terraform.tfvarsSolution: Verify subnet IDs:
Ensure subnets exist and are in different availability zones.
Problem: lambda-deployment.zip missingSolution: Build Lambda package:
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.
Problem: AWS user lacks required permissionsSolution: Attach AdministratorAccess policy (or create custom policy with required permissions).

Update Infrastructure

To modify infrastructure after initial deployment:
  1. Edit terraform.tfvars or *.tf files
  2. Review changes: terraform plan
  3. Apply changes: terraform apply
Terraform only modifies changed resources.

Destroy Infrastructure

Destructive Action: This deletes ALL AWS resources created by Terraform.
To tear down the entire infrastructure:
Type yes to confirm deletion.

Next Steps

Docker & ECS Deployment

Build Docker image and deploy to ECS Fargate