forked from toniblyx/aws-scripts-4fun-and-profit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-snippet-cloudwatchlog.yaml
406 lines (405 loc) · 18.2 KB
/
node-snippet-cloudwatchlog.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
BastionLaunchConfiguration:
Type: AWS::AutoScaling::LaunchConfiguration
Metadata:
AWS::CloudFormation::Authentication:
S3AccessCreds:
type: S3
roleName: !Ref BastionInstanceRole
buckets: !Ref RuntimeAssetsBucketName
AWS::CloudFormation::Init:
config:
packages:
yum:
awslogs: []
files:
'/etc/awslogs/awscli.conf':
content: !Sub |
[default]
region = ${AWS::Region}
[plugins]
cwlogs = cwlogs
mode: '000644'
owner: root
group: root
'/etc/awslogs/awslogs.conf':
content: !Sub |
[general]
state_file = /var/lib/awslogs/agent-state
[/var/log/bastion/bastion.log]
file = /var/log/bastion/bastion.log
datetime_format = %b %d %H:%M:%S
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/bastion/bastion.log
log_group_name = ${EC2LogGroup}
[/var/log/dmesg]
file = /var/log/dmesg
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/dmesg
log_group_name = ${EC2LogGroup}
[/var/log/messages]
datetime_format = %b %d %H:%M:%S
file = /var/log/messages
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/messages
log_group_name = ${EC2LogGroup}
[/var/log/secure]
datetime_format = %b %d %H:%M:%S
file = /var/log/secure
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/secure
log_group_name = ${EC2LogGroup}
[/var/log/audit/audit.log]
datetime_format =
file = /var/log/audit/audit.log
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/audit/audit.log
log_group_name = ${EC2LogGroup}
[/var/log/cron]
datetime_format = %b %d %H:%M:%S
file = /var/log/cron
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/cron
log_group_name = ${EC2LogGroup}
[/var/log/cfn-init.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/cfn-init.log
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/cfn-init.log
log_group_name = ${EC2LogGroup}
[/var/log/cfn-hup.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/cfn-hup.log
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/cfn-hup.log
log_group_name = ${EC2LogGroup}
[/var/log/cfn-init-cmd.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/cfn-init-cmd.log
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/cfn-init-cmd.log
log_group_name = ${EC2LogGroup}
[/var/log/cloud-init-output.log]
file = /var/log/cloud-init-output.log
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/cloud-init-output.log
log_group_name = ${EC2LogGroup}
[/var/log/amazon/ssm/amazon-ssm-agent.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/amazon/ssm/amazon-ssm-agent.log
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/amazon/ssm/amazon-ssm-agent.log
log_group_name = ${EC2LogGroup}
[/var/log/amazon/ssm/errors.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/amazon/ssm/errors.log
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/amazon/ssm/errors.log
log_group_name = ${EC2LogGroup}
[/var/log/maillog]
datetime_format = %b %d %H:%M:%S
file = /var/log/maillog
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/maillog
log_group_name = ${EC2LogGroup}
[/var/log/yum.log]
datetime_format = %b %d %H:%M:%S
file = /var/log/yum.log
log_stream_name = ${AWS::StackName}/bastion-{instance_id}/var/log/yum.log
log_group_name = ${EC2LogGroup}
mode: '000644'
owner: root
group: root
'/tmp/hardening_bootstrap.sh':
source: !Sub "https://${RuntimeAssetsBucketName}.s3.amazonaws.com/scripts/hardening_bootstrap.sh"
mode: "000550"
owner: root
group: root
authentication: S3AccessCreds
'/tmp/eks_bootstrap.sh':
content: !Sub |
#!/bin/bash
echo "Checking whether cluster exists..."
aws eks describe-cluster --region ${AWS::Region} --name ${AWS::StackName}-cluster &> /dev/null
if [ $? -ne 0 ]; then
echo Cluster does not exist, creating...
aws eks create-cluster --region ${AWS::Region} \
--name ${AWS::StackName}-cluster \
--role-arn ${EKSServiceRole.Arn} \
--resources-vpc-config subnetIds=${VPCStack.Outputs.PrivateSubnet1AID},${VPCStack.Outputs.PrivateSubnet2AID},${VPCStack.Outputs.PublicSubnet1ID},${VPCStack.Outputs.PublicSubnet2ID},securityGroupIds=${ControlPlaneSecurityGroup}
if [ $? -ne 0 ]; then
exit 1
fi
sleep 5
STATUS=$(aws eks describe-cluster --region ${AWS::Region} --name ${AWS::StackName}-cluster --query cluster.status --output text)
while [ \"$STATUS\" = \"CREATING\" ]; do
echo Cluster is still creating, sleeping for 30 seconds...
sleep 30
STATUS=$(aws eks describe-cluster --region ${AWS::Region} --name ${AWS::StackName}-cluster --query cluster.status --output text)
done
fi
echo Updating kubeconfig file...
ENDPOINT=$(aws eks describe-cluster --region ${AWS::Region} --name ${AWS::StackName}-cluster --query cluster.endpoint --output text)
CERT_DATA=$(aws eks describe-cluster --region ${AWS::Region} --name ${AWS::StackName}-cluster --query cluster.certificateAuthority.data --output text)
sed -i s,ENDPOINT,$ENDPOINT,g /home/ec2-user/.kube/config
sed -i s,CERTIFICATE_DATA,$CERT_DATA,g /home/ec2-user/.kube/config
export KUBECONFIG=/home/ec2-user/.kube/config
echo Checking whether aws-auth configmap exists...
kubectl get configmaps/aws-auth -n kube-system &> /dev/null
if [ $? -gt 0 ]; then
echo Configmap does not exist, applying...
kubectl apply -f /tmp/aws-auth-cm.yaml
fi
echo Checking whether tiller serviceaccount exists...
kubectl get serviceaccount/tiller -n kube-system &> /dev/null
if [ $? -gt 0 ]; then
echo tiller serviceaccount does not exist, applying...
kubectl apply -f /tmp/helm-rbac-config.yaml
fi
mode: "000750"
owner: root
group: root
'/tmp/aws-auth-cm.yaml':
content: !Sub |
apiVersion: v1
kind: ConfigMap
metadata:
name: aws-auth
namespace: kube-system
data:
mapRoles: |
- rolearn: ${NodeInstanceRole.Arn}
username: system:node:{{EC2PrivateDNSName}}
groups:
- system:bootstrappers
- system:nodes
mode: "000644"
owner: root
group: root
'/tmp/helm-rbac-config.yaml':
content: !Sub |
apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system
mode: "000644"
owner: root
group: root
'/home/ec2-user/.kube/config':
content: !Sub |
apiVersion: v1
clusters:
- cluster:
server: ENDPOINT
certificate-authority-data: CERTIFICATE_DATA
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: aws
name: aws
current-context: aws
kind: Config
preferences: {}
users:
- name: aws
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
command: aws-iam-authenticator
args:
- token
- -i
- ${AWS::StackName}-cluster
mode: "000666"
owner: ec2-user
group: ec2-user
services:
sysvinit:
awslogs:
enabled: "true"
ensureRunning: "true"
packages:
yum:
- awslogs
files:
- "/etc/awslogs/awslogs.conf"
- "/etc/awslogs/awscli.conf"
sendmail:
enabled: "false"
ensureRunning: "false"
commands:
01-eks-bootstrap:
command: "./tmp/eks_bootstrap.sh"
02-hardening-bootstrap:
command: "./tmp/hardening_bootstrap.sh --tcp-forwarding false --x11-forwarding false"
Properties:
AssociatePublicIpAddress: true
PlacementTenancy: default
KeyName: !Ref KeyName
IamInstanceProfile: !Ref BastionInstanceProfile
ImageId: !FindInMap [BastionAmiRegionMap, !Ref "AWS::Region", AmiId]
SecurityGroups:
- !Ref BastionSecurityGroup
InstanceType: t2.micro
UserData:
Fn::Base64: !Sub |
#!/bin/bash
set -x
export PATH=$PATH:/usr/local/bin
pip install awscli --upgrade
easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
cfn-init -v --stack ${AWS::StackName} --resource BastionLaunchConfiguration --region ${AWS::Region}
cfn-signal -e $? --stack ${AWS::StackName} --resource BastionAutoScalingGroup --region ${AWS::Region}
NodeLaunchConfig:
DependsOn: BastionAutoScalingGroup
Type: AWS::AutoScaling::LaunchConfiguration
Metadata:
AWS::CloudFormation::Authentication:
S3AccessCreds:
type: S3
roleName: !Ref NodeInstanceRole
buckets: !Ref RuntimeAssetsBucketName
AWS::CloudFormation::Init:
config:
packages:
yum:
awslogs: []
amazon-ssm-agent: []
amazon-efs-utils: []
files:
'/etc/awslogs/awscli.conf':
content: !Sub |
[default]
region = ${AWS::Region}
[plugins]
cwlogs = cwlogs
mode: '000644'
owner: root
group: root
'/etc/awslogs/awslogs.conf':
content: !Sub |
[general]
state_file = /var/lib/awslogs/agent-state
[/var/log/bastion/bastion.log]
file = /var/log/bastion/bastion.log
datetime_format = %b %d %H:%M:%S
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/bastion/bastion.log
log_group_name = ${EC2LogGroup}
[/var/log/dmesg]
file = /var/log/dmesg
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/dmesg
log_group_name = ${EC2LogGroup}
[/var/log/messages]
datetime_format = %b %d %H:%M:%S
file = /var/log/messages
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/messages
log_group_name = ${EC2LogGroup}
[/var/log/secure]
datetime_format = %b %d %H:%M:%S
file = /var/log/secure
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/secure
log_group_name = ${EC2LogGroup}
[/var/log/audit/audit.log]
datetime_format =
file = /var/log/audit/audit.log
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/audit/audit.log
log_group_name = ${EC2LogGroup}
[/var/log/cron]
datetime_format = %b %d %H:%M:%S
file = /var/log/cron
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/cron
log_group_name = ${EC2LogGroup}
[/var/log/cfn-init.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/cfn-init.log
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/cfn-init.log
log_group_name = ${EC2LogGroup}
[/var/log/cfn-hup.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/cfn-hup.log
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/cfn-hup.log
log_group_name = ${EC2LogGroup}
[/var/log/cfn-init-cmd.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/cfn-init-cmd.log
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/cfn-init-cmd.log
log_group_name = ${EC2LogGroup}
[/var/log/cloud-init-output.log]
file = /var/log/cloud-init-output.log
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/cloud-init-output.log
log_group_name = ${EC2LogGroup}
[/var/log/amazon/ssm/amazon-ssm-agent.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/amazon/ssm/amazon-ssm-agent.log
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/amazon/ssm/amazon-ssm-agent.log
log_group_name = ${EC2LogGroup}
[/var/log/amazon/ssm/errors.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/amazon/ssm/errors.log
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/amazon/ssm/errors.log
log_group_name = ${EC2LogGroup}
[/var/log/maillog]
datetime_format = %b %d %H:%M:%S
file = /var/log/maillog
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/maillog
log_group_name = ${EC2LogGroup}
[/var/log/yum.log]
datetime_format = %b %d %H:%M:%S
file = /var/log/yum.log
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/yum.log
log_group_name = ${EC2LogGroup}
[/var/log/kube-proxy.log]
datetime_format =
file = /var/log/kube-proxy.log
log_stream_name = ${AWS::StackName}/workernode-{instance_id}/var/log/kube-proxy.log
log_group_name = ${EC2LogGroup}
mode: '000644'
owner: root
group: root
'/tmp/hardening_bootstrap.sh':
source: !Sub "https://${RuntimeAssetsBucketName}.s3.amazonaws.com/scripts/hardening_bootstrap.sh"
mode: "000550"
owner: root
group: root
authentication: S3AccessCreds
services:
sysvinit:
awslogsd:
enabled: "true"
ensureRunning: "true"
packages:
yum:
- awslogs
files:
- '/etc/awslogs/awslogs.conf'
- '/etc/awslogs/awscli.conf'
amazon-ssm-agent:
enabled: "true"
ensureRunning: "true"
packages:
yum:
- amazon-ssm-agent
postfix:
enabled: "false"
ensureRunning: "false"
commands:
hardening-bootstrap:
command: "./tmp/hardening_bootstrap.sh --tcp-forwarding false --x11-forwarding false"
Properties:
AssociatePublicIpAddress: false
IamInstanceProfile: !Ref NodeInstanceProfile
ImageId: !FindInMap [NodeAmiRegionMap, !Ref "AWS::Region", AmiId]
InstanceType: m5.xlarge
KeyName: !Ref KeyName
SecurityGroups:
- !Ref NodeSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
set -x
export PATH=$PATH:/usr/local/bin
pip install awscli --upgrade
easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource NodeLaunchConfig --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource NodeAutoScalingGroup --region ${AWS::Region}