Skip to content

Commit e9aab02

Browse files
authored
Merge pull request #114 from kimisme9386/master
feat(cdk-blue-green-container-deployment): Add support ECSDeploymentConfig creation
2 parents a5f5172 + 66f5a50 commit e9aab02

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

packages/cdk-blue-green-container-deployment/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ export class BlueGreenContainerDeploymentStack extends Stack {
125125
ecsService.connections.allowFrom(loadBalancer, Port.tcp(80));
126126
ecsService.connections.allowFrom(loadBalancer, Port.tcp(8080));
127127

128+
const ecsDeploymentConfiguration = new EcsDeploymentConfiguration(
129+
this,
130+
'DeploymentConfig',
131+
{
132+
deploymentConfigName: 'Canary20Percent5Minute',
133+
trafficRoutingConfig: {
134+
type: 'TimeBasedCanary',
135+
timeBasedCanary: {
136+
canaryInterval: 5,
137+
canaryPercentage: 20,
138+
},
139+
},
140+
}
141+
);
142+
128143
const deploymentGroup = new EcsDeploymentGroup(this, 'DeploymentGroup', {
129144
applicationName: 'blue-green-application',
130145
deploymentGroupName: 'blue-green-deployment-group',
@@ -136,8 +151,11 @@ export class BlueGreenContainerDeploymentStack extends Stack {
136151
prodTrafficListener: prodListener,
137152
testTrafficListener: testListener,
138153
terminationWaitTimeInMinutes: 100,
154+
deploymentConfig: ecsDeploymentConfiguration,
139155
});
140156

157+
deploymentGroup.node.addDependency(ecsDeploymentConfiguration);
158+
141159
// @see https://github.com/cloudcomponents/cdk-constructs/tree/master/examples/blue-green-container-deployment-example/blue-green-repository
142160
const repository = new Repository(this, 'CodeRepository', {
143161
repositoryName: 'blue-green-repository',
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { IEcsDeploymentConfig, CfnDeploymentConfig } from '@aws-cdk/aws-codedeploy';
2+
3+
import { Aws, Construct, IResolvable, Resource } from '@aws-cdk/core';
4+
5+
export interface EcsDeploymentConfigurationProps {
6+
/**
7+
* `AWS::CodeDeploy::DeploymentConfig.DeploymentConfigName`.
8+
*
9+
* @external
10+
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codedeploy-deploymentconfig.html#cfn-codedeploy-deploymentconfig-deploymentconfigname
11+
*/
12+
readonly deploymentConfigName?: string;
13+
/**
14+
* `AWS::CodeDeploy::DeploymentConfig.MinimumHealthyHosts`.
15+
*
16+
* @external
17+
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codedeploy-deploymentconfig.html#cfn-codedeploy-deploymentconfig-minimumhealthyhosts
18+
*/
19+
readonly minimumHealthyHosts?: CfnDeploymentConfig.MinimumHealthyHostsProperty | IResolvable;
20+
/**
21+
* `AWS::CodeDeploy::DeploymentConfig.TrafficRoutingConfig`.
22+
*
23+
* @external
24+
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codedeploy-deploymentconfig.html#cfn-codedeploy-deploymentconfig-trafficroutingconfig
25+
*/
26+
readonly trafficRoutingConfig?: CfnDeploymentConfig.TrafficRoutingConfigProperty | IResolvable;
27+
}
28+
29+
export class EcsDeploymentConfiguration extends Resource implements IEcsDeploymentConfig {
30+
readonly deploymentConfigName: string;
31+
readonly deploymentConfigArn: string;
32+
33+
constructor(scope: Construct, id: string, props: EcsDeploymentConfigurationProps) {
34+
super(scope, id);
35+
36+
const cfnDeploymentConfig = new CfnDeploymentConfig(this, 'EcsDeploymentConfiguration', {
37+
computePlatform: 'ECS',
38+
...props,
39+
});
40+
41+
const deployConfigurationName = props.deploymentConfigName ?? cfnDeploymentConfig.ref;
42+
43+
this.deploymentConfigName = deployConfigurationName;
44+
this.deploymentConfigArn = this.arnForDeploymentConfig(deployConfigurationName);
45+
}
46+
47+
private arnForDeploymentConfig(deploymentConfigurationName: string): string {
48+
return `arn:${Aws.PARTITION}:codedeploy:${Aws.REGION}:${Aws.ACCOUNT_ID}:deploymentconfig:${deploymentConfigurationName}`;
49+
}
50+
}

packages/cdk-blue-green-container-deployment/src/ecs-deployment-group.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export class EcsDeploymentGroup extends Resource implements IEcsDeploymentGroup
112112
TestTrafficListenerArn: testTrafficListener.listenerArn,
113113
TerminationWaitTimeInMinutes: terminationWaitTimeInMinutes,
114114
AutoRollbackOnEvents: autoRollbackOnEvents,
115+
DeploymentConfigName: deploymentConfig?.deploymentConfigName,
115116
},
116117
});
117118

packages/cdk-blue-green-container-deployment/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './ecs-deployment-configuration';
12
export * from './ecs-deployment-group';
23
export * from './ecs-service';
34
export * from './dummy-task-definition';

packages/cdk-blue-green-container-deployment/src/lambdas/ecs-deployment-group/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface EcsDeploymentGroupProps {
2626
testTrafficListenerArn: string;
2727
terminationWaitTimeInMinutes: number;
2828
autoRollbackOnEvents?: RollbackEvent[];
29+
deploymentConfigName?: string;
2930
}
3031

3132
const codeDeploy = new CodeDeploy();
@@ -45,6 +46,7 @@ const getProperties = (
4546
testTrafficListenerArn: props.TestTrafficListenerArn,
4647
terminationWaitTimeInMinutes: props.TerminationWaitTimeInMinutes,
4748
autoRollbackOnEvents: props.AutoRollbackOnEvents,
49+
deploymentConfigName: props.DeploymentConfigName,
4850
});
4951

5052
const onCreate = async (event: CloudFormationCustomResourceCreateEvent): Promise<HandlerReturn> => {
@@ -58,6 +60,7 @@ const onCreate = async (event: CloudFormationCustomResourceCreateEvent): Promise
5860
testTrafficListenerArn,
5961
terminationWaitTimeInMinutes,
6062
autoRollbackOnEvents,
63+
deploymentConfigName,
6164
} = getProperties(event.ResourceProperties);
6265

6366
await codeDeploy
@@ -98,6 +101,7 @@ const onCreate = async (event: CloudFormationCustomResourceCreateEvent): Promise
98101
deploymentType: 'BLUE_GREEN',
99102
deploymentOption: 'WITH_TRAFFIC_CONTROL',
100103
},
104+
deploymentConfigName: deploymentConfigName ?? 'CodeDeployDefault.OneAtATime',
101105
})
102106
.promise();
103107

0 commit comments

Comments
 (0)