-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess-ci-ecs-cluster.yaml
163 lines (144 loc) · 4.17 KB
/
access-ci-ecs-cluster.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
---
AWSTemplateFormatVersion: 2010-09-09
Parameters:
Name:
Type: String
LaunchType:
Type: String
Default: Fargate
AllowedValues:
- Fargate
- EC2
ECSImageId:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Description: Use an Image from SSM Parameter Store
Default: /aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id
InstanceType:
Type: String
Default: t3.medium
ClusterSize:
Type: Number
Default: 2
Subnets:
Type: List<AWS::EC2::Subnet::Id>
SourceSecurityGroup:
Type: AWS::EC2::SecurityGroup::Id
VpcId:
Type: AWS::EC2::VPC::Id
Conditions:
EC2: !Equals [ !Ref LaunchType, "EC2" ]
Resources:
ECSRole:
Type: AWS::IAM::Role
Condition: EC2
Properties:
Path: /
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: ec2.amazonaws.com
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role
Tags:
- Key: WBS
Value: 'ACCESS CONECT 1.4'
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Condition: EC2
Properties:
Path: /
Roles:
- !Ref ECSRole
SecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Condition: EC2
Properties:
GroupDescription: !Sub ${Name}-hosts
SecurityGroupIngress:
- SourceSecurityGroupId: !Ref SourceSecurityGroup
IpProtocol: -1
VpcId: !Ref VpcId
Tags:
- Key: WBS
Value: 'ACCESS CONECT 1.4'
Cluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: !Ref Name
Tags:
- Key: WBS
Value: 'ACCESS CONECT 1.4'
AutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Condition: EC2
Properties:
VPCZoneIdentifier: !Ref Subnets
LaunchConfigurationName: !Ref LaunchConfiguration
MinSize: !Ref ClusterSize
MaxSize: !Ref ClusterSize
DesiredCapacity: !Ref ClusterSize
Tags:
- Key: Name
Value: !Sub ${Name} - ECS Host
PropagateAtLaunch: true
- Key: WBS
Value: 'ACCESS CONECT 1.4'
PropagateAtLaunch: true
CreationPolicy:
ResourceSignal:
Timeout: PT15M
UpdatePolicy:
AutoScalingRollingUpdate:
MinInstancesInService: 1
MaxBatchSize: 1
PauseTime: PT15M
WaitOnResourceSignals: true
LaunchConfiguration:
Type: AWS::AutoScaling::LaunchConfiguration
Condition: EC2
Metadata:
AWS::CloudFormation::Init:
config:
commands:
01_add_instance_to_cluster:
command: !Sub echo ECS_CLUSTER=${Cluster} > /etc/ecs/ecs.config
files:
"/etc/cfn/cfn-hup.conf":
mode: 000400
owner: root
group: root
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
"/etc/cfn/hooks.d/cfn-auto-reloader.conf":
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.ContainerInstances.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --region ${AWS::Region} --stack ${AWS::StackName} --resource LaunchConfiguration
services:
sysvinit:
cfn-hup:
enabled: true
ensureRunning: true
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
Properties:
ImageId: !Ref ECSImageId
InstanceType: !Ref InstanceType
IamInstanceProfile: !Ref InstanceProfile
SecurityGroups:
- !Ref SecurityGroup
UserData:
"Fn::Base64": !Sub |
#!/bin/bash
yum install -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --region ${AWS::Region} --stack ${AWS::StackName} --resource LaunchConfiguration
/opt/aws/bin/cfn-signal -e $? --region ${AWS::Region} --stack ${AWS::StackName} --resource AutoScalingGroup
Outputs:
ClusterName:
Value: !Ref Cluster