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
--force-new-deployment
in aws ecs cli if you just uselatest
only. – Marcin