Package software.amazon.awscdk.services.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:
ServerApplication application = ServerApplication.Builder.create(this, "CodeDeployApplication")
.applicationName("MyApplication")
.build();
To import an already existing Application:
IServerApplication 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:
import software.amazon.awscdk.services.autoscaling.*;
import software.amazon.awscdk.services.cloudwatch.*;
ServerApplication application;
AutoScalingGroup asg;
Alarm alarm;
ServerDeploymentGroup deploymentGroup = ServerDeploymentGroup.Builder.create(this, "CodeDeployDeploymentGroup")
.application(application)
.deploymentGroupName("MyDeploymentGroup")
.autoScalingGroups(List.of(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(Map.of(
// any instance with tags satisfying
// key1=v1 or key1=v2 or key2 (any value) or value v3 (any key)
// will match this group
"key1", List.of("v1", "v2"),
"key2", List.of(),
"", List.of("v3"))))
// adds on-premise instances matching tags
.onPremiseInstanceTags(new InstanceTagSet(Map.of(
"key1", List.of("v1", "v2")), Map.of(
"key2", List.of("v3"))))
// CloudWatch alarms
.alarms(List.of(alarm))
// whether to ignore failure to fetch the status of alarms from CloudWatch
// default: false
.ignorePollAlarmsFailure(false)
// auto-rollback configuration
.autoRollback(AutoRollbackConfig.builder()
.failedDeployment(true) // default: true
.stoppedDeployment(true) // default: false
.deploymentInAlarm(true)
.build())
.build();
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;
IServerDeploymentGroup deploymentGroup = ServerDeploymentGroup.fromServerDeploymentGroupAttributes(this, "ExistingCodeDeployDeploymentGroup", ServerDeploymentGroupAttributes.builder()
.application(application)
.deploymentGroupName("MyExistingDeploymentGroup")
.build());
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 software.amazon.awscdk.services.elasticloadbalancing.*;
LoadBalancer lb;
lb.addListener(LoadBalancerListener.builder()
.externalPort(80)
.build());
ServerDeploymentGroup deploymentGroup = ServerDeploymentGroup.Builder.create(this, "DeploymentGroup")
.loadBalancer(LoadBalancer.classic(lb))
.build();
With Application Load Balancer or Network Load Balancer, you provide a Target Group as the load balancer:
import software.amazon.awscdk.services.elasticloadbalancingv2.*;
ApplicationLoadBalancer alb;
ApplicationListener listener = alb.addListener("Listener", BaseApplicationListenerProps.builder().port(80).build());
ApplicationTargetGroup targetGroup = listener.addTargets("Fleet", AddApplicationTargetsProps.builder().port(80).build());
ServerDeploymentGroup deploymentGroup = ServerDeploymentGroup.Builder.create(this, "DeploymentGroup")
.loadBalancer(LoadBalancer.application(targetGroup))
.build();
Deployment Configurations
You can also pass a Deployment Configuration when creating the Deployment Group:
ServerDeploymentGroup deploymentGroup = ServerDeploymentGroup.Builder.create(this, "CodeDeployDeploymentGroup")
.deploymentConfig(ServerDeploymentConfig.ALL_AT_ONCE)
.build();
The default Deployment Configuration is ServerDeploymentConfig.ONE_AT_A_TIME.
You can also create a custom Deployment Configuration:
ServerDeploymentConfig deploymentConfig = ServerDeploymentConfig.Builder.create(this, "DeploymentConfiguration")
.deploymentConfigName("MyDeploymentConfiguration") // optional property
// one of these is required, but both cannot be specified at the same time
.minimumHealthyHosts(MinimumHealthyHosts.count(2))
.build();
Or import an existing one:
IServerDeploymentConfig deploymentConfig = ServerDeploymentConfig.fromServerDeploymentConfigName(this, "ExistingDeploymentConfiguration", "MyExistingDeploymentConfiguration");
Lambda Applications
To create a new CodeDeploy Application that deploys to a Lambda function:
LambdaApplication application = LambdaApplication.Builder.create(this, "CodeDeployApplication")
.applicationName("MyApplication")
.build();
To import an already existing Application:
ILambdaApplication 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;
Version version = func.getCurrentVersion();
Alias version1Alias = Alias.Builder.create(this, "alias")
.aliasName("prod")
.version(version)
.build();
LambdaDeploymentGroup deploymentGroup = LambdaDeploymentGroup.Builder.create(this, "BlueGreenDeployment")
.application(myApplication) // optional property: one will be created for you if not provided
.alias(version1Alias)
.deploymentConfig(LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE)
.build();
In order to deploy a new version of this function:
- Reference the version with the latest changes
const version = func.currentVersion. - Re-deploy the stack (this will trigger a deployment).
- Monitor the CodeDeploy deployment as traffic shifts between the versions.
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;
CustomLambdaDeploymentConfig config = CustomLambdaDeploymentConfig.Builder.create(this, "CustomConfig")
.type(CustomLambdaDeploymentConfigType.CANARY)
.interval(Duration.minutes(1))
.percentage(5)
.build();
LambdaDeploymentGroup deploymentGroup = LambdaDeploymentGroup.Builder.create(this, "BlueGreenDeployment")
.application(application)
.alias(alias)
.deploymentConfig(config)
.build();
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.
CustomLambdaDeploymentConfig config = CustomLambdaDeploymentConfig.Builder.create(this, "CustomConfig")
.type(CustomLambdaDeploymentConfigType.CANARY)
.interval(Duration.minutes(1))
.percentage(5)
.deploymentConfigName("MyDeploymentConfig")
.build();
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:
import software.amazon.awscdk.services.cloudwatch.*;
Alias alias;
// or add alarms to an existing group
Alias blueGreenAlias;
Alarm alarm = Alarm.Builder.create(this, "Errors")
.comparisonOperator(ComparisonOperator.GREATER_THAN_THRESHOLD)
.threshold(1)
.evaluationPeriods(1)
.metric(alias.metricErrors())
.build();
LambdaDeploymentGroup deploymentGroup = LambdaDeploymentGroup.Builder.create(this, "BlueGreenDeployment")
.alias(alias)
.deploymentConfig(LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE)
.alarms(List.of(alarm))
.build();
deploymentGroup.addAlarm(Alarm.Builder.create(this, "BlueGreenErrors")
.comparisonOperator(ComparisonOperator.GREATER_THAN_THRESHOLD)
.threshold(1)
.evaluationPeriods(1)
.metric(blueGreenAlias.metricErrors())
.build());
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
LambdaDeploymentGroup deploymentGroup = LambdaDeploymentGroup.Builder.create(this, "BlueGreenDeployment")
.alias(alias)
.deploymentConfig(LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE)
.preHook(warmUpUserCache)
.build();
// 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;
ILambdaDeploymentGroup deploymentGroup = LambdaDeploymentGroup.fromLambdaDeploymentGroupAttributes(this, "ExistingCodeDeployDeploymentGroup", LambdaDeploymentGroupAttributes.builder()
.application(application)
.deploymentGroupName("MyExistingDeploymentGroup")
.build());
Deprecated: 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 https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html-
ClassDescriptionThe configuration for automatically rolling back deployments in a given Deployment Group.A builder for
AutoRollbackConfigAn implementation forAutoRollbackConfigA CloudFormationAWS::CodeDeploy::Application.A fluent builder forCfnApplication.Properties for defining aCfnApplication.A builder forCfnApplicationPropsAn implementation forCfnApplicationPropsA CloudFormationAWS::CodeDeploy::DeploymentConfig.A fluent builder forCfnDeploymentConfig.MinimumHealthyHostsis a property of the DeploymentConfig resource that defines how many instances must remain healthy during an AWS CodeDeploy deployment.A builder forCfnDeploymentConfig.MinimumHealthyHostsPropertyAn implementation forCfnDeploymentConfig.MinimumHealthyHostsPropertyA configuration that shifts traffic from one version of a Lambda function or Amazon ECS task set to another in two increments.A builder forCfnDeploymentConfig.TimeBasedCanaryPropertyAn implementation forCfnDeploymentConfig.TimeBasedCanaryPropertyA 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.A builder forCfnDeploymentConfig.TimeBasedLinearPropertyAn implementation forCfnDeploymentConfig.TimeBasedLinearPropertyThe 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.A builder forCfnDeploymentConfig.TrafficRoutingConfigPropertyAn implementation forCfnDeploymentConfig.TrafficRoutingConfigPropertyProperties for defining aCfnDeploymentConfig.A builder forCfnDeploymentConfigPropsAn implementation forCfnDeploymentConfigPropsA CloudFormationAWS::CodeDeploy::DeploymentGroup.TheAlarmConfigurationproperty type configures CloudWatch alarms for an AWS CodeDeploy deployment group.A builder forCfnDeploymentGroup.AlarmConfigurationPropertyAn implementation forCfnDeploymentGroup.AlarmConfigurationPropertyTheAlarmproperty type specifies a CloudWatch alarm to use for an AWS CodeDeploy deployment group.A builder forCfnDeploymentGroup.AlarmPropertyAn implementation forCfnDeploymentGroup.AlarmPropertyTheAutoRollbackConfigurationproperty type configures automatic rollback for an AWS CodeDeploy deployment group when a deployment is not completed successfully.A builder forCfnDeploymentGroup.AutoRollbackConfigurationPropertyAn implementation forCfnDeploymentGroup.AutoRollbackConfigurationPropertyInformation about blue/green deployment options for a deployment group.An implementation forCfnDeploymentGroup.BlueGreenDeploymentConfigurationPropertyInformation about whether instances in the original environment are terminated when a blue/green deployment is successful.A builder forCfnDeploymentGroup.BlueInstanceTerminationOptionPropertyAn implementation forCfnDeploymentGroup.BlueInstanceTerminationOptionPropertyA fluent builder forCfnDeploymentGroup.Deploymentis a property of the DeploymentGroup resource that specifies an AWS CodeDeploy application revision to be deployed to instances in the deployment group.A builder forCfnDeploymentGroup.DeploymentPropertyAn implementation forCfnDeploymentGroup.DeploymentPropertyInformation about how traffic is rerouted to instances in a replacement environment in a blue/green deployment.A builder forCfnDeploymentGroup.DeploymentReadyOptionPropertyAn implementation forCfnDeploymentGroup.DeploymentReadyOptionPropertyInformation 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.A builder forCfnDeploymentGroup.DeploymentStylePropertyAn implementation forCfnDeploymentGroup.DeploymentStylePropertyInformation about an Amazon EC2 tag filter.A builder forCfnDeploymentGroup.EC2TagFilterPropertyAn implementation forCfnDeploymentGroup.EC2TagFilterPropertyTheEC2TagSetproperty type specifies information about groups of tags applied to Amazon EC2 instances.A builder forCfnDeploymentGroup.EC2TagSetListObjectPropertyAn implementation forCfnDeploymentGroup.EC2TagSetListObjectPropertyTheEC2TagSetproperty type specifies information about groups of tags applied to Amazon EC2 instances.A builder forCfnDeploymentGroup.EC2TagSetPropertyAn implementation forCfnDeploymentGroup.EC2TagSetPropertyContains the service and cluster names used to identify an Amazon ECS deployment's target.A builder forCfnDeploymentGroup.ECSServicePropertyAn implementation forCfnDeploymentGroup.ECSServicePropertyTheELBInfoproperty type specifies information about the Elastic Load Balancing load balancer used for an CodeDeploy deployment group.A builder forCfnDeploymentGroup.ELBInfoPropertyAn implementation forCfnDeploymentGroup.ELBInfoPropertyGitHubLocationis a property of the CodeDeploy DeploymentGroup Revision property that specifies the location of an application revision that is stored in GitHub.A builder forCfnDeploymentGroup.GitHubLocationPropertyAn implementation forCfnDeploymentGroup.GitHubLocationPropertyInformation about the instances that belong to the replacement environment in a blue/green deployment.A builder forCfnDeploymentGroup.GreenFleetProvisioningOptionPropertyAn implementation forCfnDeploymentGroup.GreenFleetProvisioningOptionPropertyTheLoadBalancerInfoproperty type specifies information about the load balancer or target group used for an AWS CodeDeploy deployment group.A builder forCfnDeploymentGroup.LoadBalancerInfoPropertyAn implementation forCfnDeploymentGroup.LoadBalancerInfoPropertyTheOnPremisesTagSetListObjectproperty type specifies lists of on-premises instance tag groups.A builder forCfnDeploymentGroup.OnPremisesTagSetListObjectPropertyAn implementation forCfnDeploymentGroup.OnPremisesTagSetListObjectPropertyTheOnPremisesTagSetproperty type specifies a list containing other lists of on-premises instance tag groups.A builder forCfnDeploymentGroup.OnPremisesTagSetPropertyAn implementation forCfnDeploymentGroup.OnPremisesTagSetPropertyRevisionLocationis a property that defines the location of the CodeDeploy application revision to deploy.A builder forCfnDeploymentGroup.RevisionLocationPropertyAn implementation forCfnDeploymentGroup.RevisionLocationPropertyS3Locationis a property of the CodeDeploy DeploymentGroup Revision property that specifies the location of an application revision that is stored in Amazon Simple Storage Service ( Amazon S3 ).A builder forCfnDeploymentGroup.S3LocationPropertyAn implementation forCfnDeploymentGroup.S3LocationPropertyTagFilteris a property type of the AWS::CodeDeploy::DeploymentGroup resource that specifies which on-premises instances to associate with the deployment group.A builder forCfnDeploymentGroup.TagFilterPropertyAn implementation forCfnDeploymentGroup.TagFilterPropertyTheTargetGroupInfoproperty type specifies information about a target group in Elastic Load Balancing to use in a deployment.A builder forCfnDeploymentGroup.TargetGroupInfoPropertyAn implementation forCfnDeploymentGroup.TargetGroupInfoPropertyExample:A builder forCfnDeploymentGroup.TargetGroupPairInfoPropertyAn implementation forCfnDeploymentGroup.TargetGroupPairInfoPropertyExample:A builder forCfnDeploymentGroup.TrafficRoutePropertyAn implementation forCfnDeploymentGroup.TrafficRoutePropertyInformation about notification triggers for the deployment group.A builder forCfnDeploymentGroup.TriggerConfigPropertyAn implementation forCfnDeploymentGroup.TriggerConfigPropertyProperties for defining aCfnDeploymentGroup.A builder forCfnDeploymentGroupPropsAn implementation forCfnDeploymentGroupPropsA custom Deployment Configuration for a Lambda Deployment Group.A fluent builder forCustomLambdaDeploymentConfig.Properties of a reference to a CodeDeploy Lambda Deployment Configuration.A builder forCustomLambdaDeploymentConfigPropsAn implementation forCustomLambdaDeploymentConfigPropsLambda Deployment config type.A CodeDeploy Application that deploys to an Amazon ECS service.A fluent builder forEcsApplication.Construction properties forEcsApplication.A builder forEcsApplicationPropsAn implementation forEcsApplicationPropsA custom Deployment Configuration for an ECS Deployment Group.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.Properties of a reference to a CodeDeploy ECS Deployment Group.A builder forEcsDeploymentGroupAttributesAn implementation forEcsDeploymentGroupAttributesRepresents a reference to a CodeDeploy Application deploying to Amazon ECS.Internal default implementation forIEcsApplication.A proxy class which represents a concrete javascript instance of this type.The Deployment Configuration of an ECS Deployment Group.Internal default implementation forIEcsDeploymentConfig.A proxy class which represents a concrete javascript instance of this type.Interface for an ECS deployment group.Internal default implementation forIEcsDeploymentGroup.A proxy class which represents a concrete javascript instance of this type.Represents a reference to a CodeDeploy Application deploying to AWS Lambda.Internal default implementation forILambdaApplication.A proxy class which represents a concrete javascript instance of this type.The Deployment Configuration of a Lambda Deployment Group.Internal default implementation forILambdaDeploymentConfig.A proxy class which represents a concrete javascript instance of this type.Interface for a Lambda deployment groups.Internal default implementation forILambdaDeploymentGroup.A proxy class which represents a concrete javascript instance of this type.Represents a set of instance tag groups.Represents a reference to a CodeDeploy Application deploying to EC2/on-premise instances.Internal default implementation forIServerApplication.A proxy class which represents a concrete javascript instance of this type.The Deployment Configuration of an EC2/on-premise Deployment Group.Internal default implementation forIServerDeploymentConfig.A proxy class which represents a concrete javascript instance of this type.Internal default implementation forIServerDeploymentGroup.A proxy class which represents a concrete javascript instance of this type.A CodeDeploy Application that deploys to an AWS Lambda function.A fluent builder forLambdaApplication.Construction properties forLambdaApplication.A builder forLambdaApplicationPropsAn implementation forLambdaApplicationPropsA custom Deployment Configuration for a Lambda Deployment Group.Properties of a reference to a CodeDeploy Lambda Deployment Configuration.A builder forLambdaDeploymentConfigImportPropsAn implementation forLambdaDeploymentConfigImportPropsExample:A fluent builder forLambdaDeploymentGroup.Properties of a reference to a CodeDeploy Lambda Deployment Group.A builder forLambdaDeploymentGroupAttributesAn implementation forLambdaDeploymentGroupAttributesConstruction properties forLambdaDeploymentGroup.A builder forLambdaDeploymentGroupPropsAn implementation forLambdaDeploymentGroupPropsAn interface of an abstract load balancer, as needed by CodeDeploy.The generations of AWS load balancing solutions.Minimum number of healthy hosts for a server deployment.A CodeDeploy Application that deploys to EC2/on-premise instances.A fluent builder forServerApplication.Construction properties forServerApplication.A builder forServerApplicationPropsAn implementation forServerApplicationPropsA custom Deployment Configuration for an EC2/on-premise Deployment Group.A fluent builder forServerDeploymentConfig.Construction properties ofServerDeploymentConfig.A builder forServerDeploymentConfigPropsAn implementation forServerDeploymentConfigPropsA CodeDeploy Deployment Group that deploys to EC2/on-premise instances.A fluent builder forServerDeploymentGroup.Properties of a reference to a CodeDeploy EC2/on-premise Deployment Group.A builder forServerDeploymentGroupAttributesAn implementation forServerDeploymentGroupAttributesConstruction properties forServerDeploymentGroup.A builder forServerDeploymentGroupPropsAn implementation forServerDeploymentGroupProps