How to use a blue/green deployment style with CodeDeploy and Auto Scaling Groups that allows for repetitive deployments in Terraform?

I have a load-balanced EC2 instance that hosts a web API that I am trying to use AWS CodeDeploy to deploy new versions of the API with. I am trying to make use of a blue/green deployment style, where the “blue” instance is the original EC2 that is terminated once the “green” instance has the application installed onto it and traffic is directed to it.

Our AWS infrastructure is setup/deployed using terraform – this includes the auto scaling group for the EC2, the CodeDeploy application and configuration, load balancer, etc. My understanding is for the CodeDeploy aws_codedeploy_deployment_group Terraform resource, I should stay away from using COPY_AUTO_SCALING_GROUP within the green_fleet_provisioning_option. There is a note on the linked terraform page that explains why:

When using green_fleet_provisioning_option with the COPY_AUTO_SCALING_GROUP action, CodeDeploy will create a new ASG with a different name. This ASG is not managed by terraform and will conflict with existing configuration and state. You may want to use a different approach to managing deployments that involve multiple ASG, such as DISCOVER_EXISTING with separate blue and green ASG.

If I use COPY_AUTO_SCALING_GROUP to copy the existing auto scaling group during a deployment, CodeDeploy will copy the existing auto scaling group, give it a new name, and use it for the green fleet. The issue with this is that the terraform state becomes unaware of this auto scaling group, meaning a re-deployment of our infrastructure would cause duplicate EC2s to be created (and running). This causes issues with future deployments.

Like the note says, I should probably be using DISCOVER_EXISTING (i.e. manually provision instances for a deploy). The CodeDeploy deployment group configuration setting in the console says for this:

I will specify here the instances where the current application revision is running. I will specify the instances for the replacement environment when I create a deployment.

“Here” being the deployment group configuration, which is setup via the terraform in my case. For the “specify the instances for the replacement environment” part, you would provide a --target-instances option when you execute the create-deployment command using the AWS CLI, as described in the AWS Documentation. For this option, you can supply either an auto scaling group name, or EC2 tag sets as a JSON string. I opted for an auto scaling group name for this option, where this auto scaling group is also created via our terraform. Thus I end up with two auto scaling groups where each has a desired capacity of only one, and after deploying the terraform, results in two EC2s hosting my API.

To perform a deployment then, this is the resulting command to start the deployment (called via a GitHub action):

aws deploy create-deployment --application-name <app_name> --s3-location bucket=<bucket_Name>,key=revision_LATEST.zip,bundleType=zip --deployment-group-name <deployment_group_name> --deployment-config-name CodeDeployDefault.OneAtATime --description "My description" --region <region> --target-instances={"autoScalingGroups":["<my_green_auto_scaling_group_name"]}

At this point, this deployment runs and works. But afterwards, you can no longer do anymore deployments because the target instance auto scaling group has now essentially become the “blue” group, with no “green” group existing anymore. In fact, CodeDeploy will automatically update the deployment group configuration section where you specify the instances that are running the current version of your application to be that former target instance auto scaling group. If I try to re-run the CLI command, the deployment fails during the “Installing application on replacement instances” step with:

The deployment failed because no instances were found for your deployment group

It feels like I am missing something. I would think the CodeDeploy should be aware that it needs a green instance in order to do a deployment. Am I suppose to create a new auto scaling group after each deployment and somehow keep my GitHub Action aware of this auto scaling group for when I call the CLI command? I am not fond of having two EC2s running the API as well upon initial deployment of the terraform. Ideally, I’d like a way to have only one EC2 at a time running, and a way to continuously allow deployments to occur using the blue/green deployment style. Is this possible?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật