-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
472 lines (472 loc) · 17.1 KB
/
template.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
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
---
AWSTemplateFormatVersion: '2010-09-09'
Description: Creates one Logstash server, two SNMP agents on EC2 instances and a Secret
in AWS Secrets Manager. **WARNING** This template creates Amazon EC2 instances,
Amazon CloudWatch custom metrics and related resources. You will be billed for the
AWS resources used if you create a stack from this template.
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Network configuration
Parameters:
- MyVPC
- MySubnet
- Label:
default: Amazon EC2 configuration
Parameters:
- LogstashInstanceType
- SNMPInstanceType
- LatestAmiId
- RepoLocation
- Label:
default: Troubleshooting and connectivity
Parameters:
- SSHLocation
- MyKeyPair
ParameterLabels:
MyVPC:
default: Deployment VPC
MySubnet:
default: Deployment subnet with internet access
LatestAmiId:
default: Amazon Linux 2 AMI ID
RepoLocation:
default: GitHub repository URL
MyKeyPair:
default: EC2 Key pair
LogstashInstanceType:
default: Instance type for Logstash
SNMPInstanceType:
default: Instance type for SNMP agents
SSHLocation:
default: SSH Location
Parameters:
MyVPC:
Description: Select VPC with internet access
Type: AWS::EC2::VPC::Id
MySubnet:
Description: Select only one subnet with Internet access
Type: List<AWS::EC2::Subnet::Id>
LatestAmiId:
Description: Latest Amazon Linux 2 image from SSM Parameter Store
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
RepoLocation:
Description: Archive containing configuration and settings
Type: String
Default: https://github.com/aws-samples/amazon-cloudwatch-snmp-monitoring-with-logstash/tarball/main
MyKeyPair:
Description: Amazon EC2 Key Pair
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair
LogstashInstanceType:
AllowedValues:
- t3.small
- t3.medium
- t3.large
- t3.xlarge
- t3.2xlarge
Default: t3.small
Description: Select instance size based to Logstash JVM size. Keep defaults for
demo.
Type: String
SNMPInstanceType:
AllowedValues:
- t3.nano
- t3.small
- t3.medium
- t3.large
- t3.xlarge
- t3.2xlarge
Default: t3.nano
Description: Amazon EC2 instance type for the SNMP simulator instances
Type: String
SSHLocation:
Description: The IP address range that can be used to SSH to the EC2 instances
Type: String
MinLength: '9'
MaxLength: '18'
Default: 84.255.0.0/16
AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
Resources:
SNMPSecret:
Type: AWS::SecretsManager::Secret
Properties:
Name: SNMPSecret
Description: my SNMPv3 secret
GenerateSecretString:
SecretStringTemplate: '{"username": "readonlyuser"}'
GenerateStringKey: password
PasswordLength: 8
ExcludePunctuation: true
Tags:
- Key: AppName
Value: SNMPdemo
LogstashRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: /
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/CloudWatchAgentServerPolicy"
SecretsManagerPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: SecretsManagerPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- secretsmanager:GetSecretValue
Resource: !Sub '${SNMPSecret}'
Roles:
- !Ref 'LogstashRole'
- !Ref 'SNMPRole'
LogstashInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref 'LogstashRole'
SNMPRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: /
SNMPInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref 'SNMPRole'
LogstashSG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enables SSH access
VpcId: !Ref 'MyVPC'
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref 'SSHLocation'
Description: SSH access locked down to user CIDR
SecurityGroupEgress:
- IpProtocol: '-1'
CidrIp: 0.0.0.0/0
Description: Allow all outbound traffic
SNMPSG:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enables SNMP Poll via port 161 locked down to the Logstash
server + SSH access
VpcId: !Ref 'MyVPC'
SecurityGroupIngress:
- IpProtocol: udp
FromPort: 161
ToPort: 161
SourceSecurityGroupId: !GetAtt 'LogstashSG.GroupId'
Description: Allows SNMP poll from Logstash instance
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref 'SSHLocation'
Description: SSH access locked down to user CIDR
SecurityGroupEgress:
- IpProtocol: '-1'
CidrIp: 0.0.0.0/0
Description: Allow all outbound traffic
LogstashInstance:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
configSets:
InstallAndRun:
- Prepare
- Install
Prepare:
commands:
01_import_logstash_yum:
command: rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
sources:
/tmp/repo: !Ref 'RepoLocation'
files:
/etc/yum.repos.d/logstash.repo:
content: "[logstash-7.x]\nname=Elastic repository for 7.x packages\n\
baseurl=https://artifacts.elastic.co/packages/7.x/yum\ngpgcheck=1\n\
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch\nenabled=1\n\
autorefresh=1\ntype=rpm-md \n"
mode: '000600'
owner: ec2-user
group: ec2-user
Install:
packages:
yum:
jq: []
git: []
logstash: []
files:
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
interval=1
mode: '000400'
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.LogstashInstance.Metadata.AWS::CloudFormation::Init
action=../../opt/aws/bin/cfn-init --stack ${AWS::StackName} --resource LogstashInstance --region ${AWS::Region}
runas=root
commands:
01_copy_config:
command: cp *.conf /etc/logstash/conf.d/
cwd: /tmp/repo/conf/
02_copy_settings:
command: cp pipelines.yml jvm.options logstash.yml /etc/logstash/
cwd: /tmp/repo/settings/
03_create_logstash_keystore:
command: echo y | bin/logstash-keystore --path.settings /etc/logstash
create
cwd: /usr/share/logstash/
04_update_SNMP_targets:
command: !Sub 'sed -i -e ''s/111.111.111.111/${SNMPInstance1.PrivateIp}/g''
-e ''s/222.222.222.222/${SNMPInstance2.PrivateIp}/g'' snmp.conf'
cwd: /etc/logstash/conf.d/
05_update_aws_region:
command: !Sub 'sed -i ''s/REGION/${AWS::Region}/g'' cloudwatch.conf'
cwd: /etc/logstash/conf.d/
06_add_user_keystore:
command: !Sub 'aws secretsmanager get-secret-value --region ${AWS::Region}
--secret-id SNMPSecret --query SecretString --output text
| jq .username | tr -d ''"'' | bin/logstash-keystore --path.settings
/etc/logstash add SNMP_USER'
cwd: /usr/share/logstash/
07_add_pwd_keystore:
command: !Sub 'aws secretsmanager get-secret-value --region ${AWS::Region}
--secret-id SNMPSecret --query SecretString --output text
| jq .password | tr -d ''"'' | bin/logstash-keystore --path.settings
/etc/logstash add SNMP_PWD'
cwd: /usr/share/logstash/
services:
sysvinit:
logstash:
enabled: 'true'
ensureRunning: 'true'
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
DependsOn: SNMPSecret
Properties:
ImageId: !Ref 'LatestAmiId'
InstanceType: !Ref 'LogstashInstanceType'
KeyName: !Ref 'MyKeyPair'
IamInstanceProfile: !Ref 'LogstashInstanceProfile'
SubnetId: !Select
- 0
- !Ref 'MySubnet'
SecurityGroupIds:
- !GetAtt 'LogstashSG.GroupId'
Tags:
- Key: Name
Value: Logstash Server
UserData: !Base64
Fn::Sub: "#!/bin/bash -xe\nyum update -y aws-cfn-bootstrap\n/opt/aws/bin/cfn-init\
\ -v --stack ${AWS::StackName} --resource LogstashInstance --configsets\
\ InstallAndRun --region ${AWS::Region}\n/opt/aws/bin/cfn-signal -e $? --stack\
\ ${AWS::StackName} --resource LogstashInstance --region ${AWS::Region}\
\ \n"
CreationPolicy:
ResourceSignal:
Timeout: PT10M
SNMPInstance1:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
configSets:
InstallAndRun:
- Install
Install:
packages:
yum:
net-snmp: []
net-snmp-utils: []
net-snmp-devel: []
jq: []
files:
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
interval=1
mode: 256
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.SNMPInstance1.Metadata.AWS::CloudFormation::Init
action=../../opt/aws/bin/cfn-init --stack ${AWS::StackName} --resource SNMPInstance1 --region ${AWS::Region}
runas=root
commands:
01_create_SNMPv3_user:
command: !Sub 'SECRET=$(aws secretsmanager get-secret-value --region
${AWS::Region} --secret-id SNMPSecret --query SecretString --output
text) ; SNMPUSER=$(echo $SECRET | jq .username | tr -d ''"'' )
;SNMPPWD=$(echo $SECRET | jq .password | tr -d ''"'' ) ;net-snmp-config
--create-snmpv3-user -ro -a MD5 -A $SNMPPWD $SNMPUSER >/dev/null'
services:
sysvinit:
snmpd:
enabled: 'true'
ensureRunning: 'true'
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
DependsOn: SNMPSecret
Properties:
ImageId: !Ref 'LatestAmiId'
InstanceType: !Ref 'SNMPInstanceType'
KeyName: !Ref 'MyKeyPair'
IamInstanceProfile: !Ref 'SNMPInstanceProfile'
SubnetId: !Select
- 0
- !Ref 'MySubnet'
SecurityGroupIds:
- !GetAtt 'SNMPSG.GroupId'
Tags:
- Key: Name
Value: SNMP Agent 1
UserData: !Base64
Fn::Sub: |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource SNMPInstance1 --configsets InstallAndRun --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource SNMPInstance1 --region ${AWS::Region}
CreationPolicy:
ResourceSignal:
Timeout: PT10M
SNMPInstance2:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
configSets:
InstallAndRun:
- Install
Install:
packages:
yum:
net-snmp: []
net-snmp-utils: []
net-snmp-devel: []
jq: []
files:
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
interval=1
mode: 256
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.SNMPInstance1.Metadata.AWS::CloudFormation::Init
action=../../opt/aws/bin/cfn-init --stack ${AWS::StackName} --resource SNMPInstance1 --region ${AWS::Region}
runas=root
commands:
01_create_SNMPv3_user:
command: !Sub 'SECRET=$(aws secretsmanager get-secret-value --region
${AWS::Region} --secret-id SNMPSecret --query SecretString --output
text) ; SNMPUSER=$(echo $SECRET | jq .username | tr -d ''"'' )
;SNMPPWD=$(echo $SECRET | jq .password | tr -d ''"'' ) ;net-snmp-config
--create-snmpv3-user -ro -a MD5 -A $SNMPPWD $SNMPUSER >/dev/null'
services:
sysvinit:
snmpd:
enabled: 'true'
ensureRunning: 'true'
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
DependsOn: SNMPSecret
Properties:
ImageId: !Ref 'LatestAmiId'
InstanceType: !Ref 'SNMPInstanceType'
KeyName: !Ref 'MyKeyPair'
IamInstanceProfile: !Ref 'SNMPInstanceProfile'
SubnetId: !Select
- 0
- !Ref 'MySubnet'
SecurityGroupIds:
- !GetAtt 'SNMPSG.GroupId'
Tags:
- Key: Name
Value: SNMP Agent 2
UserData: !Base64
Fn::Sub: |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource SNMPInstance2 --configsets InstallAndRun --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource SNMPInstance2 --region ${AWS::Region}
CreationPolicy:
ResourceSignal:
Timeout: PT10M
Outputs:
SNMPAgent1:
Description: Private IP address
Value: !GetAtt 'SNMPInstance1.PrivateIp'
SNMPAgent2:
Description: Private IP address
Value: !GetAtt 'SNMPInstance2.PrivateIp'