Package: aws-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 theMigrating to AWS CDK v2guide.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 ApplicationsTo create a new CodeDeploy Application that deploys to EC2/on-premise instances:application=codedeploy.ServerApplication(self,"CodeDeployApplication",application_name="MyApplication")To import an already existing Application:application=codedeploy.ServerApplication.from_server_application_name(self,"ExistingCodeDeployApplication","MyExistingApplication")EC2/on-premise Deployment GroupsTo create a new CodeDeploy Deployment Group that deploys to EC2/on-premise instances:importaws_cdk.aws_autoscalingasautoscalingimportaws_cdk.aws_cloudwatchascloudwatch# application: codedeploy.ServerApplication# asg: autoscaling.AutoScalingGroup# alarm: cloudwatch.Alarmdeployment_group=codedeploy.ServerDeploymentGroup(self,"CodeDeployDeploymentGroup",application=application,deployment_group_name="MyDeploymentGroup",auto_scaling_groups=[asg],# adds User Data that installs the CodeDeploy agent on your auto-scaling groups hosts# default: trueinstall_agent=True,# adds EC2 instances matching tagsec2_instance_tags=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 tagson_premise_instance_tags=codedeploy.InstanceTagSet({"key1":["v1","v2"]},{"key2":["v3"]}),# CloudWatch alarmsalarms=[alarm],# whether to ignore failure to fetch the status of alarms from CloudWatch# default: falseignore_poll_alarms_failure=False,# auto-rollback configurationauto_rollback=codedeploy.AutoRollbackConfig(failed_deployment=True,# default: truestopped_deployment=True,# default: falsedeployment_in_alarm=True))All properties are optional - if you don't provide an Application,
one will be automatically created.To import an already existing Deployment Group:# application: codedeploy.ServerApplicationdeployment_group=codedeploy.ServerDeploymentGroup.from_server_deployment_group_attributes(self,"ExistingCodeDeployDeploymentGroup",application=application,deployment_group_name="MyExistingDeploymentGroup")Load balancersYou canspecify a load balancerwith theloadBalancerproperty when creating a Deployment Group.LoadBalanceris 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:importaws_cdk.aws_elasticloadbalancingaselb# lb: elb.LoadBalancerlb.add_listener(external_port=80)deployment_group=codedeploy.ServerDeploymentGroup(self,"DeploymentGroup",load_balancer=codedeploy.LoadBalancer.classic(lb))With Application Load Balancer or Network Load Balancer,
you provide a Target Group as the load balancer:importaws_cdk.aws_elasticloadbalancingv2aselbv2# alb: elbv2.ApplicationLoadBalancerlistener=alb.add_listener("Listener",port=80)target_group=listener.add_targets("Fleet",port=80)deployment_group=codedeploy.ServerDeploymentGroup(self,"DeploymentGroup",load_balancer=codedeploy.LoadBalancer.application(target_group))Deployment ConfigurationsYou can also pass a Deployment Configuration when creating the Deployment Group:deployment_group=codedeploy.ServerDeploymentGroup(self,"CodeDeployDeploymentGroup",deployment_config=codedeploy.ServerDeploymentConfig.ALL_AT_ONCE)The default Deployment Configuration isServerDeploymentConfig.ONE_AT_A_TIME.You can also create a custom Deployment Configuration:deployment_config=codedeploy.ServerDeploymentConfig(self,"DeploymentConfiguration",deployment_config_name="MyDeploymentConfiguration",# optional property# one of these is required, but both cannot be specified at the same timeminimum_healthy_hosts=codedeploy.MinimumHealthyHosts.count(2))Or import an existing one:deployment_config=codedeploy.ServerDeploymentConfig.from_server_deployment_config_name(self,"ExistingDeploymentConfiguration","MyExistingDeploymentConfiguration")Lambda ApplicationsTo create a new CodeDeploy Application that deploys to a Lambda function:application=codedeploy.LambdaApplication(self,"CodeDeployApplication",application_name="MyApplication")To import an already existing Application:application=codedeploy.LambdaApplication.from_lambda_application_name(self,"ExistingCodeDeployApplication","MyExistingApplication")Lambda Deployment GroupsTo 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:# my_application: codedeploy.LambdaApplication# func: lambda.Functionversion=func.current_versionversion1_alias=lambda_.Alias(self,"alias",alias_name="prod",version=version)deployment_group=codedeploy.LambdaDeploymentGroup(self,"BlueGreenDeployment",application=my_application,# optional property: one will be created for you if not providedalias=version1_alias,deployment_config=codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE)In order to deploy a new version of this function:Reference the version with the latest changesconst 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 ConfigCodeDeploy 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.# application: codedeploy.LambdaApplication# alias: lambda.Aliasconfig=codedeploy.CustomLambdaDeploymentConfig(self,"CustomConfig",type=codedeploy.CustomLambdaDeploymentConfigType.CANARY,interval=Duration.minutes(1),percentage=5)deployment_group=codedeploy.LambdaDeploymentGroup(self,"BlueGreenDeployment",application=application,alias=alias,deployment_config=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.config=codedeploy.CustomLambdaDeploymentConfig(self,"CustomConfig",type=codedeploy.CustomLambdaDeploymentConfigType.CANARY,interval=Duration.minutes(1),percentage=5,deployment_config_name="MyDeploymentConfig")Rollbacks and AlarmsCodeDeploy will roll back if the deployment fails. You can optionally trigger a rollback when one or more alarms are in a failed state:importaws_cdk.aws_cloudwatchascloudwatch# alias: lambda.Alias# or add alarms to an existing group# blue_green_alias: lambda.Aliasalarm=cloudwatch.Alarm(self,"Errors",comparison_operator=cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,threshold=1,evaluation_periods=1,metric=alias.metric_errors())deployment_group=codedeploy.LambdaDeploymentGroup(self,"BlueGreenDeployment",alias=alias,deployment_config=codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,alarms=[alarm])deployment_group.add_alarm(cloudwatch.Alarm(self,"BlueGreenErrors",comparison_operator=cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,threshold=1,evaluation_periods=1,metric=blue_green_alias.metric_errors()))Pre and Post HooksCodeDeploy 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.# warm_up_user_cache: lambda.Function# end_to_end_validation: lambda.Function# alias: lambda.Alias# pass a hook whe creating the deployment groupdeployment_group=codedeploy.LambdaDeploymentGroup(self,"BlueGreenDeployment",alias=alias,deployment_config=codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,pre_hook=warm_up_user_cache)# or configure one on an existing deployment groupdeployment_group.add_post_hook(end_to_end_validation)Import an existing Deployment GroupTo import an already existing Deployment Group:# application: codedeploy.LambdaApplicationdeployment_group=codedeploy.LambdaDeploymentGroup.from_lambda_deployment_group_attributes(self,"ExistingCodeDeployDeploymentGroup",application=application,deployment_group_name="MyExistingDeploymentGroup")