-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare-deploy-green.ts
179 lines (166 loc) · 5.49 KB
/
prepare-deploy-green.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import {
aws_codebuild as codebuild,
aws_codepipeline as codepipeline,
aws_codepipeline_actions as actions,
aws_iam as iam,
aws_logs as logs,
aws_s3 as s3,
Duration,
RemovalPolicy,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';
export interface PrepareDeployGreenProps {
projectBaseName: string;
artifactBucket: s3.IBucket;
sourceOutput: codepipeline.Artifact;
workloadParameter: WorkloadParameter;
}
export interface WorkloadParameter {
ecsClusterName: string;
ecsServiceName: string;
prodListenerArn: string;
prodListenerRuleArn: string;
testListenerArn: string;
testListenerRuleArn: string;
taskDefinition: string;
containerName: string;
containerPort: number;
vpcId: string;
subnets: string[];
securityGroups: string[];
}
export class PrepareDeployGreen {
public readonly output: codepipeline.Artifact;
public readonly action: actions.CodeBuildAction;
constructor (scope: Construct, props: PrepareDeployGreenProps){
const PROJECT_BASE_NAME = props.projectBaseName;
const artifactBucket = props.artifactBucket;
const sourceOutput = props.sourceOutput;
const workloadParameter = props.workloadParameter;
// -----------------------------
// CodeBuild (Prepare Deploy Green Input)
// -----------------------------
const projectName = `${PROJECT_BASE_NAME}-PrepareDeployGreen`;
// LogGroup for CodeBuild
const logGroup = new logs.LogGroup(scope, 'LogsPrepareDeployGreen', {
logGroupName: `/aws/codebuild/${projectName}`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS
});
// IAM Policy for CodeBuild
const policyDocument = new iam.PolicyDocument({
statements:[
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
's3:PutObject',
's3:GetBucketAcl',
's3:GetBucketLocation'
],
resources: [
artifactBucket.bucketArn,
`${artifactBucket.bucketArn}/*`
]
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'logs:CreateLogGroup',
'logs:CreateLogStream',
'logs:PutLogEvents'
],
resources: [
logGroup.logGroupArn,
`${logGroup.logGroupArn}:*`
]
}),
]
});
// IAM Role for CodeBuild
const role = new iam.Role(scope, 'RolePrepareDeployGreen', {
assumedBy: new iam.ServicePrincipal('codebuild.amazonaws.com'),
description: `Role for CodeBuild of ${projectName}`,
inlinePolicies: {
'policy': policyDocument
},
});
// BuildProject
const project = new codebuild.PipelineProject(scope, 'ProjectPrepareDeployGreen', {
projectName: projectName,
buildSpec: codebuild.BuildSpec.fromSourceFilename('buildspec-prepare-deploy-green.yml'),
environment: {
buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_3,
computeType: codebuild.ComputeType.SMALL,
environmentVariables: {
'clusterName': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.ecsClusterName,
},
'serviceName': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.ecsServiceName,
},
'prodListenerArn': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.prodListenerArn,
},
'prodListenerRuleArn': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.prodListenerRuleArn,
},
'testListenerArn': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.testListenerArn,
},
'testListenerRuleArn': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.testListenerRuleArn,
},
'taskDefinition': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.taskDefinition,
},
'containerName': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.containerName,
},
'containerPort': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.containerPort,
},
'vpcId': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.vpcId,
},
'subnets': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.subnets,
},
'securityGroups': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: workloadParameter.securityGroups,
},
}
},
logging: {
cloudWatch: {
logGroup: logGroup
}
},
role: role,
timeout: Duration.minutes(30),
queuedTimeout: Duration.hours(8)
});
// Artifact
const output = new codepipeline.Artifact('PrepareDeployGreen');
// Action
const action = new actions.CodeBuildAction({
actionName: 'CodeBuild-Prepare-Deploy-Green',
project: project,
input: sourceOutput,
outputs: [ output ],
});
this.output = output;
this.action = action;
}
}