# AWS CodeDeploy Construct Library
<!--BEGIN STABILITY BANNER-->---
![cfn-resources: Stable](https://img.shields.io/badge/cfn--resources-stable-success.svg?style=for-the-badge)
![cdk-constructs: Stable](https://img.shields.io/badge/cdk--constructs-stable-success.svg?style=for-the-badge)
---
<!--END STABILITY BANNER-->
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:
```python
application = codedeploy.ServerApplication(self, "CodeDeployApplication",
application_name="MyApplication"
)
```
To import an already existing Application:
```python
application = codedeploy.ServerApplication.from_server_application_name(self, "ExistingCodeDeployApplication", "MyExistingApplication")
```
## EC2/on-premise Deployment Groups
To create a new CodeDeploy Deployment Group that deploys to EC2/on-premise instances:
```python
import aws_cdk.aws_autoscaling as autoscaling
import aws_cdk.aws_cloudwatch as cloudwatch
# application: codedeploy.ServerApplication
# asg: autoscaling.AutoScalingGroup
# alarm: cloudwatch.Alarm
deployment_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: true
install_agent=True,
# adds EC2 instances matching tags
ec2_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 tags
on_premise_instance_tags=codedeploy.InstanceTagSet({
"key1": ["v1", "v2"]
}, {
"key2": ["v3"]
}),
# CloudWatch alarms
alarms=[alarm],
# whether to ignore failure to fetch the status of alarms from CloudWatch
# default: false
ignore_poll_alarms_failure=False,
# auto-rollback configuration
auto_rollback=codedeploy.AutoRollbackConfig(
failed_deployment=True, # default: true
stopped_deployment=True, # default: false
deployment_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:
```python
# application: codedeploy.ServerApplication
deployment_group = codedeploy.ServerDeploymentGroup.from_server_deployment_group_attributes(self, "ExistingCodeDeployDeploymentGroup",
application=application,
deployment_group_name="MyExistingDeploymentGroup"
)
```
### Load balancers
You can [specify a load balancer](https://docs.aws.amazon.com/codedeploy/latest/userguide/integrations-aws-elastic-load-balancing.html)
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:
```python
import aws_cdk.aws_elasticloadbalancing as elb
# lb: elb.LoadBalancer
lb.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:
```python
import aws_cdk.aws_elasticloadbalancingv2 as elbv2
# alb: elbv2.ApplicationLoadBalancer
listener = 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 Configurations
You can also pass a Deployment Configuration when creating the Deployment Group:
```python
deployment_group = codedeploy.ServerDeploymentGroup(self, "CodeDeployDeploymentGroup",
deployment_config=codedeploy.ServerDeploymentConfig.ALL_AT_ONCE
)
```
The default Deployment Configuration is `ServerDeploymentConfig.ONE_AT_A_TIME`.
You can also create a custom Deployment Configuration:
```python
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 time
minimum_healthy_hosts=codedeploy.MinimumHealthyHosts.count(2)
)
```
Or import an existing one:
```python
deployment_config = codedeploy.ServerDeploymentConfig.from_server_deployment_config_name(self, "ExistingDeploymentConfiguration", "MyExistingDeploymentConfiguration")
```
## Lambda Applications
To create a new CodeDeploy Application that deploys to a Lambda function:
```python
application = codedeploy.LambdaApplication(self, "CodeDeployApplication",
application_name="MyApplication"
)
```
To import an already existing Application:
```python
application = codedeploy.LambdaApplication.from_lambda_application_name(self, "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:
```python
# my_application: codedeploy.LambdaApplication
# func: lambda.Function
version = func.current_version
version1_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 provided
alias=version1_alias,
deployment_config=codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE
)
```
In order to deploy a new version of this function:
1. Reference the version with the latest changes `const version = func.currentVersion`.
2. Re-deploy the stack (this will trigger a deployment).
3. 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.
```python
# application: codedeploy.LambdaApplication
# alias: lambda.Alias
config = 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.
```python
config = codedeploy.CustomLambdaDeploymentConfig(self, "CustomConfig",
type=codedeploy.CustomLambdaDeploymentConfigType.CANARY,
interval=Duration.minutes(1),
percentage=5,
deployment_config_name="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:
```python
import aws_cdk.aws_cloudwatch as cloudwatch
# alias: lambda.Alias
# or add alarms to an existing group
# blue_green_alias: lambda.Alias
alarm = 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 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.
```python
# warm_up_user_cache: lambda.Function
# end_to_end_validation: lambda.Function
# alias: lambda.Alias
# pass a hook whe creating the deployment group
deployment_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 group
deployment_group.add_post_hook(end_to_end_validation)
```
### Import an existing Deployment Group
To import an already existing Deployment Group:
```python
# application: codedeploy.LambdaApplication
deployment_group = codedeploy.LambdaDeploymentGroup.from_lambda_deployment_group_attributes(self, "ExistingCodeDeployDeploymentGroup",
application=application,
deployment_group_name="MyExistingDeploymentGroup"
)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/aws/aws-cdk",
"name": "aws-cdk.aws-codedeploy",
"maintainer": "",
"docs_url": null,
"requires_python": "~=3.7",
"maintainer_email": "",
"keywords": "",
"author": "Amazon Web Services",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/43/2e/a4d9617147b20241f36dedff1c7e272a0e50ef18d0f6c7f26fbc35178062/aws-cdk.aws-codedeploy-1.203.0.tar.gz",
"platform": null,
"description": "# AWS CodeDeploy Construct Library\n\n<!--BEGIN STABILITY BANNER-->---\n\n\n![cfn-resources: Stable](https://img.shields.io/badge/cfn--resources-stable-success.svg?style=for-the-badge)\n\n![cdk-constructs: Stable](https://img.shields.io/badge/cdk--constructs-stable-success.svg?style=for-the-badge)\n\n---\n<!--END STABILITY BANNER-->\n\nAWS CodeDeploy is a deployment service that automates application deployments to\nAmazon EC2 instances, on-premises instances, serverless Lambda functions, or\nAmazon ECS services.\n\nThe CDK currently supports Amazon EC2, on-premise and AWS Lambda applications.\n\n## EC2/on-premise Applications\n\nTo create a new CodeDeploy Application that deploys to EC2/on-premise instances:\n\n```python\napplication = codedeploy.ServerApplication(self, \"CodeDeployApplication\",\n application_name=\"MyApplication\"\n)\n```\n\nTo import an already existing Application:\n\n```python\napplication = codedeploy.ServerApplication.from_server_application_name(self, \"ExistingCodeDeployApplication\", \"MyExistingApplication\")\n```\n\n## EC2/on-premise Deployment Groups\n\nTo create a new CodeDeploy Deployment Group that deploys to EC2/on-premise instances:\n\n```python\nimport aws_cdk.aws_autoscaling as autoscaling\nimport aws_cdk.aws_cloudwatch as cloudwatch\n\n# application: codedeploy.ServerApplication\n# asg: autoscaling.AutoScalingGroup\n# alarm: cloudwatch.Alarm\n\ndeployment_group = codedeploy.ServerDeploymentGroup(self, \"CodeDeployDeploymentGroup\",\n application=application,\n deployment_group_name=\"MyDeploymentGroup\",\n auto_scaling_groups=[asg],\n # adds User Data that installs the CodeDeploy agent on your auto-scaling groups hosts\n # default: true\n install_agent=True,\n # adds EC2 instances matching tags\n ec2_instance_tags=codedeploy.InstanceTagSet({\n # any instance with tags satisfying\n # key1=v1 or key1=v2 or key2 (any value) or value v3 (any key)\n # will match this group\n \"key1\": [\"v1\", \"v2\"],\n \"key2\": [],\n \"\": [\"v3\"]\n }),\n # adds on-premise instances matching tags\n on_premise_instance_tags=codedeploy.InstanceTagSet({\n \"key1\": [\"v1\", \"v2\"]\n }, {\n \"key2\": [\"v3\"]\n }),\n # CloudWatch alarms\n alarms=[alarm],\n # whether to ignore failure to fetch the status of alarms from CloudWatch\n # default: false\n ignore_poll_alarms_failure=False,\n # auto-rollback configuration\n auto_rollback=codedeploy.AutoRollbackConfig(\n failed_deployment=True, # default: true\n stopped_deployment=True, # default: false\n deployment_in_alarm=True\n )\n)\n```\n\nAll properties are optional - if you don't provide an Application,\none will be automatically created.\n\nTo import an already existing Deployment Group:\n\n```python\n# application: codedeploy.ServerApplication\n\ndeployment_group = codedeploy.ServerDeploymentGroup.from_server_deployment_group_attributes(self, \"ExistingCodeDeployDeploymentGroup\",\n application=application,\n deployment_group_name=\"MyExistingDeploymentGroup\"\n)\n```\n\n### Load balancers\n\nYou can [specify a load balancer](https://docs.aws.amazon.com/codedeploy/latest/userguide/integrations-aws-elastic-load-balancing.html)\nwith the `loadBalancer` property when creating a Deployment Group.\n\n`LoadBalancer` is an abstract class with static factory methods that allow you to create instances of it from various sources.\n\nWith Classic Elastic Load Balancer, you provide it directly:\n\n```python\nimport aws_cdk.aws_elasticloadbalancing as elb\n\n# lb: elb.LoadBalancer\n\nlb.add_listener(\n external_port=80\n)\n\ndeployment_group = codedeploy.ServerDeploymentGroup(self, \"DeploymentGroup\",\n load_balancer=codedeploy.LoadBalancer.classic(lb)\n)\n```\n\nWith Application Load Balancer or Network Load Balancer,\nyou provide a Target Group as the load balancer:\n\n```python\nimport aws_cdk.aws_elasticloadbalancingv2 as elbv2\n\n# alb: elbv2.ApplicationLoadBalancer\n\nlistener = alb.add_listener(\"Listener\", port=80)\ntarget_group = listener.add_targets(\"Fleet\", port=80)\n\ndeployment_group = codedeploy.ServerDeploymentGroup(self, \"DeploymentGroup\",\n load_balancer=codedeploy.LoadBalancer.application(target_group)\n)\n```\n\n## Deployment Configurations\n\nYou can also pass a Deployment Configuration when creating the Deployment Group:\n\n```python\ndeployment_group = codedeploy.ServerDeploymentGroup(self, \"CodeDeployDeploymentGroup\",\n deployment_config=codedeploy.ServerDeploymentConfig.ALL_AT_ONCE\n)\n```\n\nThe default Deployment Configuration is `ServerDeploymentConfig.ONE_AT_A_TIME`.\n\nYou can also create a custom Deployment Configuration:\n\n```python\ndeployment_config = codedeploy.ServerDeploymentConfig(self, \"DeploymentConfiguration\",\n deployment_config_name=\"MyDeploymentConfiguration\", # optional property\n # one of these is required, but both cannot be specified at the same time\n minimum_healthy_hosts=codedeploy.MinimumHealthyHosts.count(2)\n)\n```\n\nOr import an existing one:\n\n```python\ndeployment_config = codedeploy.ServerDeploymentConfig.from_server_deployment_config_name(self, \"ExistingDeploymentConfiguration\", \"MyExistingDeploymentConfiguration\")\n```\n\n## Lambda Applications\n\nTo create a new CodeDeploy Application that deploys to a Lambda function:\n\n```python\napplication = codedeploy.LambdaApplication(self, \"CodeDeployApplication\",\n application_name=\"MyApplication\"\n)\n```\n\nTo import an already existing Application:\n\n```python\napplication = codedeploy.LambdaApplication.from_lambda_application_name(self, \"ExistingCodeDeployApplication\", \"MyExistingApplication\")\n```\n\n## Lambda Deployment Groups\n\nTo enable traffic shifting deployments for Lambda functions, CodeDeploy uses Lambda Aliases, which can balance incoming traffic between two different versions of your function.\nBefore deployment, the alias sends 100% of invokes to the version used in production.\nWhen 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.\n\nTo create a new CodeDeploy Deployment Group that deploys to a Lambda function:\n\n```python\n# my_application: codedeploy.LambdaApplication\n# func: lambda.Function\n\nversion = func.current_version\nversion1_alias = lambda_.Alias(self, \"alias\",\n alias_name=\"prod\",\n version=version\n)\n\ndeployment_group = codedeploy.LambdaDeploymentGroup(self, \"BlueGreenDeployment\",\n application=my_application, # optional property: one will be created for you if not provided\n alias=version1_alias,\n deployment_config=codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE\n)\n```\n\nIn order to deploy a new version of this function:\n\n1. Reference the version with the latest changes `const version = func.currentVersion`.\n2. Re-deploy the stack (this will trigger a deployment).\n3. Monitor the CodeDeploy deployment as traffic shifts between the versions.\n\n### Create a custom Deployment Config\n\nCodeDeploy for Lambda comes with built-in configurations for traffic shifting.\nIf you want to specify your own strategy,\nyou can do so with the CustomLambdaDeploymentConfig construct,\nletting you specify precisely how fast a new function version is deployed.\n\n```python\n# application: codedeploy.LambdaApplication\n# alias: lambda.Alias\nconfig = codedeploy.CustomLambdaDeploymentConfig(self, \"CustomConfig\",\n type=codedeploy.CustomLambdaDeploymentConfigType.CANARY,\n interval=Duration.minutes(1),\n percentage=5\n)\ndeployment_group = codedeploy.LambdaDeploymentGroup(self, \"BlueGreenDeployment\",\n application=application,\n alias=alias,\n deployment_config=config\n)\n```\n\nYou 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.\n\n```python\nconfig = codedeploy.CustomLambdaDeploymentConfig(self, \"CustomConfig\",\n type=codedeploy.CustomLambdaDeploymentConfigType.CANARY,\n interval=Duration.minutes(1),\n percentage=5,\n deployment_config_name=\"MyDeploymentConfig\"\n)\n```\n\n### Rollbacks and Alarms\n\nCodeDeploy will roll back if the deployment fails. You can optionally trigger a rollback when one or more alarms are in a failed state:\n\n```python\nimport aws_cdk.aws_cloudwatch as cloudwatch\n\n# alias: lambda.Alias\n\n# or add alarms to an existing group\n# blue_green_alias: lambda.Alias\n\nalarm = cloudwatch.Alarm(self, \"Errors\",\n comparison_operator=cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,\n threshold=1,\n evaluation_periods=1,\n metric=alias.metric_errors()\n)\ndeployment_group = codedeploy.LambdaDeploymentGroup(self, \"BlueGreenDeployment\",\n alias=alias,\n deployment_config=codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,\n alarms=[alarm\n ]\n)\ndeployment_group.add_alarm(cloudwatch.Alarm(self, \"BlueGreenErrors\",\n comparison_operator=cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,\n threshold=1,\n evaluation_periods=1,\n metric=blue_green_alias.metric_errors()\n))\n```\n\n### Pre and Post Hooks\n\nCodeDeploy allows you to run an arbitrary Lambda function before traffic shifting actually starts (PreTraffic Hook) and after it completes (PostTraffic Hook).\nWith either hook, you have the opportunity to run logic that determines whether the deployment must succeed or fail.\nFor 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.\n\n```python\n# warm_up_user_cache: lambda.Function\n# end_to_end_validation: lambda.Function\n# alias: lambda.Alias\n\n\n# pass a hook whe creating the deployment group\ndeployment_group = codedeploy.LambdaDeploymentGroup(self, \"BlueGreenDeployment\",\n alias=alias,\n deployment_config=codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,\n pre_hook=warm_up_user_cache\n)\n\n# or configure one on an existing deployment group\ndeployment_group.add_post_hook(end_to_end_validation)\n```\n\n### Import an existing Deployment Group\n\nTo import an already existing Deployment Group:\n\n```python\n# application: codedeploy.LambdaApplication\n\ndeployment_group = codedeploy.LambdaDeploymentGroup.from_lambda_deployment_group_attributes(self, \"ExistingCodeDeployDeploymentGroup\",\n application=application,\n deployment_group_name=\"MyExistingDeploymentGroup\"\n)\n```\n\n\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "The CDK Construct Library for AWS::CodeDeploy",
"version": "1.203.0",
"project_urls": {
"Homepage": "https://github.com/aws/aws-cdk",
"Source": "https://github.com/aws/aws-cdk.git"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "50e8b06f24fcf15b59a7cc5291b1f59314cdd2d9f3258d566a8ef377ac648967",
"md5": "73a58507e176135ca97be9946697f9b5",
"sha256": "f31e763896b3027ae8e9184b13f2698ea6d253e157b284be6453896625859235"
},
"downloads": -1,
"filename": "aws_cdk.aws_codedeploy-1.203.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "73a58507e176135ca97be9946697f9b5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "~=3.7",
"size": 260113,
"upload_time": "2023-05-31T22:53:37",
"upload_time_iso_8601": "2023-05-31T22:53:37.798225Z",
"url": "https://files.pythonhosted.org/packages/50/e8/b06f24fcf15b59a7cc5291b1f59314cdd2d9f3258d566a8ef377ac648967/aws_cdk.aws_codedeploy-1.203.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "432ea4d9617147b20241f36dedff1c7e272a0e50ef18d0f6c7f26fbc35178062",
"md5": "dc5ed8efd9fd919bb99f268188aad80f",
"sha256": "3225a03f331445c12ca0bc4b5eb750a9d315bf559e6dc88949bab316ff934211"
},
"downloads": -1,
"filename": "aws-cdk.aws-codedeploy-1.203.0.tar.gz",
"has_sig": false,
"md5_digest": "dc5ed8efd9fd919bb99f268188aad80f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "~=3.7",
"size": 260783,
"upload_time": "2023-05-31T23:01:23",
"upload_time_iso_8601": "2023-05-31T23:01:23.339459Z",
"url": "https://files.pythonhosted.org/packages/43/2e/a4d9617147b20241f36dedff1c7e272a0e50ef18d0f6c7f26fbc35178062/aws-cdk.aws-codedeploy-1.203.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-31 23:01:23",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "aws",
"github_project": "aws-cdk",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "aws-cdk.aws-codedeploy"
}