Amazon ECS blue/green deployments with Amazon VPC Lattice

AWS Lambda Test hook → Amazon VPC Lattice Routing rules → AWS Fargate Blue → AWS Fargate Green → Amazon CloudWatch Alarms

Amazon ECS on AWS Fargate blue/green with Amazon VPC Lattice test and production listener rules, an AWS Lambda test hook and an Amazon CloudWatch alarm for rollback.

This pattern deploys an Amazon ECS on AWS Fargate service that uses the built-in blue/green deployment strategy with Amazon VPC Lattice. The VPC Lattice service has a listener with a test rule (header x-environment: test) and a production rule, and two IP target groups that ECS registers tasks into.
When you deploy a new version, ECS starts the green tasks and sends test traffic to them. An AWS Lambda lifecycle hook in the VPC calls the VPC Lattice test rule, compares the version it receives with the expected one and returns SUCCEEDED or FAILED. Only then does ECS shift production traffic. An Amazon CloudWatch alarm on VPC Lattice 5xx responses rolls the deployment back automatically.
The VPC Lattice service uses auth type NONE, so any client in the associated VPC can call it. For production, use auth type AWS_IAM with an auth policy.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/ecs-vpc-lattice/typescript/cdk

Deploy

npm installREGION=${AWS_REGION:-$(aws configure get region)}PL=$(aws ec2 describe-managed-prefix-lists --region $REGION --filters "Name=prefix-list-name,Values=com.amazonaws.$REGION.vpc-lattice" --query "PrefixLists[0].PrefixListId" --output text)npx cdk deploy --parameters LatticePrefixListId=$PL


Testing

Read the stack outputs, then deploy a second version with npx cdk deploy --parameters LatticePrefixListId=$PL -c appVersion=v2, with PL set as in the deployment steps (look it up again if you opened a new shell).
Watch the deployment with aws ecs list-service-deployments and aws ecs describe-service-deployments, the production listener rule weights with aws vpc-lattice get-rule, and the hook Lambda logs with aws logs filter-log-events. The hook log shows the test listener returning the new version before production traffic shifts.
See the GitHub repo for the exact commands and expected output.

Cleanup

Delete the stack: npx cdk destroy.

Created by:

Nithin Chandran R

Nithin Chandran R

Technical Account Manager at AWS