0
votes

I am trying to create an ECS blue/green deployment setup by CloudFormation. I came across this documentation which mentions that ECS blue/green deployments can be be handled by CloudFormation without needing to explicitly create the CodeDeploy application and deployment groups.

Reference:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/blue-green.html

https://docs.aws.amazon.com/codedeploy/latest/userguide/deployments-create-ecs-cfn.html

It mentions that blue/green deployments are triggered only if one of the following resources needs to be re-created:

  • AWS::ECS::TaskDefinition
  • AWS::ECS::TaskSet

My question is that, if my docker image does not change (as it always have the version "latest"), the task definition suppose will not change, then how can I trigger the CloudFormation ECS blue/green deployments?

1
I don't think you can do this. You would have to use --force-new-deployment in aws ecs cli if you just use latest only.Marcin
@Marcin thanks. I have managed to achieve the desired behavior using an explicit CodeDeploy application to handle the ECS blue/green deployment. In the CodePipeline build phase, it can optionally update the docker image tag and create an "imageDetail.json" which specifies the image URI. In the deploy phase. CodeDeploy will update the taskdef.json with the image URI, thus creating a new task definition. This will in turn update the appspec.yaml with the updated task definition. This will then trigger the ECS blue/green deployment using the updated task definition and docker image.Patrick C.
Thanks for letting me know. You can answer your own question with all these details for future reference. You can also accept it for yourself.Marcin

1 Answers

0
votes

The desired behavior currently cannot be achieved with ECS blue/green deployment setup by CloudFormation, since we cannot trigger a task definition change without updating the docker image tag.

An alternative would be using an explicit CodeDeploy application to handle the ECS blue/green deployment.

In the CodePipeline build phase, it can optionally update the docker image tag and create an "imageDetail.json" which specifies the image URI. In the deploy phase, CodeDeploy will update the taskdef.json with the image URI, thus creating a new task definition. This will in turn update the appspec.yaml with the updated task definition. This will then trigger the ECS blue/green deployment using the updated task definition and docker image.

I have put the relevant sample CloudFormation templates on GitHub for refernece. Most of the infrastructure creation can be streamlined with CloudFormation, except for the creation for CodeDeploy deployment groups. CloudFormation has not supported the creation of blue/green deployment groups yet, thus we have to create it with CLI.

Create ECS cluster, task definition and service, and expose ECS cluster behind an application load balancer

https://github.com/patrickpycheung/aws/blob/main/ECS/Create_ECS_Cluster.yaml

Create CodeDeploy application

https://github.com/patrickpycheung/aws/blob/main/CodeDeploy/Create_CodeDeploy_Application.yaml

Create CodeDeploy deployment group

https://github.com/patrickpycheung/aws/blob/main/CodeDeploy/Create_Sampe_Deployment_Group_CLI_Command.txt https://github.com/patrickpycheung/aws/blob/main/CodeDeploy/Create_Deployment_Group_CLI_Param.json

Create 3-phase CodePipeline for ECS blue/green deployment

https://github.com/patrickpycheung/aws/blob/main/CodePipeline/Create_CodePipeline.yaml https://github.com/patrickpycheung/aws/blob/main/CodeBuild/Create_CodeBuild_Project.yaml