AWS ECS Notes
ECS Fargate is the AWS no-K8s path; the image and env contract are identical, ECS is just the scheduler. On EKS, prefer the Helm chart. This page covers the ECS-specific glue: the CloudFormation template, the task definition, the load balancer, RDS, and the migration one-shot.
CloudFormation template
ecs/cloudformation.yaml deploys the service on Fargate behind an Application Load Balancer, against a PostgreSQL you already run:
- Secrets: the database password comes from the Secrets Manager secret you name (
DBPasswordSecretArn, withDBPasswordSecretKeyset topasswordfor a secret RDS manages). The stack generates the session signing secret and, unless you name your existing one inEncryptionKeySecretArn, the credential encryption key; it keeps that key when the stack is deleted. No secret is a parameter value or a task environment variable. - HTTPS only:
PublicHostnameand an ACMCertificateArnfor it are required. The listener uses the TLS 1.2+ policyELBSecurityPolicy-TLS13-1-2-2021-06, port 80 redirects to 443, and the load balancer drops invalid headers. Point the host name at theLoadBalancerDNSoutput. - Private by default: the load balancer is
internaland accepts onlyAllowedCidr(the VPC's range when empty); setLoadBalancerSchemetointernet-facingwith publicLoadBalancerSubnetIdsto publish it. Tasks get no public IP unlessAssignPublicIpisENABLED, so give them private subnets with a NAT gateway or VPC endpoints. Only port 3000 is behind the load balancer, and the tasks accept traffic only from it. - TLS to the database:
DBTlsModedefaults toverify-full, and the task trusts the RDS certificate authorities bundled in the image. - Logs and secrets are encrypted with a stack KMS key that rotates yearly; the cluster has Container Insights on, and a failed deployment rolls back.
Every secret it reads needs no extra permission unless it is encrypted with your own KMS key; then allow the stack's task execution role to decrypt with it.
Image registry
Fargate can pull directly from ECR Public; copy the image to your own ECR for in-region pulls, immutable-tag policies and scan-on-push:
aws ecr get-login-password --region us-east-1 | \\
docker login --username AWS --password-stdin <ACCOUNT>.dkr.ecr.us-east-1.amazonaws.com
docker pull public.ecr.aws/n6a1z5b2/db-studio:1.0.2
docker tag public.ecr.aws/n6a1z5b2/db-studio:1.0.2 <ACCOUNT>.dkr.ecr.us-east-1.amazonaws.com/simcha-db-studio:1.0.2
docker push <ACCOUNT>.dkr.ecr.us-east-1.amazonaws.com/simcha-db-studio:1.0.2
Task definition essentials
- CPU / memory: 1 vCPU / 2048 MB is a sensible starting size. Scale up if you have many concurrent users.
- Container port mappings: 3000 only. The API on 5002 stays inside the container; the web server forwards
/apito it. - Secrets: Reference Secrets Manager or SSM Parameter Store ARNs in the
secretsblock of the task definition, never hard-codeJWT_SECRET/ENCRYPTION_KEY/DB_DEVOPS_DB_PASSWORDin the env block. - Logs:
awslogsdriver streaming to your log-aggregation system. - Health check:
command: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:5002/api/health || exit 1"]with a 60s start period. - Architecture:
X86_64. The image is built forlinux/amd64only, so anARM64(Graviton) task cannot run it.
Migrations on ECS
Set MIGRATE_ON_BOOT=false on the service and gate the schema change with a one-shot task: a separate task definition on the same image with the command overridden to node dist/server/database/migrate.js. In the pipeline, aws ecs run-task, poll to a successful STOPPED exit (code 0), then update the service. This stops the deploy if the migration fails.
ALB configuration
- One target group for port 3000, health check
/api/healthon that port, healthy threshold 2. - An HTTPS listener with an ACM certificate and a TLS 1.2+ policy, and port 80 redirecting to it.
- Drop invalid header fields (
routing.http.drop_invalid_header_fields.enabled), and keep the load balancerinternalunless people outside your network must reach it. - Set
TRUST_PROXYto the VPC's range so audit entries and rate limits see each client's address.
RDS for the metadata database
- Start with
db.t3.smallMulti-AZ, and scale up when its CPU, memory or connection count runs high. - Place the RDS instance in the same VPC as the ECS cluster, in private subnets, with a security group that only accepts 5432 from the ECS task SG.
- Enable automated backups (default 7 days), and turn on Performance Insights for query-level visibility.
- TLS: set
PGSSLMODE=verify-fullandNODE_EXTRA_CA_CERTS=/app/certs/aws-rds-global-bundle.pemon the task, so the connection checks the RDS certificate.DB_DEVOPS_DB_HOSTtakes a host name only; a?sslmode=suffix makes it unresolvable.
Updating the service
# Mirror the new tag into your registry
docker pull public.ecr.aws/n6a1z5b2/db-studio:1.0.2
docker tag public.ecr.aws/n6a1z5b2/db-studio:1.0.2 <ACCOUNT>.dkr.ecr.us-east-1.amazonaws.com/simcha-db-studio:1.0.2
docker push <ACCOUNT>.dkr.ecr.us-east-1.amazonaws.com/simcha-db-studio:1.0.2
# Run the migration task and wait
aws ecs run-task --task-definition simcha-db-studio-migrate:<rev> ...
# Update the app service to the new task definition revision
aws ecs update-service \\
--cluster <cluster> --service simcha-db-studio \\
--task-definition simcha-db-studio:<new-rev> \\
--force-new-deployment
Rolling deploys with maximumPercent: 200 / minimumHealthyPercent: 100 give zero-downtime updates.