@Stability(Experimental)

Package io.github.cdklabs.cdk.ecs.codedeploy

CDK ECS CodeDeploy

cdk-constructs: Experimental npm version Maven Central PyPI version NuGet version Gitpod Ready-to-Code Mergify

This project contains CDK constructs to create CodeDeploy ECS deployments.

Installation

TypeScript

 yarn add @cdklabs/cdk-ecs-codedeploy
 

Java

See https://mvnrepository.com/artifact/io.github.cdklabs/cdk-ecs-codedeploy

Python

See https://pypi.org/project/cdklabs.ecs-codedeploy/

C#

See https://www.nuget.org/packages/Cdklabs.CdkEcsCodeDeploy/

Deployments

CodeDeploy for ECS can manage the deployment of new task definitions to ECS services. Only 1 deployment construct can be defined for a given EcsDeploymentGroup.

 IEcsDeploymentGroup deploymentGroup;
 ITaskDefinition taskDefinition;
 
 
 EcsDeployment.Builder.create()
         .deploymentGroup(deploymentGroup)
         .targetService(TargetService.builder()
                 .taskDefinition(taskDefinition)
                 .containerName("mycontainer")
                 .containerPort(80)
                 .build())
         .build();
 

The deployment will use the AutoRollbackConfig for the EcsDeploymentGroup unless it is overridden in the deployment:

 IEcsDeploymentGroup deploymentGroup;
 ITaskDefinition taskDefinition;
 
 
 EcsDeployment.Builder.create()
         .deploymentGroup(deploymentGroup)
         .targetService(TargetService.builder()
                 .taskDefinition(taskDefinition)
                 .containerName("mycontainer")
                 .containerPort(80)
                 .build())
         .autoRollback(AutoRollbackConfig.builder()
                 .failedDeployment(true)
                 .deploymentInAlarm(true)
                 .stoppedDeployment(false)
                 .build())
         .build();
 

By default, the deployment will timeout after 30 minutes. The timeout value can be overridden:

 IEcsDeploymentGroup deploymentGroup;
 ITaskDefinition taskDefinition;
 
 
 EcsDeployment.Builder.create()
         .deploymentGroup(deploymentGroup)
         .targetService(TargetService.builder()
                 .taskDefinition(taskDefinition)
                 .containerName("mycontainer")
                 .containerPort(80)
                 .build())
         .timeout(Duration.minutes(60))
         .build();
 

API Canaries

CodeDeploy can leverage Cloudwatch Alarms to trigger automatic rollbacks. The ApiCanary construct simplifies the process for creating CloudWatch Synthetics Canaries to monitor APIs. The following code demonstrates a canary that monitors https://xkcd.com/908/info.0.json and checks the JSON response to assert that safe_title has the value of 'The Cloud'.

 ApiCanary canary = ApiCanary.Builder.create(stack, "Canary")
         .baseUrl("https://xkcd.com")
         .durationAlarmThreshold(Duration.seconds(5))
         .threadCount(5)
         .steps(List.of(ApiTestStep.builder()
                 .name("info")
                 .path("/908/info.0.json")
                 .jmesPath("safe_title")
                 .expectedValue("The Cloud")
                 .build()))
         .build();
 

Application Load Balanced CodeDeployed Fargate Service

An L3 construct named ApplicationLoadBalancedCodeDeployedFargateService extends ApplicationLoadBalancedFargateService and adds support for deploying new versions of the service with AWS CodeDeploy. Additionally, an Amazon CloudWatch Synthetic canary is created via the ApiCanary construct and is monitored by the CodeDeploy deployment to trigger rollback if the canary begins to alarm.

 ICluster cluster;
 ContainerImage image;
 
 ApplicationLoadBalancedCodeDeployedFargateService service = ApplicationLoadBalancedCodeDeployedFargateService.Builder.create(stack, "Service")
         .cluster(cluster)
         .taskImageOptions(ApplicationLoadBalancedTaskImageOptions.builder()
                 .image(image)
                 .build())
         .apiTestSteps(List.of(ApiTestStep.builder()
                 .name("health")
                 .path("/health")
                 .jmesPath("status")
                 .expectedValue("ok")
                 .build()))
         .build();
 

Local Development

 yarn install
 yarn build
 yarn test
 

To run an integration test and update the snapshot, run:

 yarn integ:ecs-deployment:deploy
 

To recreate snapshots for integration tests, run:

 yarn integ:snapshot-all
 

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.