-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudformation-template.yaml
672 lines (614 loc) · 16.1 KB
/
cloudformation-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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
Description: Launch GitLab backed up by ActiveDirectory SimpleAD stack within its own VPC or an existing VPC (with email logic using SES)
Parameters:
#GitLab
CookbookUrl:
Type: String
Default: https://github.com/netbears/opsworks-gitlab/releases/download/v1.2.0/cookbooks-v1.2.0.tar.gz
Version:
Type: String
Default: '12.4.0'
InstanceType:
Type: String
Default: 'm5.large'
AllowedValues:
- m5.large
- m5.xlarge
- m5.2xlarge
- m5.4xlarge
InstanceRootVolumeSize:
Type: String
Default: '200'
Description: Size in GB of the root volume
LoadBalancerType:
Type: String
Default: internal
AllowedValues:
- internal
- internet-facing
VpcId:
Type: AWS::EC2::VPC::Id
SubnetIdLoadBalancer:
Type: List<AWS::EC2::Subnet::Id>
SubnetIdHost:
Type: AWS::EC2::Subnet::Id
SslLoadBalancer:
Type: String
Description: SSL ARN from Certificate Manager
DnsRecord:
Type: String
CreateDnsRecord:
Type: String
AllowedValues:
- 'true'
- 'false'
DnsZone:
Type: AWS::Route53::HostedZone::Id
Description: Route53 hosted zone in which to place dns record
#LDAP
LdapHostname:
Type: String
Description: Your LDAP hostname
LdapBindDn:
Type: String
Description: User will have to be created in AD after stack creation
LdapBindPassword:
Type: String
NoEcho: 'true'
Description: Password for bind user needs to be set up to this one after stack creation
LdapUsersBaseDn:
Type: String
Description: Make sure the path exists in AD after stack creation
LdapGroupRequirement:
Type: String
Description: Make sure the group exists in AD after stack creation
LdapGroupBase:
Type: String
Description: The OU in which to look for the group requirement definition
LdapAdminGroup:
Type: String
Description: The group which will have admin rights
# Logging
ElasticsearchPort:
Type: String
Default: '9200'
ElasticsearchHost:
Type: String
KibanaPort:
Type: String
Default: '5601'
KibanaHost:
Type: String
LoggingToElasticsearchEnabled:
Type: String
Default: 'false'
AllowedValues:
- 'true'
- 'false'
Description: Whether to deploy Filebeat and connect it to ElasticSearch
MonitoringToElasticsearchEnabled:
Type: String
Default: 'false'
AllowedValues:
- 'true'
- 'false'
Description: Whether to deploy Metricbeat and connect it to ElasticSearch
# SMTP settings
SmtpEnable:
Type: String
Default: 'false'
AllowedValues:
- 'true'
- 'false'
Description: All other SMTP settings are ignored if this is set to "false"
SmtpAddress:
Type: String
SmtpPort:
Type: String
Default: '587'
SmtpUsername:
Type: String
SmtpPassword:
Type: String
NoEcho: 'true'
Default: 'some-password'
SmtpDomain:
Type: String
Default: 'netbears.com'
SmtpEnableStartTlsAuto:
Type: String
Default: 'true'
AllowedValues:
- 'true'
- 'false'
SmtpTls:
Type: String
Default: 'true'
AllowedValues:
- 'true'
- 'false'
SmtpEmailFrom:
Type: String
Default: gitlab@netbears.com
Conditions:
DeployDnsRecord:
Fn::Equals:
- Ref: CreateDnsRecord
- 'true'
Resources:
KmsEncryptRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal: {Service: [lambda.amazonaws.com]}
Action: ['sts:AssumeRole']
Policies:
- PolicyName:
Fn::Sub: ${AWS::StackName}-KMS
PolicyDocument:
Statement:
- Effect: Allow
Action:
- kms:Encrypt*
- kms:Get*
- kms:List*
- kms:Describe*
Resource: '*'
- PolicyName:
Fn::Sub: ${AWS::StackName}-CloudWatch
PolicyDocument:
Statement:
- Effect: Allow
Action:
- logs:*
- cloudwatch:*
Resource: '*'
KmsEncryptFunction:
Type: AWS::Lambda::Function
Properties:
Handler: index.lambda_handler
Runtime: python3.7
Role:
Fn::GetAtt: KmsEncryptRole.Arn
Code:
ZipFile:
Fn::Sub: |
import boto3
import cfnresponse
import base64
def lambda_handler(event, context):
responseValue={}
kms = boto3.client('kms',region_name="${AWS::Region}")
secret = kms.encrypt(KeyId=event['ResourceProperties']['GitLabPasswordKeyArn'],Plaintext=event['ResourceProperties']['TextToEncrypt'])
blob = secret["CiphertextBlob"]
responseValue["Value"] = str(base64.b64encode(blob))[2:-1]
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseValue)
return True
KmsEncryptResultLdap:
Type: Custom::KmsEncryptLdapBindPassword
Properties:
ServiceToken:
Fn::GetAtt: KmsEncryptFunction.Arn
TextToEncrypt:
Ref: LdapBindPassword
GitLabPasswordKeyArn:
Fn::GetAtt: GitLabPasswordKey.Arn
KmsEncryptResultSmtp:
Type: Custom::KmsEncryptSmtpBindPassword
Properties:
ServiceToken:
Fn::GetAtt: KmsEncryptFunction.Arn
TextToEncrypt:
Ref: SmtpPassword
GitLabPasswordKeyArn:
Fn::GetAtt: GitLabPasswordKey.Arn
GitLabPasswordKey:
Type: AWS::KMS::Key
Properties:
KeyPolicy:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: kms:*
Resource: "*"
Principal:
AWS:
- Fn::Sub: arn:aws:iam::${AWS::AccountId}:root
- Effect: Allow
Action: kms:Encrypt
Resource: "*"
Principal:
AWS:
- Fn::GetAtt: KmsEncryptRole.Arn
- Effect: Allow
Action: kms:Decrypt
Resource: "*"
Principal:
AWS:
- Fn::GetAtt: InstanceRoleGitLab.Arn
GitLabPasswordAlias:
Type: AWS::KMS::Alias
Properties:
AliasName: alias/GitLab
TargetKeyId:
Ref: GitLabPasswordKey
ServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- 'opsworks.amazonaws.com'
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName:
Fn::Sub: ${AWS::StackName}-OpsWorksDefaultPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- 'ec2:*'
- 'iam:PassRole'
- 'cloudwatch:GetMetricStatistics'
- 'cloudwatch:DescribeAlarms'
- 'elasticloadbalancing:*'
- 'ecs:*'
- 'rds:*'
Resource: '*'
InstanceRoleGitLab:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- 'ec2.amazonaws.com'
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName:
Fn::Sub: ${AWS::StackName}-ec2
PolicyDocument:
Statement:
- Effect: Allow
Action:
- 'ec2:Get*'
- 'ec2:List*'
- 'ec2:Describe*'
Resource:
- '*'
- PolicyName:
Fn::Sub: ${AWS::StackName}-s3
PolicyDocument:
Statement:
- Effect: Allow
Action:
- 's3:*'
Resource:
- '*'
- PolicyName:
Fn::Sub: ${AWS::StackName}-CloudWatchLogs
PolicyDocument:
Statement:
- Effect: Allow
Action:
- 'cloudwatch:*'
- 'logs:*'
Resource:
- '*'
InstanceProfileGitLab:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- Ref: InstanceRoleGitLab
Ec2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription:
Fn::Sub: ${AWS::StackName}-Sg
VpcId:
Ref: VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 0
ToPort: 65535
CidrIp: 0.0.0.0/0
- IpProtocol: udp
FromPort: 0
ToPort: 65535
CidrIp: 0.0.0.0/0
AlbSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription:
Fn::Sub: ${AWS::StackName}-Sg
VpcId:
Ref: VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 0
ToPort: 65535
CidrIp: 0.0.0.0/0
- IpProtocol: udp
FromPort: 0
ToPort: 65535
CidrIp: 0.0.0.0/0
SnapshotS3Bucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: True
BlockPublicPolicy: True
IgnorePublicAcls: True
RestrictPublicBuckets: True
OpsWorksStack:
Type: AWS::OpsWorks::Stack
Properties:
AgentVersion: LATEST
Name:
Ref: AWS::StackName
ServiceRoleArn:
Fn::GetAtt:
- ServiceRole
- Arn
DefaultInstanceProfileArn:
Fn::GetAtt:
- InstanceProfileGitLab
- Arn
ConfigurationManager:
Name: Chef
Version: '12'
UseCustomCookbooks: true
CustomCookbooksSource:
Type: archive
Url:
Ref: CookbookUrl
DefaultSubnetId:
Ref: SubnetIdHost
VpcId:
Ref: VpcId
UseOpsworksSecurityGroups: true
DefaultOs: Ubuntu 18.04 LTS
DefaultRootDeviceType: ebs
CustomJson:
Fn::Sub: |
{
"gitlab": {
"version": "${Version}",
"external_url": "https://${DnsRecord}",
"domain": "${DnsRecord}",
"ldap_servers": {
"bind_dn": "${LdapBindDn}",
"password_encrypted": "${KmsEncryptResultLdap.Value}",
"base": "${LdapUsersBaseDn}",
"user_filter": "${LdapGroupRequirement}",
"group_base": "${LdapGroupBase}",
"admin_group": "${LdapAdminGroup}",
"host": "${LdapHostname}"
},
"backup_upload_connection": {
"region": "${AWS::Region}",
"provider": "AWS"
},
"backup_upload_remote_directory": "${SnapshotS3Bucket}",
"smtp_enable": "${SmtpEnable}",
"smtp_address": "${SmtpAddress}",
"smtp_port": "${SmtpPort}",
"smtp_user_name": "${SmtpUsername}",
"smtp_password_encrypted": "${KmsEncryptResultSmtp.Value}",
"smtp_domain": "${SmtpDomain}",
"smtp_enable_starttls_auto": "${SmtpEnableStartTlsAuto}",
"smtp_tls": "${SmtpTls}",
"gitlab_email_from": "${SmtpEmailFrom}"
},
"elasticsearch": {
"http_port": "${ElasticsearchPort}",
"http_ip": "${ElasticsearchHost}"
},
"kibana": {
"http_port": "${KibanaPort}",
"http_ip": "${KibanaHost}"
},
"filebeat": {
"enabled": "${LoggingToElasticsearchEnabled}"
},
"metricbeat": {
"enabled": "${MonitoringToElasticsearchEnabled}"
}
}
Tags:
- Key: Application
Value: GitLab
- Key: MaintainerName
Value: Marius Mitrofan
- Key: MaintainerTeam
Value: NETBEARS
- Key: MaintainerEmail
Value: support@netbears.com
Elb:
Type: AWS::ElasticLoadBalancing::LoadBalancer
DependsOn: Layer
Properties:
HealthCheck:
Interval: 30
Target: TCP:22
Timeout: 5
UnhealthyThreshold: 5
HealthyThreshold: 5
Listeners:
- InstancePort: 80
LoadBalancerPort: 80
Protocol: HTTP
InstanceProtocol: HTTP
PolicyNames:
- Fn::Sub: ${AWS::StackName}
- InstancePort: 22
LoadBalancerPort: 22
Protocol: TCP
InstanceProtocol: TCP
- InstancePort: 80
LoadBalancerPort: 443
Protocol: HTTPS
InstanceProtocol: HTTP
PolicyNames:
- Fn::Sub: ${AWS::StackName}
SSLCertificateId:
Ref: SslLoadBalancer
Scheme:
Ref: LoadBalancerType
SecurityGroups:
- Ref: AlbSecurityGroup
Subnets:
Ref: SubnetIdLoadBalancer
ConnectionDrainingPolicy:
Enabled: 'true'
Timeout: '300'
LBCookieStickinessPolicy:
- CookieExpirationPeriod: 600
PolicyName:
Ref: AWS::StackName
Layer:
Type: AWS::OpsWorks::Layer
Properties:
Name: GitLab
Shortname: gitlab-
Type: custom
EnableAutoHealing: 'false'
AutoAssignElasticIps: 'false'
AutoAssignPublicIps: 'false'
StackId:
Ref: OpsWorksStack
CustomRecipes:
Setup:
- 'gitlab_stack::setup'
UseEbsOptimizedInstances: True
CustomSecurityGroupIds:
- Fn::GetAtt: Ec2SecurityGroup.GroupId
ElbLayer:
DependsOn: Elb
Type: AWS::OpsWorks::ElasticLoadBalancerAttachment
Properties:
ElasticLoadBalancerName:
Ref: Elb
LayerId:
Ref: Layer
InstanceOpsWorks:
Type: AWS::OpsWorks::Instance
DependsOn: Elb
Properties:
StackId:
Ref: OpsWorksStack
LayerIds:
- Ref: Layer
InstanceType:
Ref: InstanceType
RootDeviceType: ebs
SubnetId:
Ref: SubnetIdHost
Hostname: gitlab-1
BlockDeviceMappings:
- DeviceName: ROOT_DEVICE
Ebs:
VolumeType: gp2
VolumeSize:
Ref: InstanceRootVolumeSize
Route53Record:
Type: AWS::Route53::RecordSetGroup
Condition: DeployDnsRecord
Properties:
HostedZoneId:
Ref: DnsZone
Comment:
Fn::Sub: Route53-record-for-GitLab
RecordSets:
- Name:
Ref: DnsRecord
Type: A
AliasTarget:
HostedZoneId:
Fn::GetAtt:
- Elb
- CanonicalHostedZoneNameID
DNSName:
Fn::GetAtt:
- Elb
- DNSName
Outputs:
GitLabURL:
Export:
Name:
Fn::Sub: "${AWS::StackName}-GitLabURL"
Value:
Fn::Sub: "https://${DnsRecord}"
OpsWorksStackId:
Export:
Name:
Fn::Sub: "${AWS::StackName}-OpsWorksStack"
Value:
Ref: OpsWorksStack
LayerId:
Export:
Name:
Fn::Sub: "${AWS::StackName}-Layer"
Value:
Ref: Layer
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: GitLab
Parameters:
- CookbookUrl
- Version
- InstanceType
- InstanceRootVolumeSize
- SslLoadBalancer
- AdminPassword
- CreateDnsRecord
- DnsRecord
- DnsZone
- SubnetIdHost
- SubnetIdLoadBalancer
- VpcId
- LoadBalancerType
- Label:
default: LDAP
Parameters:
- LdapBindDn
- LdapBindPassword
- LdapUsersBaseDn
- LdapGroupRequirement
- LdapGroupBase
- LdapAdminGroup
- LdapHostname
- Label:
default: Logging
Parameters:
- ElasticsearchHost
- ElasticsearchPort
- KibanaHost
- KibanaPort
- LoggingToElasticsearchEnabled
- MonitoringToElasticsearchEnabled
- Label:
default: SMTP
Parameters:
- SmtpEnable
- SmtpAddress
- SmtpPort
- SmtpUsername
- SmtpPassword
- SmtpDomain
- SmtpEnableStartTlsAuto
- SmtpTls
- SmtpEmailFrom