Skip to main content

EC2/On-Premise Server Blue/Green Deployment Group construct

View on Construct Hub

GitHub Actions Workflow Status NPM Downloads NPM License

This construct creates a CodeDeploy Deployment Group for an EC2/On-Premises Deployment Group using the Blue/Green Deployment configuration.

ATTENTION: At the moment, this construct only supports the EC2 deployments with Auto Scaling Groups. The construct does not support neither the EC2 tag-based deployments nor the On-Premises tag-based deployments.

Table of Contents

Overview

Blue/Green deployments is popular deployment strategy that allows you to deploy your application to a new set of instances while keeping the old set of instances running. This allows you to switch traffic from the old set of instances to the new set of instances in a controlled manner.

This construct creates a CodeDeploy Deployment Group for an EC2 using the Blue/Green deployment configuration. The Deployment Group is associated with an Auto Scaling Group and a Load Balancer Target Group.

When a deployment is triggered, the Deployment Group will create a new Auto Scaling Group with the same configuration as the original Auto Scaling Group. The new Auto Scaling Group is called the Green Fleet. The Deployment Group will deploy the new revision of the application to the Green Fleet. After the deployment is successful, the Deployment Group will switch the traffic from the original Auto Scaling Group (Blue Fleet) to the Green Fleet. The Deployment Group will then terminate the instances in the original Auto Scaling Group (if configured to do so). The Deployment Group will wait for the original instances to be terminated before marking the deployment as successful.

You can configure the deployment to automatically rollback if the deployment fails, if the deployment is stopped, or if the deployment is in alarm.

Installation

NPM package: https://www.npmjs.com/package/@otocolobus/aws-cdk-serverbluegreendeploymentgroup

npm install @otocolobus/aws-cdk-serverbluegreendeploymentgroup

PyPI package: https://pypi.org/project/aws-cdk-serverbluegreendeploymentgroup/

pip install aws-cdk-serverbluegreendeploymentgroup

Usage

import { ServerBlueGreenDeploymentGroup } from "@otocolobus/aws-cdk-serverbluegreendeploymentgroup";

class MyStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // ... other resources for LoadBalancer, AutoScalingGroup, etc.

    const application = new codedeploy.ServerApplication(this, "Application", {
      applicationName: "MyApplication",
    });

    // role required by CodeDeploy to do Blue/Green deployments with EC2 Auto Scaling Groups
    const role = new iam.Role(this, "Role", {
      assumedBy: new iam.ServicePrincipal("codedeploy.amazonaws.com"),
      managedPolicies: [
        iam.ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSCodeDeployRole"),
      ],
      inlinePolicies: {
        ASGWithLaunchTemplate: new iam.PolicyDocument({
          statements: [
            new iam.PolicyStatement({
              actions: [
                "ec2:CreateTags",
                "ec2:RunInstances",
                "iam:PassRole",
                "ssm:GetParameters",
              ],
              resources: ["*"],
            }),
          ],
        }),
      },
    });

    const deploymentGroup = new ServerBlueGreenDeploymentGroup(
      this,
      "DeploymentGroup",
      {
        application,
        deploymentGroupName: "DeploymentGroup",
        role,
        loadBalancers: [
          codedeploy.LoadBalancer.application(loadBalancerTargetGroup),
        ],
        autoScalingGroups,
        // ... other optional properties
      },
    );

    // ... other resources for CodeDeploy and CodePipeline
    // where buildOutput is the output of the CodeBuild action

    pipeline.addStage({
      stageName: "Deploy",
      actions: [
        new codepipeline_actions.CodeDeployServerDeployAction({
          actionName: "Deploy",
          input: buildOutput,
          deploymentGroup,
        });
      ],
    });
  }
}

Limitations

Currently, it is not possible to update the Deployment Group name after the deployment group is created.

Currently, it is not possible to create a Deployment Group for an EC2 or On-Premises deployment with the tag-based configuration.

Author

Władysław Czyżewski (https://otocolobus.com)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for aws-cdk-serverbluegreendeploymentgroup 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aws-cdk-serverbluegreendeploymentgroup 1.0.3
File Size Uploaded
aws-cdk-serverbluegreendeploymentgroup-1.0.3.tar.gz 39.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aws-cdk-serverbluegreendeploymentgroup 1.0.3
File Interpreter ABI Platform
aws_cdk_serverbluegreendeploymentgroup-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 77.5 kB

Release files / aws-cdk-serverbluegreendeploymentgroup-1.0.3.tar.gz

Download URL aws-cdk-serverbluegreendeploymentgroup-1.0.3.tar.gz
Size 39.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e411954a7bf49ae9be8844f74f84d4d4cc71082732a4f12ed49fdd956f6f52ce
BLAKE2b-256 checksum
How to use checksums
f300deb6a09007f4e6dbaf8823110c520249baab3720c7ffd74565df984d2715
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release files / aws_cdk_serverbluegreendeploymentgroup-1.0.3-py3-none-any.whl

Download URL aws_cdk_serverbluegreendeploymentgroup-1.0.3-py3-none-any.whl
Size 38.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e5b154a77409d74e0f49f928cc9ba2c14fed6e030d3f444400dd0d21b680b691
BLAKE2b-256 checksum
How to use checksums
f8f3c37a9c4a4aa50d8105e82ffc5e282857d5be0aa5fefae848c346f95b89fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page