The CDK Construct Library for AWS CodeDeploy
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',
});
To import an already existing Application:
const application = codedeploy.ServerApplicationRef.import(this, 'ExistingCodeDeployApplication', {
applicationName: 'MyExistingApplication',
});
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],
installAgent: true,
ec2InstanceTags: new codedeploy.InstanceTagSet(
{
'key1': ['v1', 'v2'],
'key2': [],
'': ['v3'],
},
),
onPremiseInstanceTags: new codedeploy.InstanceTagSet(
{
'key1': ['v1', 'v2'],
},
{
'key2': ['v3'],
},
),
alarms: [
new cloudwatch.Alarm(),
],
ignorePollAlarmsFailure: false,
autoRollback: {
failedDeployment: true,
stoppedDeployment: true,
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:
const deploymentGroup = codedeploy.ServerDeploymentGroupRef.import(this, 'ExistingCodeDeployDeploymentGroup', {
application,
deploymentGroupName: 'MyExistingDeploymentGroup',
});
Load balancers
You can specify a load balancer
with the loadBalancer
property when creating a Deployment Group.
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: 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: 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.AllAtOnce,
});
The default Deployment Configuration is ServerDeploymentConfig.OneAtATime
.
You can also create a custom Deployment Configuration:
const deploymentConfig = new codedeploy.ServerDeploymentConfig(this, 'DeploymentConfiguration', {
deploymentConfigName: 'MyDeploymentConfiguration',
minHealthyHostCount: 2,
minHealthyHostPercentage: 75,
});
Or import an existing one:
const deploymentConfig = codedeploy.ServerDeploymentConfigRef.import(this, 'ExistingDeploymentConfiguration', {
deploymentConfigName: 'MyExistingDeploymentConfiguration',
});
Use in CodePipeline
This module also contains an Action that allows you to use CodeDeploy with AWS CodePipeline.
Example:
import codepipeline = require('@aws-cdk/aws-codepipeline');
const pipeline = new codepipeline.Pipeline(this, 'MyPipeline', {
pipelineName: 'MyPipeline',
});
const deployStage = pipeline.addStage('Deploy');
new codedeploy.PipelineDeployAction(this, 'CodeDeploy', {
stage: deployStage,
deploymentGroup,
});
You can also add the Deployment Group to the Pipeline directly:
deploymentGroup.addToPipeline(deployStage, 'CodeDeploy');