Namespace Amazon.CDK.AWS.CodeDeploy
AWS CodeDeploy Construct Library
---AWS CDK v1 has reached End-of-Support on 2023-06-01.
This package is no longer being updated, and users should migrate to AWS CDK v2.
For more information on how to migrate, see the Migrating to AWS CDK v2 guide.
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:
var application = new ServerApplication(this, "CodeDeployApplication", new ServerApplicationProps {
ApplicationName = "MyApplication"
});
To import an already existing Application:
var application = ServerApplication.FromServerApplicationName(this, "ExistingCodeDeployApplication", "MyExistingApplication");
EC2/on-premise Deployment Groups
To create a new CodeDeploy Deployment Group that deploys to EC2/on-premise instances:
using Amazon.CDK.AWS.AutoScaling;
using Amazon.CDK.AWS.CloudWatch;
ServerApplication application;
AutoScalingGroup asg;
Alarm alarm;
var deploymentGroup = new ServerDeploymentGroup(this, "CodeDeployDeploymentGroup", new ServerDeploymentGroupProps {
Application = application,
DeploymentGroupName = "MyDeploymentGroup",
AutoScalingGroups = new [] { asg },
// 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 InstanceTagSet(new Dictionary<string, string[]> {
// 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 InstanceTagSet(new Dictionary<string, string[]> {
{ "key1", new [] { "v1", "v2" } }
}, new Dictionary<string, string[]> {
{ "key2", new [] { "v3" } }
}),
// CloudWatch alarms
Alarms = new [] { alarm },
// whether to ignore failure to fetch the status of alarms from CloudWatch
// default: false
IgnorePollAlarmsFailure = false,
// auto-rollback configuration
AutoRollback = new AutoRollbackConfig {
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:
ServerApplication application;
var deploymentGroup = ServerDeploymentGroup.FromServerDeploymentGroupAttributes(this, "ExistingCodeDeployDeploymentGroup", new ServerDeploymentGroupAttributes {
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:
using Amazon.CDK.AWS.ElasticLoadBalancing;
LoadBalancer lb;
lb.AddListener(new LoadBalancerListener {
ExternalPort = 80
});
var deploymentGroup = new ServerDeploymentGroup(this, "DeploymentGroup", new ServerDeploymentGroupProps {
LoadBalancer = LoadBalancer.Classic(lb)
});
With Application Load Balancer or Network Load Balancer, you provide a Target Group as the load balancer:
using Amazon.CDK.AWS.ElasticLoadBalancingV2;
ApplicationLoadBalancer alb;
var listener = alb.AddListener("Listener", new BaseApplicationListenerProps { Port = 80 });
var targetGroup = listener.AddTargets("Fleet", new AddApplicationTargetsProps { Port = 80 });
var deploymentGroup = new ServerDeploymentGroup(this, "DeploymentGroup", new ServerDeploymentGroupProps {
LoadBalancer = LoadBalancer.Application(targetGroup)
});
Deployment Configurations
You can also pass a Deployment Configuration when creating the Deployment Group:
var deploymentGroup = new ServerDeploymentGroup(this, "CodeDeployDeploymentGroup", new ServerDeploymentGroupProps {
DeploymentConfig = ServerDeploymentConfig.ALL_AT_ONCE
});
The default Deployment Configuration is ServerDeploymentConfig.ONE_AT_A_TIME
.
You can also create a custom Deployment Configuration:
var deploymentConfig = new ServerDeploymentConfig(this, "DeploymentConfiguration", new ServerDeploymentConfigProps {
DeploymentConfigName = "MyDeploymentConfiguration", // optional property
// one of these is required, but both cannot be specified at the same time
MinimumHealthyHosts = MinimumHealthyHosts.Count(2)
});
Or import an existing one:
var deploymentConfig = ServerDeploymentConfig.FromServerDeploymentConfigName(this, "ExistingDeploymentConfiguration", "MyExistingDeploymentConfiguration");
Lambda Applications
To create a new CodeDeploy Application that deploys to a Lambda function:
var application = new LambdaApplication(this, "CodeDeployApplication", new LambdaApplicationProps {
ApplicationName = "MyApplication"
});
To import an already existing Application:
var application = 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:
LambdaApplication myApplication;
Function func;
var version = func.CurrentVersion;
var version1Alias = new Alias(this, "alias", new AliasProps {
AliasName = "prod",
Version = version
});
var deploymentGroup = new LambdaDeploymentGroup(this, "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.
LambdaApplication application;
Alias alias;
var config = new CustomLambdaDeploymentConfig(this, "CustomConfig", new CustomLambdaDeploymentConfigProps {
Type = CustomLambdaDeploymentConfigType.CANARY,
Interval = Duration.Minutes(1),
Percentage = 5
});
var deploymentGroup = new LambdaDeploymentGroup(this, "BlueGreenDeployment", new LambdaDeploymentGroupProps {
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.
var config = new CustomLambdaDeploymentConfig(this, "CustomConfig", new CustomLambdaDeploymentConfigProps {
Type = 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:
using Amazon.CDK.AWS.CloudWatch;
Alias alias;
// or add alarms to an existing group
Alias blueGreenAlias;
var alarm = new Alarm(this, "Errors", new AlarmProps {
ComparisonOperator = ComparisonOperator.GREATER_THAN_THRESHOLD,
Threshold = 1,
EvaluationPeriods = 1,
Metric = alias.MetricErrors()
});
var deploymentGroup = new LambdaDeploymentGroup(this, "BlueGreenDeployment", new LambdaDeploymentGroupProps {
Alias = alias,
DeploymentConfig = LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,
Alarms = new [] { alarm }
});
deploymentGroup.AddAlarm(new Alarm(this, "BlueGreenErrors", new AlarmProps {
ComparisonOperator = 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.
Function warmUpUserCache;
Function endToEndValidation;
Alias alias;
// pass a hook whe creating the deployment group
var deploymentGroup = new LambdaDeploymentGroup(this, "BlueGreenDeployment", new LambdaDeploymentGroupProps {
Alias = alias,
DeploymentConfig = LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,
PreHook = warmUpUserCache
});
// or configure one on an existing deployment group
deploymentGroup.AddPostHook(endToEndValidation);
Import an existing Deployment Group
To import an already existing Deployment Group:
LambdaApplication application;
var deploymentGroup = LambdaDeploymentGroup.FromLambdaDeploymentGroupAttributes(this, "ExistingCodeDeployDeploymentGroup", new LambdaDeploymentGroupAttributes {
Application = application,
DeploymentGroupName = "MyExistingDeploymentGroup"
});
Classes
Auto |
The configuration for automatically rolling back deployments in a given Deployment Group. |
Cfn |
A CloudFormation |
Cfn |
Properties for defining a |
Cfn |
A CloudFormation |
Cfn |
|
Cfn |
A configuration that shifts traffic from one version of a Lambda function or Amazon ECS task set to another in two increments. |
Cfn |
A configuration that shifts traffic from one version of a Lambda function or ECS task set to another in equal increments, with an equal number of minutes between each increment. |
Cfn |
The configuration that specifies how traffic is shifted from one version of a Lambda function to another version during an AWS Lambda deployment, or from one Amazon ECS task set to another during an Amazon ECS deployment. |
Cfn |
Properties for defining a |
Cfn |
A CloudFormation |
Cfn |
The |
Cfn |
The |
Cfn |
The |
Cfn |
Information about blue/green deployment options for a deployment group. |
Cfn |
Information about whether instances in the original environment are terminated when a blue/green deployment is successful. |
Cfn |
|
Cfn |
Information about how traffic is rerouted to instances in a replacement environment in a blue/green deployment. |
Cfn |
Information about the type of deployment, either in-place or blue/green, you want to run and whether to route deployment traffic behind a load balancer. |
Cfn |
Information about an Amazon EC2 tag filter. |
Cfn |
The |
Cfn |
The |
Cfn |
Contains the service and cluster names used to identify an Amazon ECS deployment's target. |
Cfn |
The |
Cfn |
|
Cfn |
Information about the instances that belong to the replacement environment in a blue/green deployment. |
Cfn |
The |
Cfn |
The |
Cfn |
The |
Cfn |
|
Cfn |
|
Cfn |
|
Cfn |
The |
Cfn |
|
Cfn |
|
Cfn |
Information about notification triggers for the deployment group. |
Cfn |
Properties for defining a |
Custom |
A custom Deployment Configuration for a Lambda Deployment Group. |
Custom |
Properties of a reference to a CodeDeploy Lambda Deployment Configuration. |
Custom |
Lambda Deployment config type. |
Ecs |
A CodeDeploy Application that deploys to an Amazon ECS service. |
Ecs |
Construction properties for {@link EcsApplication}. |
Ecs |
A custom Deployment Configuration for an ECS Deployment Group. |
Ecs |
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. |
Ecs |
Properties of a reference to a CodeDeploy ECS Deployment Group. |
Instance |
Represents a set of instance tag groups. |
Lambda |
A CodeDeploy Application that deploys to an AWS Lambda function. |
Lambda |
Construction properties for {@link LambdaApplication}. |
Lambda |
A custom Deployment Configuration for a Lambda Deployment Group. |
Lambda |
Properties of a reference to a CodeDeploy Lambda Deployment Configuration. |
Lambda |
|
Lambda |
Properties of a reference to a CodeDeploy Lambda Deployment Group. |
Lambda |
Construction properties for {@link LambdaDeploymentGroup}. |
Load |
An interface of an abstract load balancer, as needed by CodeDeploy. |
Load |
The generations of AWS load balancing solutions. |
Minimum |
Minimum number of healthy hosts for a server deployment. |
Server |
A CodeDeploy Application that deploys to EC2/on-premise instances. |
Server |
Construction properties for {@link ServerApplication}. |
Server |
A custom Deployment Configuration for an EC2/on-premise Deployment Group. |
Server |
Construction properties of {@link ServerDeploymentConfig}. |
Server |
A CodeDeploy Deployment Group that deploys to EC2/on-premise instances. |
Server |
Properties of a reference to a CodeDeploy EC2/on-premise Deployment Group. |
Server |
Construction properties for {@link ServerDeploymentGroup}. |
Interfaces
Cfn |
|
Cfn |
A configuration that shifts traffic from one version of a Lambda function or Amazon ECS task set to another in two increments. |
Cfn |
A configuration that shifts traffic from one version of a Lambda function or ECS task set to another in equal increments, with an equal number of minutes between each increment. |
Cfn |
The configuration that specifies how traffic is shifted from one version of a Lambda function to another version during an AWS Lambda deployment, or from one Amazon ECS task set to another during an Amazon ECS deployment. |
Cfn |
The |
Cfn |
The |
Cfn |
The |
Cfn |
Information about blue/green deployment options for a deployment group. |
Cfn |
Information about whether instances in the original environment are terminated when a blue/green deployment is successful. |
Cfn |
|
Cfn |
Information about how traffic is rerouted to instances in a replacement environment in a blue/green deployment. |
Cfn |
Information about the type of deployment, either in-place or blue/green, you want to run and whether to route deployment traffic behind a load balancer. |
Cfn |
Information about an Amazon EC2 tag filter. |
Cfn |
The |
Cfn |
The |
Cfn |
Contains the service and cluster names used to identify an Amazon ECS deployment's target. |
Cfn |
The |
Cfn |
|
Cfn |
Information about the instances that belong to the replacement environment in a blue/green deployment. |
Cfn |
The |
Cfn |
The |
Cfn |
The |
Cfn |
|
Cfn |
|
Cfn |
|
Cfn |
The |
Cfn |
|
Cfn |
|
Cfn |
Information about notification triggers for the deployment group. |
IAuto |
The configuration for automatically rolling back deployments in a given Deployment Group. |
ICfn |
Properties for defining a |
ICfn |
Properties for defining a |
ICfn |
Properties for defining a |
ICustom |
Properties of a reference to a CodeDeploy Lambda Deployment Configuration. |
IEcs |
Represents a reference to a CodeDeploy Application deploying to Amazon ECS. |
IEcs |
Construction properties for {@link EcsApplication}. |
IEcs |
The Deployment Configuration of an ECS Deployment Group. |
IEcs |
Interface for an ECS deployment group. |
IEcs |
Properties of a reference to a CodeDeploy ECS Deployment Group. |
ILambda |
Represents a reference to a CodeDeploy Application deploying to AWS Lambda. |
ILambda |
Construction properties for {@link LambdaApplication}. |
ILambda |
The Deployment Configuration of a Lambda Deployment Group. |
ILambda |
Properties of a reference to a CodeDeploy Lambda Deployment Configuration. |
ILambda |
Interface for a Lambda deployment groups. |
ILambda |
Properties of a reference to a CodeDeploy Lambda Deployment Group. |
ILambda |
Construction properties for {@link LambdaDeploymentGroup}. |
IServer |
Represents a reference to a CodeDeploy Application deploying to EC2/on-premise instances. |
IServer |
Construction properties for {@link ServerApplication}. |
IServer |
The Deployment Configuration of an EC2/on-premise Deployment Group. |
IServer |
Construction properties of {@link ServerDeploymentConfig}. |
IServer |
|
IServer |
Properties of a reference to a CodeDeploy EC2/on-premise Deployment Group. |
IServer |
Construction properties for {@link ServerDeploymentGroup}. |