This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
template.yml
288 lines (268 loc) · 7.64 KB
/
template.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
AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::Serverless-2016-10-31'
Description: GitHub personal bot
Parameters:
GitHubTokenParameter:
Description: Parameter Store parameter where encrypted GitHub OAuth Token is stored
Type: AWS::SSM::Parameter::Name
Default: clare-bot-github-token
BotUser:
Description: Github Bot User Name
Type: String
Default: clare-bot
WhitelistedUsers:
Description: White listed users splitted per commas
Type: String
Default: clareliguori
Vpc:
Description: ID of the default VPC (or other VPC with public subnets)
Type: AWS::EC2::VPC::Id
Subnets:
Description: Public subnets for the bot
Type: List<AWS::EC2::Subnet::Id>
BotEnabled:
Description: Whether the bot should be running
Type: String
Default: "Yes"
AllowedValues:
- "Yes"
- "No"
Conditions:
IsBotEnabled: !Equals [!Ref BotEnabled, "Yes"]
Resources:
BotImageRepository:
Type: AWS::ECR::Repository
Properties:
RepositoryName: !Ref 'AWS::StackName'
Service:
Type: AWS::ECS::Service
Properties:
ServiceName: !Ref 'AWS::StackName'
Cluster: "default"
LaunchType: FARGATE
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 100
DesiredCount: !If [IsBotEnabled, 1, 0]
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: ENABLED
SecurityGroups:
- !Ref TaskSecurityGroup
Subnets: !Ref Subnets
TaskDefinition: !Ref 'TaskDefinition'
TaskDefinition:
Type: 'AWS::ECS::TaskDefinition'
Properties:
Family: !Ref 'AWS::StackName'
ContainerDefinitions:
- Name: bot
Image:
'Fn::Join':
- ''
- - !Ref 'AWS::AccountId'
- .dkr.ecr.
- !Ref 'AWS::Region'
- .amazonaws.com/
- !Ref 'AWS::StackName'
- ':latest'
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref Logs
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: 'bot-logs'
Environment:
- Name: botUser
Value: !Ref BotUser
- Name: whitelistedUsers
Value: !Ref WhitelistedUsers
Secrets:
- Name: githubToken
ValueFrom: !Ref GitHubTokenParameter
Cpu: '256'
Memory: '512'
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
ExecutionRoleArn:
'Fn::GetAtt':
- TaskExecutionRole
- Arn
TaskRoleArn:
'Fn::GetAtt':
- TaskRole
- Arn
Logs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Ref 'AWS::StackName'
RetentionInDays: 365
TaskSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: Bot security group, no ingress, all egress
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
Description: Allow all outbound traffic by default
IpProtocol: '-1'
SecurityGroupIngress: []
VpcId: !Ref Vpc
TaskRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: 'sts:AssumeRole'
Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Version: '2012-10-17'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AdministratorAccess # Be able to provision any preview stack resources
TaskExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: 'sts:AssumeRole'
Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Version: '2012-10-17'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
- arn:aws:iam::aws:policy/AmazonSSMReadOnlyAccess
PreviewStackImageRepository:
Type: AWS::ECR::Repository
Properties:
RepositoryName: !Join
- ''
- - !Ref 'AWS::StackName'
- "-preview-images"
LifecyclePolicy:
LifecyclePolicyText: |
{
"rules": [
{
"rulePriority": 1,
"description": "Expire images older than 14 days",
"selection": {
"tagStatus": "any",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 14
},
"action": {
"type": "expire"
}
}
]
}
PreviewStackBuildProject:
DependsOn:
- PreviewStackBuildPolicy
Properties:
Artifacts:
Location: !Ref 'ArtifactsBucket'
NamespaceType: BUILD_ID
Packaging: NONE
Type: S3
Description: !Join
- ''
- - 'CodeBuild Project for '
- !Ref 'AWS::StackName'
Environment:
ComputeType: BUILD_GENERAL1_LARGE
Image: aws/codebuild/nodejs:10.1.0
PrivilegedMode: true
Type: LINUX_CONTAINER
Name: !Ref 'AWS::StackName'
ServiceRole: !Ref 'PreviewStackBuildRole'
Source:
Type: GITHUB
Location: "https://github.com/clareliguori/clare-bot.git"
Auth:
Type: OAUTH
Type: AWS::CodeBuild::Project
PreviewStackBuildRole:
Description: Creating service role in IAM for Amazon EC2 instances
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Path: /
RoleName: !Join
- '-'
- - !Ref 'AWS::StackName'
- CodeBuild
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPowerUser
- arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess
Type: AWS::IAM::Role
PreviewStackBuildPolicy:
Description: Setting IAM policy for service role for CodeBuild
Properties:
PolicyDocument:
Statement:
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource: '*'
- Action:
- s3:PutObject
- s3:GetObject
- s3:GetObjectVersion
Effect: Allow
Resource:
- !Join
- ''
- - 'arn:aws:s3:::'
- !Ref 'ArtifactsBucket'
- !Join
- ''
- - 'arn:aws:s3:::'
- !Ref 'ArtifactsBucket'
- /*
- Action:
- kms:GenerateDataKey*
- kms:Encrypt
- kms:Decrypt
Effect: Allow
Resource:
- !Join
- ':'
- - arn:aws:kms
- !Ref 'AWS::Region'
- !Ref 'AWS::AccountId'
- !Join
- /
- - alias
- aws/s3
PolicyName: !Join
- '-'
- - !Ref 'AWS::StackName'
- PreviewStackBuildPolicy
Roles:
- !Ref 'PreviewStackBuildRole'
Type: AWS::IAM::Policy
ArtifactsBucket:
DeletionPolicy: Delete
Description: Amazon S3 bucket for CodeBuild artifacts
Properties:
Tags:
- Key: Name
Value: !Join
- '-'
- - !Ref 'AWS::StackName'
- ArtifactsBucket
LifecycleConfiguration:
Rules:
- ExpirationInDays : 14
Status: Enabled
Type: AWS::S3::Bucket