-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfn.yml
459 lines (441 loc) · 14.9 KB
/
cfn.yml
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
---
AWSTemplateFormatVersion: 2010-09-09
Description: >-
Nightly Builds of Custom Docker Images
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Installation
Parameters:
- InstallationId
- Environment
- LogsRetentionPeriod
- ContainerRepositoryName
- TerraformVersion
- TfenvVersion
- SlackChannelId
- SlackWorkpaceId
- SlackConfigurationName
- ChatBotLoggingLevel
ParameterLabels:
InstallationId:
default: Choose a name for this installation
Environment:
default: Deployment environment name
ContainerRepositoryName:
default: Container repository name
LogsRetentionPeriod:
default: The number of days to retain the log events in the specified log group
TerraformVersion:
default: Terraform version to bake inside the docker image
TfenvVersion:
default: Tfenv version to bake inside the docker image
SlackChannelId:
default: Slack channel Id
SlackWorkpaceId:
default : Slack workspace Id
SlackConfigurationName:
default: Slack Configuration name
ChatBotLoggingLevel:
default: Chatbot logging level
Parameters:
InstallationId:
Type: String
Description: >-
An arbitrary short technical name for this installation.
Used to generate (parts of) the names of same resources.
AllowedPattern: "^[a-z0-9]([a-z0-9-]*[a-z0-9])?$"
MaxLength: 20
ConstraintDescription: >-
The installation id must consist only of lowercase letters, digits and dashes.
It cannot start or end in an dash or be longer than twenty characters.
Environment:
Type: String
Description: >-
Deployment environment name
AllowedPattern: "^[a-z0-9]([a-z0-9-]*[a-z0-9])?$"
MaxLength: 20
ContainerRepositoryName:
Type: String
Description: Container repository name
Default: "docker-images"
LogsRetentionPeriod:
Description: >-
The number of days to retain the log events in the specified log group. Possible
values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653.
Type: String
Default: 14
TerraformVersion:
Description: Terraform version to bake inside the docker image
TfenvVersion:
Description: Tfenv version to bake inside the docker image
SlackConfigurationName:
Type: String
Description: The name of the configuration.
ChatBotLoggingLevel:
Type: String
Description: >-
Specifies the logging level for this configuration. This property affects the log entries pushed to
Amazon CloudWatch Logs. Logging levels include ERROR, INFO, or NONE.
AllowedValues:
- ERROR
- INFO
- NONE
SlackChannelId:
Type: String
Description: >-
The ID of the Slack channel. To get the ID, open Slack, right click on the channel name in the
left pane, then choose Copy Link. The channel ID is the 9-character string at the end of the URL.
For example, ABCBBLZZZ.
SlackWorkspaceId:
Type: String
Description: >-
The ID of the Slack workspace authorized with AWS Chatbot. To get the workspace ID, you must
perform the initial authorization flow with Slack in the AWS Chatbot console.
Then you can copy and paste the workspace ID from the console. For more details,
see steps 1-4 in Setting Up AWS Chatbot with Slack in the AWS Chatbot User Guide.
# SnsTopicArn:
# Type: CommaDelimitedList
# Description: The ARNs of the SNS topics that deliver notifications to AWS Chatbot.
Resources:
EventRole:
Type: AWS::IAM::Role
Description: IAM role to allow Amazon CloudWatch Events to trigger AWS CodeBuild build
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service:
- events.amazonaws.com
Sid: 1
Policies:
- PolicyDocument:
Statement:
- Action:
- codebuild:StartBuild
Effect: Allow
Resource: !GetAtt 'BakeDockerImageCodeBuildProject.Arn'
PolicyName: !Join
- '-'
- - !Ref 'AWS::StackName'
- CloudWatchEventPolicy
RoleName: !Join
- '-'
- - !Ref 'AWS::StackName'
- CloudWatchEventRule
Tags:
- Key: Team
Value: DevOps
- Key: Application
Value: DockerImageAutomation
- Key: Environment
Value: !Ref Environment
CronBuildDockerImage:
Type: AWS::Events::Rule
Properties:
ScheduleExpression: cron(0 0 * * ? *) # Run every day at 00:00:00
State: ENABLED
Name: !Join
- '-'
- - !Ref 'AWS::StackName'
- NightlyBuild
Targets:
- Arn: !GetAtt 'BakeDockerImageCodeBuildProject.Arn'
Id: NightlyCheck
RoleArn: !GetAtt 'EventRole.Arn'
# Service role for CodeBuild project
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- codebuild.amazonaws.com
Action:
- sts:AssumeRole
Path: '/'
Policies:
- PolicyName: CodeBuildRunnerLogsPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource: !GetAtt BakeDockerImageCodeBuildLogGroup.Arn
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- ecr:GetAuthorizationToken
- ecr:GetDownloadUrlForLayer
- ecr:BatchGetImage
- ecr:BatchCheckLayerAvailability
- ecr:PutImage
- ecr:InitiateLayerUpload
- ecr:UploadLayerPart
- ecr:CompleteLayerUpload
- sts:AssumeRole
Resource: !GetAtt ContainerRepository.Arn
- Effect: Allow
Action:
- iam:GetRole
- iam:CreateServiceLinkedRole
Resource: "*"
RoleName: !Join
- '-'
- - !Ref 'AWS::StackName'
- CodeBuildRole
Tags:
- Key: Team
Value: DevOps
- Key: Application
Value: DockerImageAutomation
- Key: Environment
Value: !Ref Environment
# Container repository for storing the CodeBuild image
ContainerRepository:
Type: AWS::ECR::Repository
Properties:
RepositoryName: !Sub "${ContainerRepositoryName}-${InstallationId}"
RepositoryPolicyText:
Version: "2012-10-17"
Statement:
- Sid: AllowPushPull
Effect: Allow
Principal:
AWS:
- Ref: AWS::AccountId
Service:
- codebuild.amazonaws.com
Action:
- "ecr:GetDownloadUrlForLayer"
- "ecr:BatchGetImage"
- "ecr:BatchCheckLayerAvailability"
- "ecr:PutImage"
- "ecr:InitiateLayerUpload"
- "ecr:UploadLayerPart"
- "ecr:CompleteLayerUpload"
Tags:
- Key: Team
Value: DevOps
- Key: Application
Value: DockerImageAutomation
- Key: Environment
Value: !Ref Environment
# Log Group for Codebuild Job. This results into no orphaned log groups
BakeDockerImageCodeBuildLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/coodebuild/docker-image-builder-${InstallationId}"
RetentionInDays: !Ref LogsRetentionPeriod
ChatBotLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/chatbot/devops-chatbot-${InstallationId}"
RetentionInDays: !Ref LogsRetentionPeriod
# CodeBuild project which builds the docker image for the ModulesRunner
BakeDockerImageCodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: NO_ARTIFACTS
Description: !Join
- ''
- - 'CodeBuild Project for '
- !Ref 'AWS::StackName'
Environment:
ComputeType: BUILD_GENERAL1_SMALL
EnvironmentVariables:
- Name: TERRAFORM_VERSION
Type: PLAINTEXT
Value: !Ref TerraformVersion
- Name: TFENV_VERSION
Type: PLAINTEXT
Value: !Ref TfenvVersion
Image: "aws/codebuild/standard:2.0"
PrivilegedMode: true
Type: LINUX_CONTAINER
LogsConfig:
CloudWatchLogs:
Status: ENABLED
GroupName: !Ref BakeDockerImageCodeBuildLogGroup
# S3Logs:
# Location:
# Fn::Sub: "${LogBucket}/codebuild/image-builder/build-log"
# Status: ENABLED
Name: !Ref 'AWS::StackName'
ServiceRole:
Fn::GetAtt:
- "CodeBuildRole"
- "Arn"
Source:
BuildSpec:
Fn::Join:
- ""
- - |
version: 0.2
phases:
install:
runtime-versions:
docker: 18
pre_build:
commands:
- echo Login to AWS ECR
- $(aws ecr get-login --no-include-email)
build:
commands:
- echo Create Dockerfile
- |
TPL="$(cat <<-EOF
FROM ubuntu
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=UTC
ENV TFENV_DIR=/root/.tfenv
ENV TFENV_VER=$TFENV_VERSION
RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime
RUN apt-get update
RUN apt-get install -y $PACKAGES
RUN pip3 install --upgrade $PIP_PACKAGES
RUN ln -s /usr/bin/python3 /usr/bin/python
RUN mkdir \$TFENV_DIR \
&& curl -sL https://github.com/tfutils/tfenv/archive/v\$TFENV_VER.tar.gz | tar xzvf - --strip-components=1 -C \$TFENV_DIR \
&& ln -s \$TFENV_DIR/bin/tfenv /usr/local/sbin/tfenv \
&& ln -s \$TFENV_DIR/bin/terraform /usr/local/sbin/terraform \
&& tfenv install $TERRAFORM_VERSION \
&& tfenv use $TERRAFORM_VERSION \
&& tfenv --version && terraform --version
EOF
)"
- echo "$TPL" | tee Dockerfile
- echo Build docker image
- docker build -t terraform:latest .
- echo Tag docker image
- " "
- Fn::Sub: "- docker tag terraform:latest ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/containers-${InstallationId}:terraform\n"
- " - echo Push docker image to ECR\n"
- " "
- Fn::Sub: "- docker push ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/containers-${InstallationId}:terraform"
Type: NO_SOURCE
Tags:
- Key: Team
Value: DevOps
- Key: Application
Value: DockerImageAutomation
- Key: Environment
Value: !Ref Environment
# SNS Topic for codebuild status
CodeBuildStatusUpdates:
Type: AWS::SNS::Topic
Properties:
TopicName: !Join
- '-'
- - !Ref 'AWS::StackName'
- CodeBuildStatusUpdates
Tags:
- Key: Team
Value: DevOps
- Key: Application
Value: DockerImageAutomation
- Key: Environment
Value: !Ref Environment
CodebuildStatusEventRule:
Type: AWS::Events::Rule
Properties:
Name:
Fn::Sub: "codebuild-status-events-${InstallationId}"
Description: "code build event rule for status"
State: "ENABLED"
EventPattern:
source:
- "aws.codebuild"
detail-type:
- "CodeBuild Build State Change"
detail:
build-status:
- "FAILED"
- "STOPPED"
- "SUCCEEDED"
project-name:
- !Ref BakeDockerImageCodeBuildProject
Targets:
-
Arn:
Ref: "CodeBuildStatusUpdates"
Id: "CodeBuildStatusUpdates"
EventTopicPolicy:
Type: 'AWS::SNS::TopicPolicy'
Properties:
PolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: events.amazonaws.com
Action: 'sns:Publish'
Resource: '*'
Topics:
- !Ref CodeBuildStatusUpdates
# Service-linked Role
ServiceLinkedRoleForChatbot:
Type: 'AWS::IAM::ServiceLinkedRole'
UpdateReplacePolicy: Retain
DeletionPolicy: Retain
Properties:
AWSServiceName: management.chatbot.amazonaws.com
Description: The Service Linked Role used by AWS Chatbot.
# Service role for CodeBuild project
ChatBotRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- chatbot.amazonaws.com.com
Action:
- sts:AssumeRole
Path: '/'
Policies:
- PolicyName: ChatBotPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- cloudwatch:Describe
- cloudwatch:List*
- cloudwatch:Get*
- sns:Get*
Resource: *
RoleName: !Join
- '-'
- - !Ref 'AWS::StackName'
- ChatBotdRole
Tags:
- Key: Team
Value: DevOps
- Key: Application
Value: DockerImageAutomation
- Key: Environment
Value: !Ref Environment
SlackChannelConfiguration:
Type: AWS::Chatbot::SlackChannelConfiguration
Properties:
ConfigurationName: !Ref SlackConfigurationName
IamRoleArn: !Ref ChatBotRole
LoggingLevel: !Ref ChatBotLoggingLevel
SlackChannelId: !Ref SlackChannelId
SlackWorkspaceId: !Ref SlackWorkspaceId
SnsTopicArns:
- !Ref CodeBuildStatusUpdates