This repository has been archived by the owner on Dec 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhss-ecs-jenkins-efs.yml
327 lines (325 loc) · 9.19 KB
/
hss-ecs-jenkins-efs.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
---
AWSTemplateFormatVersion: !!str 2010-09-09
Description: ECS Jenkins topped with EFS sprinkles
Parameters:
PKeyName:
Type: AWS::EC2::KeyPair::KeyName
Description: EC2 Key Pair
PVpcId:
Type: AWS::EC2::VPC::Id
Description: VPC
PSubnets:
Type: CommaDelimitedList
Description: Comma separated subnet ids
PAlbPort:
Default: 443
Description: ALB port
Type: Number
MinValue: 80
MaxValue: 65535
PHostPort:
Default: 8000
Description: Host Port
Type: Number
MinValue: 80
MaxValue: 65535
PContainerPort:
Default: 8080
Description: Container Port
Type: Number
MinValue: 80
MaxValue: 65535
PInstanceType:
Type: String
Description: EC2 Instance Type
Default: t2.micro
AllowedValues:
- t2.micro
- t2.small
PDockerImage:
Type: String
Description: Docker Image
PEFSFileSysId:
Type: String
Description: EFS File System Id
PECSMountPath:
Type: String
Description: ECS folder mount path
PEFSMountPrefix:
Type: String
Description: EFS folder mount prefix
PALBSecurityGroup:
Type: String
Description: ALB Security Group
PEFSSecurityGroup:
Type: String
Description: EFS Security Group
Mappings:
ECSAmiMapping:
us-east-1:
AMI: ami-aff65ad2
Resources:
ServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: !!str 2012-10-17
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Path: /
Policies:
- PolicyName: hss-ecs-jenkins
PolicyDocument:
Version: !!str 2012-10-17
Statement:
- Effect: Allow
Action:
- elasticloadbalancing:DeregisterInstancesFromLoadBalancer
- elasticloadbalancing:DeregisterTargets
- elasticloadbalancing:Describe*
- elasticloadbalancing:RegisterInstancesWithLoadBalancer
- elasticloadbalancing:RegisterTargets
- ec2:Describe*
- ec2:AuthorizeSecurityGroupIngress
Resource: '*'
EC2Role:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: !!str 2012-10-17
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Path: /
Policies:
- PolicyName: hss-ec2-jenkins
PolicyDocument:
Version: !!str 2012-10-17
Statement:
- Effect: Allow
Action:
- ecs:*
- ecr:GetAuthorizationToken
- ecr:BatchCheckLayerAvailability
- ecr:GetDownloadUrlForLayer
- ecr:GetRepositoryPolicy
- ecr:DescribeRepositories
- ecr:ListImages
- ecr:DescribeImages
- ecr:BatchGetImage
- elasticloadbalancing:Describe
- elasticfilesystem:DescribeMountTargets
- logs:CreateLogStream
- logs:PutLogEvents
Resource: '*'
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref EC2Role
JenkinsSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: Jenkins-ecs-efs
GroupDescription: Jenkins
VpcId: !Ref PVpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 65.204.38.226/32
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 65.204.38.226/32
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: Jenkins-ecs-efs
ALBIngressJenkins:
Type: AWS::EC2::SecurityGroupIngress
Properties:
Description: Ingress from ALB
IpProtocol: tcp
FromPort: !Ref PAlbPort
ToPort: !Ref PAlbPort
GroupId: !GetAtt JenkinsSecurityGroup.GroupId
SourceSecurityGroupId: !Ref PALBSecurityGroup
ALBEgressJenkins:
Type: AWS::EC2::SecurityGroupEgress
Properties:
Description: Egress to ALB
IpProtocol: tcp
FromPort: !Ref PAlbPort
ToPort: !Ref PAlbPort
GroupId: !GetAtt JenkinsSecurityGroup.GroupId
DestinationSecurityGroupId: !Ref PALBSecurityGroup
EFSIngressJenkins:
Type: AWS::EC2::SecurityGroupIngress
Properties:
Description: Ingress from EFS
IpProtocol: tcp
FromPort: 2049
ToPort: 2049
GroupId: !GetAtt JenkinsSecurityGroup.GroupId
SourceSecurityGroupId: !Ref PEFSSecurityGroup
EFSEgressJenkins:
Type: AWS::EC2::SecurityGroupEgress
Properties:
Description: Egress to EFS
IpProtocol: tcp
FromPort: 2049
ToPort: 2049
GroupId: !GetAtt JenkinsSecurityGroup.GroupId
DestinationSecurityGroupId: !Ref PEFSSecurityGroup
JenkinsIngressEFS:
Type: AWS::EC2::SecurityGroupIngress
Properties:
Description: Ingress from Jenkins
IpProtocol: tcp
FromPort: 2049
ToPort: 2049
GroupId: !Ref PEFSSecurityGroup
SourceSecurityGroupId: !GetAtt JenkinsSecurityGroup.GroupId
JenkinsEgressEFS:
Type: AWS::EC2::SecurityGroupEgress
Properties:
Description: Egress to Jenkins
IpProtocol: tcp
FromPort: 2049
ToPort: 2049
GroupId: !Ref PEFSSecurityGroup
DestinationSecurityGroupId: !GetAtt JenkinsSecurityGroup.GroupId
JenkinsTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 10
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 2
Name: JenkinsTG
Port: !Ref PHostPort
Protocol: HTTP
UnhealthyThresholdCount: 2
VpcId: !Ref PVpcId
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: hss-jenkins
MasterTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: jenkins
NetworkMode: bridge
ContainerDefinition:
- Name: jenkins
Image: !Ref PDockerImage
MountPoints:
- SourceVolume: jenkins-home
ContainerPath: /var/jenkins_home
Essential: true
Cpu: 1024
MemoryReservation: 768
PortMappings:
- HostPort: !Ref PHostPort
ContainerPort: !Ref PContainerPort
Protocol: tcp
Volumes:
- Name: jenkins-home
Host:
SourcePath: /ecs/jenkins_home
ECSService:
Type: AWS::ECS::Service
Properties:
Cluster: !Ref ECSCluster
DesiredCount: 1
ServiceName: jenkins-dev
TaskDefinition: !Ref MasterTaskDefinition
ECSAutoScaling:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier: !Ref PSubnets
DesiredCapacity: 1
LaunchConfigurationName: !Ref ECSLaunchConfiguration
MinSize: 1
MaxSize: 2
HealthCheckType: EC2
HealthCheckGracePeriod: 400
Tags:
- Key: Name
Value: hss-jenkins-dev
PropagateAtLaunch: true
CreationPolicy:
ResourceSignal:
Timeout: PT10M
UpdatePolicy:
AutoScalingReplacingUpdate:
WillReplace: 'true'
ECSLaunchConfiguration:
DependsOn:
- ECSCluster
- JenkinsEgressEFS
- JenkinsIngressEFS
- EFSIngressJenkins
- EFSEgressJenkins
- ALBIngressJenkins
- ALBEgressJenkins
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
AssociatePublicIpAddress: true
BlockDeviceMappings:
- DeviceName: /dev/xvdcz
Ebs:
VolumeSize: 24
DeleteOnTermination: true
ImageId: !FindInMap [ECSAmiMapping, !Ref "AWS::Region", AMI]
IamInstanceProfile: !Ref InstanceProfile
InstanceType: !Ref PInstanceType
KeyName: !Ref PKeyName
SecurityGroups:
- !Ref JenkinsSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash
echo "ECS_CLUSTER=${ECSCluster}" >> /etc/ecs/ecs.config
yum install -y nfs-utils aws-cli jq aws-cfn-bootstrap
mkdir -p ${PECSMountPath}
EFS_MOUNT_DNS='${PEFSFileSysId}.efs.${AWS::Region}.amazonaws.com'
echo $EFS_MOUNT_DNS
mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 $EFS_MOUNT_DNS:/${PEFSMountPrefix} ${PECSMountPath}
chown 1000 ${PECSMountPath}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource ECSAutoScaling --region ${AWS::Region}
Outputs:
Cluster:
Value: !Ref ECSCluster
Service:
Value: !Ref ECSService
TaskDef:
Value: !Ref MasterTaskDefinition