-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathecs_cdk-stack.ts
276 lines (227 loc) · 7.97 KB
/
ecs_cdk-stack.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ecs_patterns from 'aws-cdk-lib/aws-ecs-patterns';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as codebuild from 'aws-cdk-lib/aws-codebuild';
import * as codepipeline from 'aws-cdk-lib/aws-codepipeline';
import * as codepipeline_actions from 'aws-cdk-lib/aws-codepipeline-actions';
import { Construct } from 'constructs';
export class EcsCdkStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const githubUserName = new cdk.CfnParameter(this, "githubUserName", {
type: "String",
description: "Github username for source code repository"
})
const githubRepository = new cdk.CfnParameter(this, "githubRespository", {
type: "String",
description: "Github source code repository",
default: "amazon-ecs-fargate-cdk-v2-cicd"
})
const githubPersonalTokenSecretName = new cdk.CfnParameter(this, "githubPersonalTokenSecretName", {
type: "String",
description: "The name of the AWS Secrets Manager Secret which holds the GitHub Personal Access Token for this project.",
default: "/aws-samples/amazon-ecs-fargate-cdk-v2-cicd/github/personal_access_token"
})
//default: `${this.stackName}`
const ecrRepo = new ecr.Repository(this, 'ecrRepo');
/**
* create a new vpc with single nat gateway
*/
const vpc = new ec2.Vpc(this, 'ecs-cdk-vpc', {
subnetConfiguration: [
{
cidrMask: 24,
name: 'public',
subnetType: ec2.SubnetType.PUBLIC,
},
// Add more subnet configurations as needed for private and isolated subnets
],
natGateways: 1,
maxAzs: 3 /* does a sample need 3 az's? */
});
const clusteradmin = new iam.Role(this, 'adminrole', {
assumedBy: new iam.AccountRootPrincipal()
});
const cluster = new ecs.Cluster(this, "ecs-cluster", {
vpc: vpc,
});
const logging = new ecs.AwsLogDriver({
streamPrefix: "ecs-logs"
});
const taskrole = new iam.Role(this, `ecs-taskrole-${this.stackName}`, {
roleName: `ecs-taskrole-${this.stackName}`,
assumedBy: new iam.ServicePrincipal('ecs-tasks.amazonaws.com')
});
// ***ecs contructs***
const executionRolePolicy = new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: ['*'],
actions: [
"ecr:getauthorizationtoken",
"ecr:batchchecklayeravailability",
"ecr:getdownloadurlforlayer",
"ecr:batchgetimage",
"logs:createlogstream",
"logs:putlogevents"
]
});
const taskDef = new ecs.FargateTaskDefinition(this, "ecs-taskdef", {
taskRole: taskrole
});
taskDef.addToExecutionRolePolicy(executionRolePolicy);
const baseImage = 'public.ecr.aws/amazonlinux/amazonlinux:2022'
const container = taskDef.addContainer('flask-app', {
image: ecs.ContainerImage.fromRegistry(baseImage),
memoryLimitMiB: 256,
cpu: 256,
logging
});
container.addPortMappings({
containerPort: 5000,
protocol: ecs.Protocol.TCP
});
const fargateService = new ecs_patterns.ApplicationLoadBalancedFargateService(this, "ecs-service", {
cluster: cluster,
taskDefinition: taskDef,
publicLoadBalancer: true,
desiredCount: 1,
listenerPort: 80
});
/* where do these constants come from? 6, 10, 60? */
const scaling = fargateService.service.autoScaleTaskCount({ maxCapacity: 6 });
scaling.scaleOnCpuUtilization('cpuscaling', {
targetUtilizationPercent: 10,
scaleInCooldown: cdk.Duration.seconds(60),
scaleOutCooldown: cdk.Duration.seconds(60)
});
const gitHubSource = codebuild.Source.gitHub({
owner: githubUserName.valueAsString,
repo: githubRepository.valueAsString,
webhook: true, // optional, default: true if `webhookfilteres` were provided, false otherwise
webhookFilters: [
codebuild.FilterGroup.inEventOf(codebuild.EventAction.PUSH).andBranchIs('main'),
], // optional, by default all pushes and pull requests will trigger a build
});
// codebuild - project
const project = new codebuild.Project(this, 'myProject', {
projectName: `${this.stackName}`,
source: gitHubSource,
environment: {
buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_2,
privileged: true
},
environmentVariables: {
'cluster_name': {
value: `${cluster.clusterName}`
},
'ecr_repo_uri': {
value: `${ecrRepo.repositoryUri}`
}
},
badge: true,
// TODO - I had to hardcode tag here
buildSpec: codebuild.BuildSpec.fromObject({
version: "0.2",
phases: {
pre_build: {
/*
commands: [
'env',
'export tag=${CODEBUILD_RESOLVED_SOURCE_VERSION}'
]
*/
commands: [
'env',
'export tag=latest'
]
},
build: {
commands: [
'cd flask-docker-app',
`docker build -t $ecr_repo_uri:$tag .`,
'$(aws ecr get-login --no-include-email)',
'docker push $ecr_repo_uri:$tag'
]
},
post_build: {
commands: [
'echo "in post-build stage"',
'cd ..',
"printf '[{\"name\":\"flask-app\",\"imageUri\":\"%s\"}]' $ecr_repo_uri:$tag > imagedefinitions.json",
"pwd; ls -al; cat imagedefinitions.json"
]
}
},
artifacts: {
files: [
'imagedefinitions.json'
]
}
})
});
// ***pipeline actions***
const sourceOutput = new codepipeline.Artifact();
const buildOutput = new codepipeline.Artifact();
const nameOfGithubPersonTokenParameterAsString = githubPersonalTokenSecretName.valueAsString
const sourceAction = new codepipeline_actions.GitHubSourceAction({
actionName: 'github_source',
owner: githubUserName.valueAsString,
repo: githubRepository.valueAsString,
branch: 'main',
oauthToken: cdk.SecretValue.secretsManager(nameOfGithubPersonTokenParameterAsString),
output: sourceOutput
});
const buildAction = new codepipeline_actions.CodeBuildAction({
actionName: 'codebuild',
project: project,
input: sourceOutput,
outputs: [buildOutput], // optional
});
const manualApprovalAction = new codepipeline_actions.ManualApprovalAction({
actionName: 'approve',
});
const deployAction = new codepipeline_actions.EcsDeployAction({
actionName: 'deployAction',
service: fargateService.service,
imageFile: new codepipeline.ArtifactPath(buildOutput, `imagedefinitions.json`)
});
// pipeline stages
// NOTE - Approve action is commented out!
new codepipeline.Pipeline(this, 'myecspipeline', {
stages: [
{
stageName: 'source',
actions: [sourceAction],
},
{
stageName: 'build',
actions: [buildAction],
},
{
stageName: 'approve',
actions: [manualApprovalAction],
},
{
stageName: 'deploy-to-ecs',
actions: [deployAction],
}
]
});
ecrRepo.grantPullPush(project.role!)
project.addToRolePolicy(new iam.PolicyStatement({
actions: [
"ecs:describecluster",
"ecr:getauthorizationtoken",
"ecr:batchchecklayeravailability",
"ecr:batchgetimage",
"ecr:getdownloadurlforlayer"
],
resources: [`${cluster.clusterArn}`],
}));
new cdk.CfnOutput(this, "image", { value: ecrRepo.repositoryUri + ":latest" })
new cdk.CfnOutput(this, 'loadbalancerdns', { value: fargateService.loadBalancer.loadBalancerDnsName });
}
}