Show / Hide Table of Contents

Namespace Amazon.CDK.AWS.CodeDeploy

AWS CodeDeploy Construct Library

--- cfn-resources: Stable cdk-constructs: 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:

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
using Amazon.CDK.AWS.CodeDeploy;

ServerApplication application = new ServerApplication(this, "CodeDeployApplication", new ServerApplicationProps {
    ApplicationName = "MyApplication"
});

To import an already existing Application:

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
var 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:

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
var deploymentGroup = new codedeploy.ServerDeploymentGroup(this, "CodeDeployDeploymentGroup", new Struct {
    Application = application,
    DeploymentGroupName = "MyDeploymentGroup",
    AutoScalingGroups = new [] { 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(new Struct {
        // any instance with tags satisfying
        // key1=v1 or key1=v2 or key2 (any value) or value v3 (any key)
        // will match this group
        Key1 = new [] { "v1", "v2" },
        Key2 = new [] {  },
         = new [] { "v3" }
    }),
    // adds on-premise instances matching tags
    OnPremiseInstanceTags = new codedeploy.InstanceTagSet(new Struct {
        Key1 = new [] { "v1", "v2" }
    }, new Struct {
        Key2 = new [] { "v3" }
    }),
    // CloudWatch alarms
    Alarms = new [] {
        new cloudwatch.Alarm() },
    // whether to ignore failure to fetch the status of alarms from CloudWatch
    // default: false
    IgnorePollAlarmsFailure = false,
    // auto-rollback configuration
    AutoRollback = new Struct {
        FailedDeployment = true, // default: true
        StoppedDeployment = true, // default: false
        DeploymentInAlarm = true
    }
});

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

To import an already existing Deployment Group:

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
var deploymentGroup = codedeploy.ServerDeploymentGroup.FromLambdaDeploymentGroupAttributes(this, "ExistingCodeDeployDeploymentGroup", new Struct {
    Application = 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:

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
using Amazon.CDK.AWS.ElasticLoadBalancing;

LoadBalancer elb = new LoadBalancer(this, "ELB", new LoadBalancerProps { });
elb.AddTarget();
elb.AddListener(new LoadBalancerListener { });

var deploymentGroup = new codedeploy.ServerDeploymentGroup(this, "DeploymentGroup", new Struct {
    LoadBalancer = codedeploy.LoadBalancer.Classic(elb)
});

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

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
using Amazon.CDK.AWS.ElasticLoadBalancingV2;

ApplicationLoadBalancer alb = new ApplicationLoadBalancer(this, "ALB", new ApplicationLoadBalancerProps { });
ApplicationListener listener = alb.AddListener("Listener", new BaseApplicationListenerProps { });
ApplicationTargetGroup targetGroup = listener.AddTargets("Fleet", new AddApplicationTargetsProps { });

var deploymentGroup = new codedeploy.ServerDeploymentGroup(this, "DeploymentGroup", new Struct {
    LoadBalancer = codedeploy.LoadBalancer.Application(targetGroup)
});

Deployment Configurations

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

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
var deploymentGroup = new codedeploy.ServerDeploymentGroup(this, "CodeDeployDeploymentGroup", new Struct {
    DeploymentConfig = codedeploy.ServerDeploymentConfig.ALL_AT_ONCE
});

The default Deployment Configuration is ServerDeploymentConfig.ONE_AT_A_TIME.

You can also create a custom Deployment Configuration:

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
var deploymentConfig = new codedeploy.ServerDeploymentConfig(this, "DeploymentConfiguration", new Struct {
    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:

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
var deploymentConfig = codedeploy.ServerDeploymentConfig.FromServerDeploymentConfigName(this, "ExistingDeploymentConfiguration", "MyExistingDeploymentConfiguration");

Lambda Applications

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

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
using Amazon.CDK.AWS.CodeDeploy;

LambdaApplication application = new LambdaApplication(this, "CodeDeployApplication", new LambdaApplicationProps {
    ApplicationName = "MyApplication"
});

To import an already existing Application:

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
var 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:

// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
using Amazon.CDK.AWS.CodeDeploy;
using Amazon.CDK.AWS.Lambda;

LambdaApplication myApplication = new LambdaApplication();
Function func = new Function();
Version version = func.AddVersion("1");
Alias version1Alias = new Alias(this, "alias", new AliasProps {
    AliasName = "prod",
    Version = version
});

LambdaDeploymentGroup deploymentGroup = new LambdaDeploymentGroup(stack, "BlueGreenDeployment", new LambdaDeploymentGroupProps {
    Application = myApplication, // optional property: one will be created for you if not provided
    Alias = version1Alias,
    DeploymentConfig = LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE
});

In order to deploy a new version of this function:

    Create a custom Deployment Config

    CodeDeploy for Lambda comes with built-in configurations for traffic shifting. If you want to specify your own strategy, you can do so with the CustomLambdaDeploymentConfig construct, letting you specify precisely how fast a new function version is deployed.

    // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
    var config = new codedeploy.CustomLambdaDeploymentConfig(stack, "CustomConfig", new Struct {
        Type = codedeploy.CustomLambdaDeploymentConfigType.CANARY,
        Interval = Duration.Minutes(1),
        Percentage = 5
    });
    var deploymentGroup = new codedeploy.LambdaDeploymentGroup(stack, "BlueGreenDeployment", new Struct {
        Application = application,
        Alias = alias,
        DeploymentConfig = config
    });

    You can specify a custom name for your deployment config, but if you do you will not be able to update the interval/percentage through CDK.

    // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
    var config = new codedeploy.CustomLambdaDeploymentConfig(stack, "CustomConfig", new Struct {
        Type = codedeploy.CustomLambdaDeploymentConfigType.CANARY,
        Interval = Duration.Minutes(1),
        Percentage = 5,
        DeploymentConfigName = "MyDeploymentConfig"
    });

    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:

    // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
    var deploymentGroup = new codedeploy.LambdaDeploymentGroup(stack, "BlueGreenDeployment", new Struct {
        Alias = alias,
        DeploymentConfig = codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,
        Alarms = new [] {
            // pass some alarms when constructing the deployment group
            new cloudwatch.Alarm(stack, "Errors", new Struct {
                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", new Struct {
        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.

    // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
    var warmUpUserCache = new lambda.Function();
    var endToEndValidation = new lambda.Function();
    
    // pass a hook whe creating the deployment group
    var deploymentGroup = new codedeploy.LambdaDeploymentGroup(stack, "BlueGreenDeployment", new Struct {
        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:

    // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
    var deploymentGroup = codedeploy.LambdaDeploymentGroup.Import(this, "ExistingCodeDeployDeploymentGroup", new Struct {
        Application = application,
        DeploymentGroupName = "MyExistingDeploymentGroup"
    });

    Classes

    AutoRollbackConfig

    The configuration for automatically rolling back deployments in a given Deployment Group.

    CfnApplication

    A CloudFormation AWS::CodeDeploy::Application.

    CfnApplicationProps

    Properties for defining a AWS::CodeDeploy::Application.

    CfnDeploymentConfig

    A CloudFormation AWS::CodeDeploy::DeploymentConfig.

    CfnDeploymentConfig.MinimumHealthyHostsProperty
    CfnDeploymentConfigProps

    Properties for defining a AWS::CodeDeploy::DeploymentConfig.

    CfnDeploymentGroup

    A CloudFormation AWS::CodeDeploy::DeploymentGroup.

    CfnDeploymentGroup.AlarmConfigurationProperty
    CfnDeploymentGroup.AlarmProperty
    CfnDeploymentGroup.AutoRollbackConfigurationProperty
    CfnDeploymentGroup.DeploymentProperty
    CfnDeploymentGroup.DeploymentStyleProperty
    CfnDeploymentGroup.EC2TagFilterProperty
    CfnDeploymentGroup.EC2TagSetListObjectProperty
    CfnDeploymentGroup.EC2TagSetProperty
    CfnDeploymentGroup.ELBInfoProperty
    CfnDeploymentGroup.GitHubLocationProperty
    CfnDeploymentGroup.LoadBalancerInfoProperty
    CfnDeploymentGroup.OnPremisesTagSetListObjectProperty
    CfnDeploymentGroup.OnPremisesTagSetProperty
    CfnDeploymentGroup.RevisionLocationProperty
    CfnDeploymentGroup.S3LocationProperty
    CfnDeploymentGroup.TagFilterProperty
    CfnDeploymentGroup.TargetGroupInfoProperty
    CfnDeploymentGroup.TriggerConfigProperty
    CfnDeploymentGroupProps

    Properties for defining a AWS::CodeDeploy::DeploymentGroup.

    CustomLambdaDeploymentConfig

    A custom Deployment Configuration for a Lambda Deployment Group.

    CustomLambdaDeploymentConfigProps

    Properties of a reference to a CodeDeploy Lambda Deployment Configuration.

    CustomLambdaDeploymentConfigType

    Lambda Deployment config type.

    EcsApplication

    A CodeDeploy Application that deploys to an Amazon ECS service.

    EcsApplicationProps

    Construction properties for {@link EcsApplication}.

    EcsDeploymentConfig

    A custom Deployment Configuration for an ECS Deployment Group.

    EcsDeploymentGroup

    Note: This class currently stands as a namespaced container for importing an ECS Deployment Group defined outside the CDK app until CloudFormation supports provisioning ECS Deployment Groups.

    EcsDeploymentGroupAttributes

    Properties of a reference to a CodeDeploy ECS Deployment Group.

    InstanceTagSet

    Represents a set of instance tag groups.

    LambdaApplication

    A CodeDeploy Application that deploys to an AWS Lambda function.

    LambdaApplicationProps

    Construction properties for {@link LambdaApplication}.

    LambdaDeploymentConfig

    A custom Deployment Configuration for a Lambda Deployment Group.

    LambdaDeploymentConfigImportProps

    Properties of a reference to a CodeDeploy Lambda Deployment Configuration.

    LambdaDeploymentGroup
    LambdaDeploymentGroupAttributes

    Properties of a reference to a CodeDeploy Lambda Deployment Group.

    LambdaDeploymentGroupProps

    Construction properties for {@link LambdaDeploymentGroup}.

    LoadBalancer

    An interface of an abstract load balancer, as needed by CodeDeploy.

    LoadBalancerGeneration

    The generations of AWS load balancing solutions.

    MinimumHealthyHosts

    Minimum number of healthy hosts for a server deployment.

    ServerApplication

    A CodeDeploy Application that deploys to EC2/on-premise instances.

    ServerApplicationProps

    Construction properties for {@link ServerApplication}.

    ServerDeploymentConfig

    A custom Deployment Configuration for an EC2/on-premise Deployment Group.

    ServerDeploymentConfigProps

    Construction properties of {@link ServerDeploymentConfig}.

    ServerDeploymentGroup

    A CodeDeploy Deployment Group that deploys to EC2/on-premise instances.

    ServerDeploymentGroupAttributes

    Properties of a reference to a CodeDeploy EC2/on-premise Deployment Group.

    ServerDeploymentGroupProps

    Construction properties for {@link ServerDeploymentGroup}.

    Interfaces

    CfnDeploymentConfig.IMinimumHealthyHostsProperty
    CfnDeploymentGroup.IAlarmConfigurationProperty
    CfnDeploymentGroup.IAlarmProperty
    CfnDeploymentGroup.IAutoRollbackConfigurationProperty
    CfnDeploymentGroup.IDeploymentProperty
    CfnDeploymentGroup.IDeploymentStyleProperty
    CfnDeploymentGroup.IEC2TagFilterProperty
    CfnDeploymentGroup.IEC2TagSetListObjectProperty
    CfnDeploymentGroup.IEC2TagSetProperty
    CfnDeploymentGroup.IELBInfoProperty
    CfnDeploymentGroup.IGitHubLocationProperty
    CfnDeploymentGroup.ILoadBalancerInfoProperty
    CfnDeploymentGroup.IOnPremisesTagSetListObjectProperty
    CfnDeploymentGroup.IOnPremisesTagSetProperty
    CfnDeploymentGroup.IRevisionLocationProperty
    CfnDeploymentGroup.IS3LocationProperty
    CfnDeploymentGroup.ITagFilterProperty
    CfnDeploymentGroup.ITargetGroupInfoProperty
    CfnDeploymentGroup.ITriggerConfigProperty
    IAutoRollbackConfig

    The configuration for automatically rolling back deployments in a given Deployment Group.

    ICfnApplicationProps

    Properties for defining a AWS::CodeDeploy::Application.

    ICfnDeploymentConfigProps

    Properties for defining a AWS::CodeDeploy::DeploymentConfig.

    ICfnDeploymentGroupProps

    Properties for defining a AWS::CodeDeploy::DeploymentGroup.

    ICustomLambdaDeploymentConfigProps

    Properties of a reference to a CodeDeploy Lambda Deployment Configuration.

    IEcsApplication

    Represents a reference to a CodeDeploy Application deploying to Amazon ECS.

    IEcsApplicationProps

    Construction properties for {@link EcsApplication}.

    IEcsDeploymentConfig

    The Deployment Configuration of an ECS Deployment Group.

    IEcsDeploymentGroup

    Interface for an ECS deployment group.

    IEcsDeploymentGroupAttributes

    Properties of a reference to a CodeDeploy ECS Deployment Group.

    ILambdaApplication

    Represents a reference to a CodeDeploy Application deploying to AWS Lambda.

    ILambdaApplicationProps

    Construction properties for {@link LambdaApplication}.

    ILambdaDeploymentConfig

    The Deployment Configuration of a Lambda Deployment Group.

    ILambdaDeploymentConfigImportProps

    Properties of a reference to a CodeDeploy Lambda Deployment Configuration.

    ILambdaDeploymentGroup

    Interface for a Lambda deployment groups.

    ILambdaDeploymentGroupAttributes

    Properties of a reference to a CodeDeploy Lambda Deployment Group.

    ILambdaDeploymentGroupProps

    Construction properties for {@link LambdaDeploymentGroup}.

    IServerApplication

    Represents a reference to a CodeDeploy Application deploying to EC2/on-premise instances.

    IServerApplicationProps

    Construction properties for {@link ServerApplication}.

    IServerDeploymentConfig

    The Deployment Configuration of an EC2/on-premise Deployment Group.

    IServerDeploymentConfigProps

    Construction properties of {@link ServerDeploymentConfig}.

    IServerDeploymentGroup
    IServerDeploymentGroupAttributes

    Properties of a reference to a CodeDeploy EC2/on-premise Deployment Group.

    IServerDeploymentGroupProps

    Construction properties for {@link ServerDeploymentGroup}.

    Back to top Generated by DocFX