Skip to main content

The CDK Construct Library for AWS::CodeDeploy

Project description

AWS CodeDeploy Construct Library


Stability: Stable


AWS CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances, on-premises instances, serverless Lambda functions, or Amazon ECS services.

The CDK currently supports Amazon EC2, on-premise and AWS Lambda applications.

EC2/on-premise Applications

To create a new CodeDeploy Application that deploys to EC2/on-premise instances:

import codedeploy = require('@aws-cdk/aws-codedeploy');

const application = new codedeploy.ServerApplication(this, 'CodeDeployApplication', {
    applicationName: 'MyApplication', // optional property
});

To import an already existing Application:

const application = codedeploy.ServerApplication.fromServerApplicationName(
  this, 'ExistingCodeDeployApplication', 'MyExistingApplication'
);

EC2/on-premise Deployment Groups

To create a new CodeDeploy Deployment Group that deploys to EC2/on-premise instances:

const deploymentGroup = new codedeploy.ServerDeploymentGroup(this, 'CodeDeployDeploymentGroup', {
    application,
    deploymentGroupName: 'MyDeploymentGroup',
    autoScalingGroups: [asg1, asg2],
    // adds User Data that installs the CodeDeploy agent on your auto-scaling groups hosts
    // default: true
    installAgent: true,
    // adds EC2 instances matching tags
    ec2InstanceTags: new codedeploy.InstanceTagSet(
        {
            // any instance with tags satisfying
            // key1=v1 or key1=v2 or key2 (any value) or value v3 (any key)
            // will match this group
            'key1': ['v1', 'v2'],
            'key2': [],
            '': ['v3'],
        },
    ),
    // adds on-premise instances matching tags
    onPremiseInstanceTags: new codedeploy.InstanceTagSet(
        // only instances with tags (key1=v1 or key1=v2) AND key2=v3 will match this set
        {
            'key1': ['v1', 'v2'],
        },
        {
            'key2': ['v3'],
        },
    ),
    // CloudWatch alarms
    alarms: [
        new cloudwatch.Alarm(/* ... */),
    ],
    // whether to ignore failure to fetch the status of alarms from CloudWatch
    // default: false
    ignorePollAlarmsFailure: false,
    // auto-rollback configuration
    autoRollback: {
        failedDeployment: true, // default: true
        stoppedDeployment: true, // default: false
        deploymentInAlarm: true, // default: true if you provided any alarms, false otherwise
    },
});

All properties are optional - if you don't provide an Application, one will be automatically created.

To import an already existing Deployment Group:

const deploymentGroup = codedeploy.ServerDeploymentGroup.fromLambdaDeploymentGroupAttributes(this, 'ExistingCodeDeployDeploymentGroup', {
    application,
    deploymentGroupName: 'MyExistingDeploymentGroup',
});

Load balancers

You can specify a load balancer with the loadBalancer property when creating a Deployment Group.

LoadBalancer is an abstract class with static factory methods that allow you to create instances of it from various sources.

With Classic Elastic Load Balancer, you provide it directly:

import lb = require('@aws-cdk/aws-elasticloadbalancing');

const elb = new lb.LoadBalancer(this, 'ELB', {
  // ...
});
elb.addTarget(/* ... */);
elb.addListener({
  // ...
});

const deploymentGroup = new codedeploy.ServerDeploymentGroup(this, 'DeploymentGroup', {
  loadBalancer: codedeploy.LoadBalancer.classic(elb),
});

With Application Load Balancer or Network Load Balancer, you provide a Target Group as the load balancer:

import lbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');

const alb = new lbv2.ApplicationLoadBalancer(this, 'ALB', {
  // ...
});
const listener = alb.addListener('Listener', {
  // ...
});
const targetGroup = listener.addTargets('Fleet', {
  // ...
});

const deploymentGroup = new codedeploy.ServerDeploymentGroup(this, 'DeploymentGroup', {
  loadBalancer: codedeploy.LoadBalancer.application(targetGroup),
});

Deployment Configurations

You can also pass a Deployment Configuration when creating the Deployment Group:

const deploymentGroup = new codedeploy.ServerDeploymentGroup(this, 'CodeDeployDeploymentGroup', {
    deploymentConfig: codedeploy.ServerDeploymentConfig.ALL_AT_ONCE,
});

The default Deployment Configuration is ServerDeploymentConfig.ONE_AT_A_TIME.

You can also create a custom Deployment Configuration:

const deploymentConfig = new codedeploy.ServerDeploymentConfig(this, 'DeploymentConfiguration', {
    deploymentConfigName: 'MyDeploymentConfiguration', // optional property
    // one of these is required, but both cannot be specified at the same time
    minHealthyHostCount: 2,
    minHealthyHostPercentage: 75,
});

Or import an existing one:

const deploymentConfig = codedeploy.ServerDeploymentConfig.fromServerDeploymentConfigName(
  this, 'ExistingDeploymentConfiguration', 'MyExistingDeploymentConfiguration'
);

Lambda Applications

To create a new CodeDeploy Application that deploys to a Lambda function:

import codedeploy = require('@aws-cdk/aws-codedeploy');

const application = new codedeploy.LambdaApplication(this, 'CodeDeployApplication', {
    applicationName: 'MyApplication', // optional property
});

To import an already existing Application:

const application = codedeploy.LambdaApplication.fromLambdaApplicationName(
  this, 'ExistingCodeDeployApplication', 'MyExistingApplication'
);

Lambda Deployment Groups

To enable traffic shifting deployments for Lambda functions, CodeDeploy uses Lambda Aliases, which can balance incoming traffic between two different versions of your function. Before deployment, the alias sends 100% of invokes to the version used in production. When you publish a new version of the function to your stack, CodeDeploy will send a small percentage of traffic to the new version, monitor, and validate before shifting 100% of traffic to the new version.

To create a new CodeDeploy Deployment Group that deploys to a Lambda function:

import codedeploy = require('@aws-cdk/aws-codedeploy');
import lambda = require('@aws-cdk/aws-lambda');

const myApplication = new codedeploy.LambdaApplication(..);
const func = new lambda.Function(..);
const version = func.addVersion('1');
const version1Alias = new lambda.Alias(this, 'alias', {
  aliasName: 'prod',
  version
});

const deploymentGroup = new codedeploy.LambdaDeploymentGroup(stack, 'BlueGreenDeployment', {
  application: myApplication, // optional property: one will be created for you if not provided
  alias: version1Alias,
  deploymentConfig: codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,
});

In order to deploy a new version of this function:

  1. Increment the version, e.g. const version = func.addVersion('2').
  2. Re-deploy the stack (this will trigger a deployment).
  3. Monitor the CodeDeploy deployment as traffic shifts between the versions.

Rollbacks and Alarms

CodeDeploy will roll back if the deployment fails. You can optionally trigger a rollback when one or more alarms are in a failed state:

const deploymentGroup = new codedeploy.LambdaDeploymentGroup(stack, 'BlueGreenDeployment', {
  alias,
  deploymentConfig: codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,
  alarms: [
    // pass some alarms when constructing the deployment group
    new cloudwatch.Alarm(stack, 'Errors', {
      comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
      threshold: 1,
      evaluationPeriods: 1,
      metric: alias.metricErrors()
    })
  ]
});

// or add alarms to an existing group
deploymentGroup.addAlarm(new cloudwatch.Alarm(stack, 'BlueGreenErrors', {
  comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
  threshold: 1,
  evaluationPeriods: 1,
  metric: blueGreenAlias.metricErrors()
}));

Pre and Post Hooks

CodeDeploy allows you to run an arbitrary Lambda function before traffic shifting actually starts (PreTraffic Hook) and after it completes (PostTraffic Hook). With either hook, you have the opportunity to run logic that determines whether the deployment must succeed or fail. For example, with PreTraffic hook you could run integration tests against the newly created Lambda version (but not serving traffic). With PostTraffic hook, you could run end-to-end validation checks.

const warmUpUserCache = new lambda.Function(..);
const endToEndValidation = new lambda.Function(..);

// pass a hook whe creating the deployment group
const deploymentGroup = new codedeploy.LambdaDeploymentGroup(stack, 'BlueGreenDeployment', {
  alias: alias,
  deploymentConfig: codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,
  preHook: warmUpUserCache,
});

// or configure one on an existing deployment group
deploymentGroup.onPostHook(endToEndValidation);

Import an existing Deployment Group

To import an already existing Deployment Group:

const deploymentGroup = codedeploy.LambdaDeploymentGroup.import(this, 'ExistingCodeDeployDeploymentGroup', {
    application,
    deploymentGroupName: 'MyExistingDeploymentGroup',
});

Project details


Release history Release notifications | RSS feed

This version

1.3.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws-cdk.aws-codedeploy-1.3.0.tar.gz (132.2 kB view details)

Uploaded Source

Built Distribution

aws_cdk.aws_codedeploy-1.3.0-py3-none-any.whl (130.3 kB view details)

Uploaded Python 3

File details

Details for the file aws-cdk.aws-codedeploy-1.3.0.tar.gz.

File metadata

  • Download URL: aws-cdk.aws-codedeploy-1.3.0.tar.gz
  • Upload date:
  • Size: 132.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.5

File hashes

Hashes for aws-cdk.aws-codedeploy-1.3.0.tar.gz
Algorithm Hash digest
SHA256 0d676afe6256bcf0d4eb8f2efbdbb466f1890b3e780bb5f437843ca252c79588
MD5 3812170f27e85ce6f5cffa76a9a6fc83
BLAKE2b-256 6c0d80c4f0869e676135d2a2eb742deb01700cac125b42f9a5a6506f0c8056f2

See more details on using hashes here.

File details

Details for the file aws_cdk.aws_codedeploy-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: aws_cdk.aws_codedeploy-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 130.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.5

File hashes

Hashes for aws_cdk.aws_codedeploy-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f509c1743357e52e396c5f4fe28a987eeb7fd1240465e450776240ac252eb0d
MD5 05694c2c513a3a5761ed297a5ebc32d9
BLAKE2b-256 6ef1f7e61f19d2658659f1b91e1876ac2639469adfb98364ca7ecc565d78b327

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page