Overview
The base infrastructure is managed separately and provides foundational resources that multiple services share. This reduces costs and simplifies management.How base infrastructure is accessed
The base infrastructure state is read via aterraform_remote_state data source in provider.tf:
Base infrastructure resources
1. VPC ID
What it provides: The Virtual Private Cloud that all resources live in. How it’s used: Referenced when creating security groups. Code location:aws_security_group.ecs(ECS tasks security group)aws_security_group.postgres(RDS security group)
2. Public subnet IDs
What it provides: Subnets with internet access via an Internet Gateway. How it’s used: ALB is placed in public subnets to accept traffic from the internet. Code location:3. Private subnet IDs
What it provides: Subnets without direct internet access (use NAT Gateway for outbound). How it’s used:- ECS tasks are deployed in private subnets for security
- RDS database is placed in private subnets
- ECS tasks don’t need direct internet access (use VPC endpoints)
- RDS should never be exposed to the internet
4. ECS cluster ID
What it provides: A shared ECS cluster where multiple services can run. How it’s used: The ECS service is deployed to this cluster. Code location:- Reduced management overhead
- Centralized monitoring
- Cost efficiency
5. ECS cluster name
What it provides: The human-readable name of the ECS cluster. How it’s used: CloudWatch alarms use the cluster name as a dimension. Code location:6. Hosted zone
What it provides: The Route53 hosted zone for the domain (e.g.,staging.commenda.io or commenda.io).
How it’s used:
- Construct the FQDN for the service
- Create DNS records pointing to the ALB
- Staging:
address.in.staging.commenda.io - Production:
address.in.commenda.io
7. VPC endpoints (implicit use)
What it provides: Private connections to AWS services without going through the internet. VPC endpoints created by base infrastructure:- ECR API - Pull Docker images
- ECR DKR - Docker registry operations
- S3 - Access ECR image layers
- CloudWatch Logs - Send application logs
- Secrets Manager - Retrieve secrets
- ECS tasks don’t have public IPs
- Without VPC endpoints, they couldn’t pull images or send logs
- Reduces data transfer costs (no NAT Gateway charges for AWS service traffic)
FQDN construction
The fully qualified domain name (FQDN) is constructed using base infrastructure outputs:| Environment | Subdomain | Region | Hosted zone | Result |
|---|---|---|---|---|
| Staging | address | in | staging.commenda.io | address.in.staging.commenda.io |
| Production | address | in | commenda.io | address.in.commenda.io |
ECR image URI construction
The Docker image URI is constructed using the AWS account ID and region:| Environment | AWS account | Region | Image tag | Result |
|---|---|---|---|---|
| Staging | 127214192604 | ap-south-1 | v0.1.0-rc.1 | 127214192604.dkr.ecr.ap-south-1.amazonaws.com/staging/address-api:v0.1.0-rc.1 |
| Production | 429032495558 | ap-south-1 | v0.1.0 | 429032495558.dkr.ecr.ap-south-1.amazonaws.com/prod/address-api:v0.1.0 |
What the Address API creates
While the base infrastructure provides foundational resources, the Address API creates its own:| Resource | Why not shared |
|---|---|
| ALB | Each service needs its own load balancer for isolation and independent scaling |
| Target group | Service-specific health checks and routing |
| ACM certificate | Service-specific domain name |
| Route53 record | Service-specific DNS entry |
| Security groups | Service-specific network rules |
| ECS service | Service-specific task management |
| Task definition | Service-specific container configuration |
| RDS instance | Service-specific database |
| CloudWatch log group | Service-specific logs |
| Secrets | Service-specific credentials |
Benefits of shared infrastructure
Cost savings
- VPC: $0 (shared across all services)
- VPC endpoints: ~$7/month per endpoint (shared across all services)
- ECS cluster: $0 (pay only for tasks)
- NAT Gateway: ~$32/month (shared across all services)
Simplified management
- Single VPC: One place to manage network configuration
- Centralized DNS: All services use the same hosted zone
- Consistent networking: All services follow the same patterns
Security
- Network isolation: All services in the same VPC can communicate securely
- Centralized VPC endpoints: Consistent access to AWS services
- Shared security groups: Can reference other services’ security groups
Terraform workspace mapping
The base infrastructure uses the same workspace names as the Address API:| Workspace | Environment | AWS account |
|---|---|---|
staging | Staging | 127214192604 |
prod | Production | 429032495558 |
terraform workspace select staging, it automatically reads the staging base infrastructure state.
Troubleshooting
Error: “No outputs found for region ‘in’”
Cause: The base infrastructure hasn’t been deployed for this region. Fix: Deploy the base infrastructure first or check the region name.Error: “VPC not found”
Cause: The base infrastructure state is not accessible or the workspace is incorrect. Fix:- Verify you’re in the correct workspace:
terraform workspace show - Check the base infrastructure state exists:
aws s3 ls s3://tofu-backend-429032495558/env:/staging/