-
Notifications
You must be signed in to change notification settings - Fork 4
/
cloudformation.yaml
291 lines (287 loc) · 7.79 KB
/
cloudformation.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
AWSTemplateFormatVersion: '2010-09-09'
Description: Podcasts RSS feed generator
Parameters:
Stack:
Description: Stack name
Type: String
Default: content-api
Stage:
Description: Stage name
Type: String
Default: PROD
VPC:
Description: Virtual Private Cloud to run EC2 instances within
Type: AWS::EC2::VPC::Id
Subnets:
Description: Subnets to run load balancer within
Type: List<AWS::EC2::Subnet::Id>
AMI:
Description: AMI ID
Type: String
AlarmTopic:
Description: SNS topic ARN for Cloudwatch alerts
Type: String
Resources:
RootRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
DescribeStackResourcePolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: describe-stack-resource
PolicyDocument:
Statement:
- Effect: Allow
Action:
- cloudformation:DescribeStackResource
Resource: "*"
Roles:
- Ref: RootRole
DownloadConfigFromS3Policy:
Type: AWS::IAM::Policy
Properties:
PolicyName: download-config-from-s3
PolicyDocument:
Statement:
- Effect: Allow
Action:
- s3:GetObject
Resource:
- arn:aws:s3:::content-api-config/*
- arn:aws:s3:::content-api-dist/*
Roles:
- Ref: RootRole
CloudwatchPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: cloudwatch-policy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- cloudwatch:*
Resource: '*'
Roles:
- !Ref RootRole
DescribeInstancesPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Statement:
- Action: ec2:DescribeInstances
Effect: Allow
Resource: '*'
PolicyName: ec2-describe-instances
Roles:
- !Ref RootRole
Ec2DescribeAutoScalingGroupsPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: ec2-describe-autoscaling-groups
PolicyDocument:
Statement:
- Effect: Allow
Action:
- autoscaling:DescribeAutoScalingGroups
- autoscaling:DescribeAutoScalingInstances
Resource: "*"
Roles:
- !Ref RootRole
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: "/"
Roles:
- Ref: RootRole
LoadBalancer:
Type: AWS::ElasticLoadBalancing::LoadBalancer
Properties:
Scheme: internet-facing
SecurityGroups:
- Ref: LoadBalancerSecurityGroup
Subnets:
Ref: Subnets
CrossZone: true
Listeners:
- Protocol: HTTP
LoadBalancerPort: '80'
InstancePort: '9000'
HealthCheck:
Target: HTTP:9000/healthcheck
Timeout: '5'
Interval: '10'
UnhealthyThreshold: '2'
HealthyThreshold: '2'
AutoscalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier:
Ref: Subnets
AvailabilityZones:
Fn::GetAZs: ''
LaunchConfigurationName:
Ref: LaunchConfig
MinSize: '2'
MaxSize: '20'
HealthCheckType: ELB
HealthCheckGracePeriod: 300
LoadBalancerNames:
- Ref: LoadBalancer
Tags:
- Key: Stage
Value:
Ref: Stage
PropagateAtLaunch: 'true'
- Key: Stack
Value:
Ref: Stack
PropagateAtLaunch: 'true'
- Key: App
Value: podcasts-rss
PropagateAtLaunch: 'true'
ScaleUpPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AutoScalingGroupName:
Ref: AutoscalingGroup
AdjustmentType: PercentChangeInCapacity
ScalingAdjustment: '100'
Cooldown: '60'
ScaleDownPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AutoScalingGroupName:
Ref: AutoscalingGroup
AdjustmentType: ChangeInCapacity
ScalingAdjustment: "-1"
Cooldown: '600'
HighCPUAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: CPU utilization alarm for autoscaling
EvaluationPeriods: '1'
Statistic: Average
Threshold: '10'
AlarmActions:
- Ref: ScaleUpPolicy
OKActions:
- Ref: ScaleDownPolicy
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: AutoScalingGroupName
Value:
Ref: AutoscalingGroup
MetricName: CPUUtilization
Namespace: AWS/EC2
Period: '60'
High5xxAlarmAlert:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: !Sub ${Stack}-podcasts-rss-${Stage}-alarm-5xx-alert
AlarmDescription: Number of 5XXs is >= 20 per minute for a 2 minute period
Namespace: AWS/ELB
Dimensions:
- Name: LoadBalancerName
Value: !Ref LoadBalancer
MetricName: HTTPCode_Backend_5XX
Statistic: Sum
ComparisonOperator: GreaterThanOrEqualToThreshold
Threshold: '20'
Period: '60'
EvaluationPeriods: '2'
AlarmActions:
- !Ref AlarmTopic
LaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
ImageId:
Ref: AMI
AssociatePublicIpAddress: true
SecurityGroups:
- Ref: ApplicationSecurityGroup
- Ref: WazuhSecurityGroup
InstanceType: t4g.small
IamInstanceProfile:
Ref: InstanceProfile
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -ev
adduser --disabled-password content-api
aws configure set region ${AWS::Region}
cd /home/content-api
mkdir logs
mkdir -p /etc/gu
aws s3 cp s3://content-api-config/podcasts-rss/${Stage}/podcasts-rss.conf /etc/gu/podcasts-rss.conf
aws s3 cp s3://content-api-dist/${Stack}/${Stage}/podcasts-rss/podcasts-rss.service /etc/systemd/system/podcasts-rss.service
aws s3 cp s3://content-api-dist/${Stack}/${Stage}/podcasts-rss/podcasts-rss-0.1.0-SNAPSHOT.tgz .
tar -xvf podcasts-rss-0.1.0-SNAPSHOT.tgz
rm podcasts-rss-0.1.0-SNAPSHOT.tgz
mv podcasts-rss-0.1.0-SNAPSHOT podcasts-rss
chown -R content-api /home/content-api /etc/gu
chgrp -R content-api /home/content-api /etc/gu
systemctl start podcasts-rss
LoadBalancerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Public access to the load balancer on port 80
VpcId:
Ref: VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: '9000'
ToPort: '9000'
CidrIp: 0.0.0.0/0
ApplicationSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SSH and HTTP
VpcId:
Ref: VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '9000'
ToPort: '9000'
CidrIp: 77.91.248.0/21
- IpProtocol: tcp
FromPort: '9000'
ToPort: '9000'
SourceSecurityGroupId:
Ref: LoadBalancerSecurityGroup
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 77.91.248.0/21
WazuhSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow outbound traffic from wazuh agent to manager
VpcId:
!Ref VPC
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 1514
ToPort: 1515
CidrIp: 0.0.0.0/0
Outputs:
LoadBalancer:
Value:
Fn::GetAtt:
- LoadBalancer
- DNSName