1
1
[ ![ cloudcomponents Logo] ( https://raw.githubusercontent.com/cloudcomponents/cdk-constructs/master/logo.png )] ( https://github.com/cloudcomponents/cdk-constructs )
2
2
3
- # @cloudcomponents/cdk-blue-green-container-deployment
3
+ # @cloudcomponents/cdk-blue-green-container-deployment
4
4
5
5
[ ![ Build Status] ( https://github.com/cloudcomponents/cdk-constructs/workflows/Build/badge.svg )] ( https://github.com/cloudcomponents/cdk-constructs/actions?query=workflow=Build )
6
6
[ ![ cdkdx] ( https://img.shields.io/badge/buildtool-cdkdx-blue.svg )] ( https://github.com/hupe1980/cdkdx )
11
11
> Blue green container deployment with CodeDeploy
12
12
13
13
## Install
14
-
15
14
TypeScript/JavaScript:
16
15
17
16
``` bash
@@ -27,97 +26,97 @@ pip install cloudcomponents.cdk-blue-green-container-deployment
27
26
## How to use
28
27
29
28
``` typescript
30
- import { Construct , Stack , StackProps } from " @aws-cdk/core" ;
31
- import { Repository } from " @aws-cdk/aws-codecommit" ;
32
- import { Pipeline , Artifact } from " @aws-cdk/aws-codepipeline" ;
33
- import { Vpc , Port } from " @aws-cdk/aws-ec2" ;
34
- import { Cluster } from " @aws-cdk/aws-ecs" ;
29
+ import { Construct , Stack , StackProps } from ' @aws-cdk/core' ;
30
+ import { Repository } from ' @aws-cdk/aws-codecommit' ;
31
+ import { Pipeline , Artifact } from ' @aws-cdk/aws-codepipeline' ;
32
+ import { Vpc , Port } from ' @aws-cdk/aws-ec2' ;
33
+ import { Cluster } from ' @aws-cdk/aws-ecs' ;
35
34
import {
36
35
ApplicationLoadBalancer ,
37
36
ApplicationTargetGroup ,
38
37
TargetType ,
39
- } from " @aws-cdk/aws-elasticloadbalancingv2" ;
38
+ } from ' @aws-cdk/aws-elasticloadbalancingv2' ;
40
39
import {
41
40
CodeBuildAction ,
42
41
CodeCommitSourceAction ,
43
42
CodeDeployEcsDeployAction ,
44
- } from " @aws-cdk/aws-codepipeline-actions" ;
43
+ } from ' @aws-cdk/aws-codepipeline-actions' ;
45
44
46
- import { ImageRepository } from " @cloudcomponents/cdk-container-registry" ;
45
+ import { ImageRepository } from ' @cloudcomponents/cdk-container-registry' ;
47
46
import {
48
47
EcsService ,
49
48
DummyTaskDefinition ,
50
49
EcsDeploymentGroup ,
51
50
PushImageProject ,
52
- } from " @cloudcomponents/cdk-blue-green-container-deployment" ;
51
+ } from ' @cloudcomponents/cdk-blue-green-container-deployment' ;
53
52
54
53
export class BlueGreenContainerDeploymentStack extends Stack {
55
54
constructor (scope : Construct , id : string , props ? : StackProps ) {
56
55
super (scope , id , props );
57
56
58
- const vpc = new Vpc (this , " Vpc" , {
57
+ const vpc = new Vpc (this , ' Vpc' , {
59
58
maxAzs: 2 ,
60
59
});
61
60
62
- const cluster = new Cluster (this , " Cluster" , {
61
+ const cluster = new Cluster (this , ' Cluster' , {
63
62
vpc ,
64
- clusterName: " blue-green-cluster" ,
63
+ clusterName: ' blue-green-cluster' ,
65
64
});
66
65
67
- const loadBalancer = new ApplicationLoadBalancer (this , " LoadBalancer" , {
66
+ const loadBalancer = new ApplicationLoadBalancer (this , ' LoadBalancer' , {
68
67
vpc ,
69
68
internetFacing: true ,
70
69
});
71
70
72
- const prodListener = loadBalancer .addListener (" ProfListener" , {
71
+ const prodListener = loadBalancer .addListener (' ProfListener' , {
73
72
port: 80 ,
74
73
});
75
74
76
- const testListener = loadBalancer .addListener (" TestListener" , {
75
+ const testListener = loadBalancer .addListener (' TestListener' , {
77
76
port: 8080 ,
78
77
});
79
78
80
79
const prodTargetGroup = new ApplicationTargetGroup (
81
80
this ,
82
- " ProdTargetGroup" ,
81
+ ' ProdTargetGroup' ,
83
82
{
84
83
port: 80 ,
85
84
targetType: TargetType .IP ,
86
85
vpc ,
87
- }
86
+ },
88
87
);
89
88
90
- prodListener .addTargetGroups (" AddProdTg" , {
89
+ prodListener .addTargetGroups (' AddProdTg' , {
91
90
targetGroups: [prodTargetGroup ],
92
91
});
93
92
94
93
const testTargetGroup = new ApplicationTargetGroup (
95
94
this ,
96
- " TestTargetGroup" ,
95
+ ' TestTargetGroup' ,
97
96
{
98
97
port: 8080 ,
99
98
targetType: TargetType .IP ,
100
99
vpc ,
101
- }
100
+ },
102
101
);
103
102
104
- testListener .addTargetGroups (" AddTestTg" , {
103
+ testListener .addTargetGroups (' AddTestTg' , {
105
104
targetGroups: [testTargetGroup ],
106
105
});
107
106
108
107
// Will be replaced by CodeDeploy in CodePipeline
109
108
const taskDefinition = new DummyTaskDefinition (
110
109
this ,
111
- " DummyTaskDefinition" ,
110
+ ' DummyTaskDefinition' ,
112
111
{
113
- image: " nginx" ,
114
- family: " blue-green" ,
115
- }
112
+ image: ' nginx' ,
113
+ family: ' blue-green' ,
114
+ },
116
115
);
117
116
118
- const ecsService = new EcsService (this , " EcsService" , {
117
+ const ecsService = new EcsService (this , ' EcsService' , {
119
118
cluster ,
120
- serviceName: " blue-green-service" ,
119
+ serviceName: ' blue-green-service' ,
121
120
desiredCount: 2 ,
122
121
taskDefinition ,
123
122
prodTargetGroup ,
@@ -126,9 +125,9 @@ export class BlueGreenContainerDeploymentStack extends Stack {
126
125
ecsService .connections .allowFrom (loadBalancer , Port .tcp (80 ));
127
126
ecsService .connections .allowFrom (loadBalancer , Port .tcp (8080 ));
128
127
129
- const deploymentGroup = new EcsDeploymentGroup (this , " DeploymentGroup" , {
130
- applicationName: " blue-green-application" ,
131
- deploymentGroupName: " blue-green-deployment-group" ,
128
+ const deploymentGroup = new EcsDeploymentGroup (this , ' DeploymentGroup' , {
129
+ applicationName: ' blue-green-application' ,
130
+ deploymentGroupName: ' blue-green-deployment-group' ,
132
131
ecsServices: [ecsService ],
133
132
targetGroupNames: [
134
133
prodTargetGroup .targetGroupName ,
@@ -137,78 +136,66 @@ export class BlueGreenContainerDeploymentStack extends Stack {
137
136
prodTrafficListener: prodListener ,
138
137
testTrafficListener: testListener ,
139
138
terminationWaitTimeInMinutes: 100 ,
140
- autoRollbackOnEvents: [RollbackEvent .DEPLOYMENT_FAILURE ],
141
- createDeploymentConfigInput: {
142
- computePlatform: " ECS" ,
143
- deploymentConfigName: " Canary20Percent5Minute" ,
144
- trafficRoutingConfig: {
145
- type: " TimeBasedCanary" ,
146
- timeBasedCanary: {
147
- canaryInterval: 5 ,
148
- canaryPercentage: 20 ,
149
- },
150
- },
151
- },
152
139
});
153
140
154
141
// @see https://github.com/cloudcomponents/cdk-constructs/tree/master/examples/blue-green-container-deployment-example/blue-green-repository
155
- const repository = new Repository (this , " CodeRepository" , {
156
- repositoryName: " blue-green-repository" ,
142
+ const repository = new Repository (this , ' CodeRepository' , {
143
+ repositoryName: ' blue-green-repository' ,
157
144
});
158
145
159
- const imageRepository = new ImageRepository (this , " ImageRepository" , {
146
+ const imageRepository = new ImageRepository (this , ' ImageRepository' , {
160
147
forceDelete: true , // Only for tests
161
148
});
162
149
163
150
const sourceArtifact = new Artifact ();
164
151
165
152
const sourceAction = new CodeCommitSourceAction ({
166
- actionName: " CodeCommit" ,
153
+ actionName: ' CodeCommit' ,
167
154
repository ,
168
155
output: sourceArtifact ,
169
156
});
170
157
171
- const imageArtifact = new Artifact (" ImageArtifact" );
172
- const manifestArtifact = new Artifact (" ManifestArtifact" );
158
+ const imageArtifact = new Artifact (' ImageArtifact' );
159
+ const manifestArtifact = new Artifact (' ManifestArtifact' );
173
160
174
- const pushImageProject = new PushImageProject (this , " PushImageProject" , {
161
+ const pushImageProject = new PushImageProject (this , ' PushImageProject' , {
175
162
imageRepository ,
176
163
taskDefinition ,
177
164
});
178
165
179
166
const buildAction = new CodeBuildAction ({
180
- actionName: " PushImage" ,
167
+ actionName: ' PushImage' ,
181
168
project: pushImageProject ,
182
169
input: sourceArtifact ,
183
170
outputs: [imageArtifact , manifestArtifact ],
184
171
});
185
172
186
173
const deployAction = new CodeDeployEcsDeployAction ({
187
- actionName: " CodeDeploy" ,
174
+ actionName: ' CodeDeploy' ,
188
175
taskDefinitionTemplateInput: manifestArtifact ,
189
176
appSpecTemplateInput: manifestArtifact ,
190
177
containerImageInputs: [
191
178
{
192
179
input: imageArtifact ,
193
- taskDefinitionPlaceholder: " IMAGE1_NAME" ,
180
+ taskDefinitionPlaceholder: ' IMAGE1_NAME' ,
194
181
},
195
182
],
196
183
deploymentGroup ,
197
184
});
198
185
199
- new Pipeline (this , " Pipeline" , {
200
- pipelineName: " blue-green-pipeline" ,
186
+ new Pipeline (this , ' Pipeline' , {
187
+ pipelineName: ' blue-green-pipeline' ,
201
188
stages: [
202
189
{
203
- stageName: " Source" ,
190
+ stageName: ' Source' ,
204
191
actions: [sourceAction ],
205
192
},
206
193
{
207
- stageName: " Build" ,
194
+ stageName: ' Build' ,
208
195
actions: [buildAction ],
209
196
},
210
197
{
211
- stageName: " Deploy" ,
198
+ stageName: ' Deploy' ,
212
199
actions: [deployAction ],
213
200
},
214
201
],
0 commit comments