Terraform infrastructure as code for FTW API deployment on AWS. Creates a complete FastAPI application infrastructure with GPU testing capabilities.
- AWS CLI v2 installed and configured
- Terraform >= 1.10 installed
- AWS account with appropriate permissions
This Terraform configuration creates:
- VPC: DNS support, hostnames enabled, public/private subnets across 2 AZs
- Networking: Internet Gateway, NAT Gateway(s), route tables
- Compute: Auto Scaling Group with EC2 instances for FastAPI application
- Load Balancing: Internal Application Load Balancer with HTTP
- API Gateway: HTTP API with CORS configuration and custom domain support
- CDN: CloudFront distribution for caching and global distribution
- Security: WAF with rate limiting, IAM roles, security groups, SSL certificates
- Storage: S3 bucket for model outputs with VPC endpoint
- Access: SSM Session Manager for secure instance access
With the default VPC CIDR block of 10.0.0.0/16:
| Subnet | CIDR Block | Availability Zone | Type |
|---|---|---|---|
| Public AZ1 | 10.0.0.0/24 |
us-east-1a | Public |
| Public AZ2 | 10.0.1.0/24 |
us-east-1b | Public |
| Private AZ1 | 10.0.2.0/24 |
us-east-1a | Private |
| Private AZ2 | 10.0.3.0/24 |
us-east-1b | Private |
.
├── environments/
│ └── dev/
│ ├── main.tf # Main configuration
│ ├── variables.tf # Variable definitions
│ └── terraform.tfvars # Environment-specific values
├── modules/
│ ├── vpc/ # VPC and networking
│ ├── ec2/ # Auto Scaling Group and EC2
│ ├── alb/ # Application Load Balancer
│ ├── api-gateway/ # API Gateway HTTP API
│ ├── cloudfront/ # CloudFront CDN distribution
│ ├── waf/ # Web Application Firewall
│ ├── certificate-manager/ # SSL certificates and Route53
│ ├── s3/ # S3 storage and VPC endpoint
│ ├── iam/ # IAM roles and policies
│ ├── github-deploy/ # GitHub Actions OIDC role + SSM deploy document
│ └── security-groups/ # Security group rules
└── bootstrap/
└── bootstrap-s3.sh # S3 backend setup script
- Clone this repository
- Configure AWS credentials:
aws configure - Create the S3 backend:
./bootstrap/bootstrap-s3.sh
- Navigate to environment:
cd environments/dev - Edit
terraform.tfvarswith your specific values - Initialize Terraform:
terraform init - Review the plan:
terraform plan - Apply the configuration:
terraform apply
# Basic Configuration
region = "us-east-1"
vpc_cidr_block = "10.0.0.0/16"
environment = "dev"
single_nat_gateway = true
# EC2 Configuration
instance_type = "t3.micro" # Use "g4dn.xlarge" for GPU testing
key_pair_name = "" # Leave empty for SSM-only access
# Auto Scaling Group Configuration
asg_config = {
min_size = 1
max_size = 1 # Set to 3+ to enable scaling
desired_capacity = 1
health_check_type = "EC2"
health_check_grace_period = 300
}For GPU-based inference testing, update your terraform.tfvars:
instance_type = "g4dn.xlarge" # or other GPU instance typesUse AWS Systems Manager Session Manager for secure access:
# List running instances
aws ec2 describe-instances --filters "Name=tag:Environment,Values=dev" --query 'Reservations[].Instances[?State.Name==`running`].[InstanceId,Tags[?Key==`Name`].Value|[0]]' --output table
# Connect to instance
aws ssm start-session --target i-1234567890abcdef0Merges to main in ftw-inference-api deploy automatically from GitHub Actions. The
modules/github-deploy module provides:
- A GitHub OIDC provider and the role
dev-ftw-inference-api-deploy. Only workflows onrefs/heads/mainoffieldsoftheworld/ftw-inference-apican assume it. - The SSM Command document
ftw-dev-deploy-api(singleshaparameter). It checks out that exact SHA in~/ftw-inference-apiand runsscripts/update.sh, which syncs dependencies, restarts the service, and health-checks it. The role can run only this document, and only on instances taggedPurpose=fastapi-appandEnvironment=dev. - The SSM parameter
/ftw/dev/api/deployed-sha. The workflow writes the deployed SHA here, and user data reads it at boot, so new instances start at the deployed SHA instead of the head ofmain. The initial value ismain; Terraform ignores later changes. - The CloudWatch log group
/ftw/dev/deploy(30-day retention) for command output.
One-time setup after terraform apply:
# Set the role ARN as a repo variable (not a secret)
gh variable set AWS_DEPLOY_ROLE_ARN --repo fieldsoftheworld/ftw-inference-api \
--body "$(terraform output -raw deploy_role_arn)"An AWS account can have only one OIDC provider for token.actions.githubusercontent.com.
If it already exists, set create_github_oidc_provider = false and the module looks it up.
To roll back, run the ftw-inference-api deploy workflow manually (workflow_dispatch) with an
older SHA from main. To check what a running instance would boot:
aws ssm get-parameter --name /ftw/dev/api/deployed-sha --query Parameter.Value --output textChanging user-data.sh creates a new launch template version. The ASG uses $Latest, so
the change only applies to instances launched later; it does not start an instance refresh.
To enable auto scaling for load testing:
asg_config = {
min_size = 1
max_size = 5
desired_capacity = 2
health_check_type = "ELB" # Use ALB health checks
health_check_grace_period = 600
}- Development: Use
single_nat_gateway = trueandt3.microinstances - GPU Testing: Use
g4dn.xlargeor similar GPU instances as needed - Production: Set
single_nat_gateway = falsefor high availability
Key outputs after deployment:
vpc_id- VPC identifierapi_url- Primary API URL (custom domain or CloudFront)api_gateway_stage_invoke_url- Direct API Gateway endpointroute53_name_servers- DNS name servers for custom domainssl_certificate_arn- SSL certificate ARNautoscaling_group_name- ASG name for managementlaunch_template_id- Launch template identifierami_id- AMI used by EC2 instancesdeploy_role_arn- Role for the ftw-inference-api deploy workflow (AWS_DEPLOY_ROLE_ARN)deploy_document_name- SSM document the deploy workflow runsdeployed_sha_parameter_name- SSM parameter holding the deployed SHAdeploy_log_group_name- CloudWatch log group for deploy output
To destroy the infrastructure:
cd environments/dev
terraform destroy