-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
642 lines (600 loc) · 20 KB
/
template.yaml
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
Description: Deploys a VPC, with a public and private subnet in on AZ,
along with an IG, NAT, Route Tables, VPCEndpoints
Parameters:
ProjectName:
Type: String
Description: Name of the project these resources belongs to
Default: data-derp
ModuleName:
Type: String
Description: Name of the module these resources belongs to
Default: base
VpcCIDR:
Description: Please enter the IP range (CIDR notation) for this VPC
Type: String
Default: 10.11.0.0/16
PublicSubnetCIDR:
Description: Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone
Type: String
Default: 10.11.1.0/24
PrivateSubnetCIDR:
Description: Please enter the IP range (CIDR notation) for the private subnet in the first Availability Zone
Type: String
Default: 10.11.9.0/24
GithubRepoUrl:
Type: String
Description: The Url of the Github repo (https://github.com/user/repo)
InstanceType:
Type: String
Default: "t3.small"
Description: Instance Type of the Github Runner EC2 Instance
VolumeSize:
Type: String
Default: "8"
Description: Volume Size of the Github Runner EBS Volume
LatestAmiId:
Description: Region specific image from the Parameter Store
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCIDR
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: !Sub "${ProjectName}-${ModuleName}-githubrunner"
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Sub "${ProjectName}-${ModuleName}-githubrunner"
InternetGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 0, !GetAZs '' ]
CidrBlock: !Ref PublicSubnetCIDR
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub "${ProjectName} ${ModuleName} GithubRunner Public Subnet"
PrivateSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 0, !GetAZs '' ]
CidrBlock: !Ref PrivateSubnetCIDR
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Sub "${ProjectName} ${ModuleName} GithubRunner Private Subnet"
NatGateway1EIP:
Type: AWS::EC2::EIP
DependsOn: InternetGatewayAttachment
Properties:
Domain: vpc
NatGateway1:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NatGateway1EIP.AllocationId
SubnetId: !Ref PublicSubnet
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub "${ProjectName} ${ModuleName} GithubRunner Public Routes"
DefaultPublicRoute:
Type: AWS::EC2::Route
DependsOn: InternetGatewayAttachment
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub "${ProjectName} ${ModuleName} GithubRunner Private Routes"
DefaultPrivateRoute1:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTable
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway1
PrivateSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateRouteTable
SubnetId: !Ref PrivateSubnet
NoIngressSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub "${ProjectName}${ModuleName}NoIngress"
GroupDescription: "Security group with no ingress rules"
VpcId: !Ref VPC
S3VPCEndpoint:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
RouteTableIds:
- !Ref PrivateRouteTable
ServiceName:
'Fn::Sub': 'com.amazonaws.${AWS::Region}.s3'
VpcId: !Ref VPC
VPCEndpointSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub "${ProjectName}${ModuleName}VpcEndpoints"
GroupDescription: "Security group for VPC Endpoints"
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: !Ref PrivateSubnetCIDR
CloudwatchVPCEndpoint:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
ServiceName:
'Fn::Sub': 'com.amazonaws.${AWS::Region}.monitoring'
VpcId: !Ref VPC
VpcEndpointType: "Interface"
PrivateDnsEnabled: true
SecurityGroupIds:
- !GetAtt VPCEndpointSecurityGroup.GroupId
SubnetIds:
- !Ref PrivateSubnet
LogsVPCEndpoint:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
ServiceName:
'Fn::Sub': 'com.amazonaws.${AWS::Region}.logs'
VpcId: !Ref VPC
VpcEndpointType: "Interface"
PrivateDnsEnabled: true
SecurityGroupIds:
- !GetAtt VPCEndpointSecurityGroup.GroupId
SubnetIds:
- !Ref PrivateSubnet
SSMVPCEndpoint:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
ServiceName:
'Fn::Sub': 'com.amazonaws.${AWS::Region}.ssm'
VpcId: !Ref VPC
VpcEndpointType: "Interface"
PrivateDnsEnabled: true
SecurityGroupIds:
- !GetAtt VPCEndpointSecurityGroup.GroupId
SubnetIds:
- !Ref PrivateSubnet
SSMMessagesVPCEndpoint:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
ServiceName:
'Fn::Sub': 'com.amazonaws.${AWS::Region}.ssmmessages'
VpcId: !Ref VPC
VpcEndpointType: "Interface"
PrivateDnsEnabled: true
SecurityGroupIds:
- !GetAtt VPCEndpointSecurityGroup.GroupId
SubnetIds:
- !Ref PrivateSubnet
CloudTrailVPCEndpoint:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
ServiceName:
'Fn::Sub': 'com.amazonaws.${AWS::Region}.cloudtrail'
VpcId: !Ref VPC
VpcEndpointType: "Interface"
PrivateDnsEnabled: true
SecurityGroupIds:
- !GetAtt VPCEndpointSecurityGroup.GroupId
SubnetIds:
- !Ref PrivateSubnet
EC2VPCEndpoint:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
ServiceName:
'Fn::Sub': 'com.amazonaws.${AWS::Region}.ec2'
VpcId: !Ref VPC
VpcEndpointType: "Interface"
PrivateDnsEnabled: true
SecurityGroupIds:
- !GetAtt VPCEndpointSecurityGroup.GroupId
SubnetIds:
- !Ref PrivateSubnet
EC2MessagesVPCEndpoint:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
ServiceName:
'Fn::Sub': 'com.amazonaws.${AWS::Region}.ec2messages'
VpcId: !Ref VPC
VpcEndpointType: "Interface"
PrivateDnsEnabled: true
SecurityGroupIds:
- !GetAtt VPCEndpointSecurityGroup.GroupId
SubnetIds:
- !Ref PrivateSubnet
VPCFlowLogsRole:
Type: AWS::IAM::Role
Properties:
Tags:
- Key: Project
Value: !Ref ProjectName
AssumeRolePolicyDocument:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "vpc-flow-logs.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Policies:
- PolicyName: write-to-vpc-logs
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
- "logs:DescribeLogGroups"
- "logs:DescribeLogStreams"
Resource:
- "*"
VPCFlowLogs:
Type: AWS::EC2::FlowLog
Properties:
DeliverLogsPermissionArn: !GetAtt VPCFlowLogsRole.Arn
LogGroupName: VPCFlowLogs
LogFormat: '${version} ${vpc-id} ${subnet-id} ${instance-id} ${interface-id} ${account-id} ${type} ${srcaddr} ${dstaddr} ${srcport} ${dstport} ${pkt-srcaddr} ${pkt-dstaddr} ${protocol} ${bytes} ${packets} ${start} ${end} ${action} ${tcp-flags} ${log-status}'
ResourceId: !Ref VPC
ResourceType: VPC
TrafficType: ALL
Tags:
- Key: Name
Value: !Ref ProjectName
AutoscalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
MinSize: '1'
MaxSize: '1'
DesiredCapacity: '1'
HealthCheckGracePeriod: 300
HealthCheckType: "EC2"
LaunchConfigurationName: !Ref LaunchConfiguration
VPCZoneIdentifier: [ !Ref PrivateSubnet ]
Tags:
- Key: Name
Value: !Sub "${ProjectName}-${ModuleName}"
PropagateAtLaunch: true
- Key: Project
Value: !Ref ProjectName
PropagateAtLaunch: true
- Key: Module
Value: !Ref ModuleName
PropagateAtLaunch: true
UpdatePolicy:
AutoScalingReplacingUpdate:
WillReplace: true
AutoScalingRollingUpdate:
WaitOnResourceSignals: true
LaunchConfiguration:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
ImageId: !Ref LatestAmiId
SecurityGroups:
- !Ref RunnerSecurityGroup
InstanceType: !Ref InstanceType
IamInstanceProfile: !Ref InstanceProfile
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeSize: !Ref VolumeSize
VolumeType: "gp2"
DeleteOnTermination: true
Encrypted: true
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
set -ex
sudo yum install -y git
sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64
curl -o ~/CloudWatchMonitoringScripts-1.2.2.zip https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip
unzip ~/CloudWatchMonitoringScripts-1.2.2.zip -d ~ && rm -f ~/CloudWatchMonitoringScripts-1.2.2.zip
(crontab -l || true; echo "*/2 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --mem-used --mem-avail --disk-space-used --disk-space-avail --disk-space-util --disk-path=/ --from-cron") | crontab -
sudo amazon-linux-extras enable docker
sudo yum install -y docker jq
sudo service docker start
mkdir actions-runner && cd actions-runner
curl -o actions-runner-linux-x64-2.277.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.277.1/actions-runner-linux-x64-2.277.1.tar.gz
tar xzf ./actions-runner-linux-x64-2.277.1.tar.gz
get-token() {
key="/${ProjectName}/${ModuleName}/github-runner-reg-token"
aws ssm get-parameters --name ${!key} --region ${AWS::Region} --with-decryption | jq -r .Parameters[0].Value
}
set +x
token=$(get-token)
if [ "${!token}" == "null" ]; then
echo "Registration Token not available in Parameter Store. Exiting"
exit 1
fi
RUNNER_ALLOW_RUNASROOT=1 ./config.sh --url ${GithubRepoUrl} --token "${!token}" --unattended
RUNNER_ALLOW_RUNASROOT=1 ./run.sh
set -x
RunnerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub "${ProjectName}${ModuleName}GithubRunner"
GroupDescription: !Sub "${ProjectName} ${ModuleName} Security group Github Runner"
VpcId: !Ref VPC
EgressToVPCEndpoints:
Type: AWS::EC2::SecurityGroupEgress
Properties:
IpProtocol: tcp
FromPort: 443
ToPort: 443
GroupId: !Ref RunnerSecurityGroup
DestinationSecurityGroupId: !GetAtt VPCEndpointSecurityGroup.GroupId
Egress80:
Type: AWS::EC2::SecurityGroupEgress
Properties:
IpProtocol: tcp
FromPort: 80
ToPort: 80
GroupId: !Ref RunnerSecurityGroup
CidrIp: "0.0.0.0/0"
Egress443:
Type: AWS::EC2::SecurityGroupEgress
Properties:
IpProtocol: tcp
FromPort: 443
ToPort: 443
GroupId: !Ref RunnerSecurityGroup
CidrIp: "0.0.0.0/0"
InstanceRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub "${ProjectName}-${ModuleName}-github-runner"
Tags:
- Key: Module
Value: !Ref ModuleName
- Key: Project
Value: !Ref ProjectName
AssumeRolePolicyDocument:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
Policies:
- PolicyName: !Sub "${ProjectName}-${ModuleName}-get-parameter"
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "ssm:GetParameters"
Resource:
- !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${ProjectName}/${ModuleName}/github-runner-reg-token"
- Effect: Allow
Action:
- "kms:Decrypt"
- "kms:GenerateDataKey"
Resource:
- !Sub "arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/alias/aws/ssm"
- PolicyName: !Sub "${ProjectName}-${ModuleName}-put-cloudwatch-metrics"
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "cloudwatch:PutMetricData"
- "cloudwatch:GetMetricStatistics"
- "cloudwatch:ListMetrics"
Resource:
- "*"
- Effect: Allow
Action:
- "ec2:DescribeTags"
Resource:
- "*"
Condition:
StringEquals:
ec2:ResourceTag/Name: !Sub "${ProjectName}-${ModuleName}"
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref InstanceRole
Boundary:
Type: AWS::IAM::ManagedPolicy
# DeletionPolicy: Retain
Properties:
ManagedPolicyName: !Sub "${ProjectName}-${ModuleName}-delegated-boundary"
Path: !Sub "/${ProjectName}-${ModuleName}/"
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
NotAction:
- "iam:*"
- "organizations:*"
- "account:*"
- "billing:*"
Resource:
- "*"
- Effect: Allow
Action:
- "iam:PassRole"
Resource:
- "*"
Condition:
ForAnyValue:StringLike:
iam:PassedToService: "ecs-tasks.amazonaws.com"
Role:
Type: AWS::IAM::Role
# DeletionPolicy: Retain
Properties:
RoleName: !Sub "${ProjectName}-${ModuleName}-github-runner-aws"
Tags:
- Key: Module
Value: !Ref ModuleName
- Key: Project
Value: !Ref ProjectName
AssumeRolePolicyDocument:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"AWS": !GetAtt InstanceRole.Arn
},
"Action": "sts:AssumeRole"
}
]
}
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/PowerUserAccess"
Policies:
- PolicyName: !Sub "${ProjectName}${ModuleName}DenyInPath"
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Deny
Action:
- "*"
Resource:
- !Sub "arn:aws:iam::*:group/${ProjectName}-${ModuleName}/*"
- !Sub "arn:aws:iam::*:instance-profile/${ProjectName}-${ModuleName}/*"
- !Sub "arn:aws:iam::*:policy/${ProjectName}-${ModuleName}/*"
- !Sub "arn:aws:iam::*:role/${ProjectName}-${ModuleName}/*"
- !Sub "arn:aws:iam::*:user/${ProjectName}-${ModuleName}/*"
- PolicyName: !Sub "${ProjectName}${ModuleName}AllowManagingRolesWithBoundary"
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "iam:AttachRolePolicy"
- "iam:CreateRole"
- "iam:DeleteRolePolicy"
- "iam:DetachRolePolicy"
- "iam:PutRolePermissionsBoundary"
- "iam:DeleteRolePermissionsBoundary"
- "iam:PutRolePolicy"
Resource:
- "*"
Condition:
ForAnyValue:StringEquals:
iam:PermissionsBoundary: !Ref Boundary
- Effect: Allow
Action:
- "iam:AttachUserPolicy"
- "iam:CreateUser"
- "iam:DetachUserPolicy"
- "iam:PutUserPermissionsBoundary"
- "iam:PutUserPolicy"
Resource:
- "*"
Condition:
ForAnyValue:StringEquals:
iam:PermissionsBoundary: !Ref Boundary
- Effect: Allow
Action:
- "iam:DeleteRole"
- "iam:GetRole"
- "iam:GetRolePolicy"
- "iam:ListAttachedRolePolicies"
- "iam:ListInstanceProfilesForRole"
- "iam:ListRolePolicies"
- "iam:ListRoleTags"
- "iam:ListRoles"
- "iam:PassRole"
- "iam:TagRole"
- "iam:UntagRole"
- "iam:UpdateAssumeRolePolicy"
- "iam:UpdateRole"
- "iam:UpdateRoleDescription"
Resource:
- "*"
- Effect: Allow
Action:
- "iam:AddUserToGroup"
- "iam:CreateAccessKey"
- "iam:DeleteAccessKey"
- "iam:DeleteUser"
- "iam:GetUser"
- "iam:GetUserPolicy"
- "iam:ListAccessKeys"
- "iam:ListAttachedUserPolicies"
- "iam:ListGroupsForUser"
- "iam:ListUsers"
- "iam:ListUserPolicies"
- "iam:TagUser"
- "iam:UntagUser"
Resource:
- "*"
- Effect: Allow
Action:
- "iam:CreatePolicy"
- "iam:CreatePolicyVersion"
- "iam:DeletePolicy"
- "iam:DeletePolicyVersion"
- "iam:GetPolicy"
- "iam:GetPolicyVersion"
- "iam:ListEntitiesForPolicy"
- "iam:ListPolicies"
- "iam:ListPolicyVersions"
- "iam:SetDefaultPolicyVersion"
Resource:
- "*"
- Effect: Allow
Action:
- "iam:AddRoleToInstanceProfile"
- "iam:CreateInstanceProfile"
- "iam:DeleteInstanceProfile"
- "iam:GetInstanceProfile"
- "iam:ListInstanceProfiles"
- "iam:RemoveRoleFromInstanceProfile"
- "iam:GetServiceLinkedRoleDeletionStatus"
Resource:
- "*"