Search code examples
amazon-web-servicesaws-cloudformationamazon-ecsaws-code-deployinfrastructure-as-code

<AWS CloudFormation> How to trigger ECS blue/green deployments without updating the docker image?


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?


Solution

  • 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