-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path3rd-Bootstrap_AmazonLinux-1-HVM.sh
800 lines (620 loc) · 32 KB
/
3rd-Bootstrap_AmazonLinux-1-HVM.sh
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
#!/bin/bash -v
set -e -x
# Logger
exec > >(tee /var/log/user-data_3rd-bootstrap.log || logger -t user-data -s 2> /dev/console) 2>&1
#-------------------------------------------------------------------------------
# Set UserData Parameter
#-------------------------------------------------------------------------------
if [ -f /tmp/userdata-parameter ]; then
source /tmp/userdata-parameter
fi
if [[ -z "${Language}" || -z "${Timezone}" || -z "${VpcNetwork}" ]]; then
# Default Language
Language="en_US.UTF-8"
# Default Timezone
Timezone="Asia/Tokyo"
# Default VPC Network
VpcNetwork="IPv4"
fi
# echo
echo $Language
echo $Timezone
echo $VpcNetwork
#-------------------------------------------------------------------------------
# Parameter Settings
#-------------------------------------------------------------------------------
# Parameter Settings
CWAgentConfig="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/Config_AmazonCloudWatchAgent/AmazonCloudWatchAgent_AmazonLinux-1-HVM.json"
#-------------------------------------------------------------------------------
# Acquire unique information of Linux distribution
# - Amazon Linux 1
# https://aws.amazon.com/jp/amazon-linux-ami/
# https://aws.amazon.com/jp/releasenotes/
# https://aws.amazon.com/jp/amazon-linux-ami/faqs/
#
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/amazon-linux-ami-basics.html
#-------------------------------------------------------------------------------
# Cleanup repository information
yum clean all
# Show Linux Distribution/Distro information
if [ $(command -v lsb_release) ]; then
lsb_release -a
fi
# Show Linux System Information
uname -a
# Show Linux distribution release Information
cat /etc/os-release
cat /etc/system-release
cat /etc/image-id
# Default installation package [rpm command]
rpm -qa --qf="%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n" | sort > /tmp/command-log_rpm_installed-package.txt
# Default installation package [yum command]
yum list installed > /tmp/command-log_yum_installed-package.txt
# Default repository package [yum command]
yum list all > /tmp/command-log_yum_repository-package-list.txt
# Default repository package group [yum command]
yum groups list -v > /tmp/command-log_yum_repository-package-group-list.txt
# Special package information
yum --enablerepo="amzn-preview" list | grep amzn-preview
# upstartd service config [chkconfig command]
chkconfig --list > /tmp/command-log_chkconfig_list.txt
# Default repository list [yum command]
yum repolist all > /tmp/command-log_yum_repository-list.txt
# EPEL repository package [yum command]
yum --disablerepo="*" --enablerepo="epel" list available > /tmp/command-log_yum_repository-epel-package-list.txt
#-------------------------------------------------------------------------------
# Default Package Update
#-------------------------------------------------------------------------------
# yum repository metadata Clean up
yum --enablerepo="*" --verbose clean all
# Default Package Update
yum update -y
#-------------------------------------------------------------------------------
# Custom Package Installation
#-------------------------------------------------------------------------------
# Package Install Amazon Linux System Administration Tools (from Amazon Official Repository)
yum install -y acpid arptables_jf bc bcc collectl dstat dmidecode ebtables fio gdisk git hdparm jq kexec-tools lsof lzop iotop mlocate mtr nc net-snmp-utils nmap nvme-cli perf psmisc rsync sos strace sysstat tcpdump traceroute tree vim-enhanced yum-plugin-versionlock yum-utils wget
yum install -y aws-efs-utils cifs-utils nfs-utils nfs4-acl-tools
yum install -y iscsi-initiator-utils lsscsi
# Package Install Python 3 Runtime (from Amazon Official Repository)
yum install -y python36 python36-pip python36-setuptools python36-test python36-tools python36-virtualenv
# Package Install Amazon Linux System Administration Tools (from EPEL Repository)
yum --enablerepo="epel" install -y bash-completion iperf3 zstd
#-------------------------------------------------------------------------------
# Get AWS Instance MetaData Service (IMDS v1, v2)
# https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html
# https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
#-------------------------------------------------------------------------------
# Getting an Instance Metadata Service v2 (IMDS v2) token
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
if [ -n "$TOKEN" ]; then
#-----------------------------------------------------------------------
# Retrieving Metadata Using the Instance Metadata Service v2 (IMDS v2)
#-----------------------------------------------------------------------
# Instance MetaData
Az=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/meta-data/placement/availability-zone")
AzId=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/meta-data/placement/availability-zone-id")
Region=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/meta-data/placement/region")
InstanceId=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/meta-data/instance-id")
InstanceType=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/meta-data/instance-type")
PrivateIp=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/meta-data/local-ipv4")
AmiId=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/meta-data/ami-id")
# IAM Role & STS Information
if [ $(compgen -ac | sort | uniq | grep -x jq) ]; then
RoleArn=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/meta-data/iam/info" | jq -r '.InstanceProfileArn')
RoleName=$(echo $RoleArn | cut -d '/' -f 2)
fi
if [ -n "$RoleName" ]; then
StsCredential=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/meta-data/iam/security-credentials/$RoleName")
if [ $(compgen -ac | sort | uniq | grep -x jq) ]; then
StsAccessKeyId=$(echo $StsCredential | jq -r '.AccessKeyId')
StsSecretAccessKey=$(echo $StsCredential | jq -r '.SecretAccessKey')
StsToken=$(echo $StsCredential | jq -r '.Token')
fi
fi
# AWS Account ID
if [ $(compgen -ac | sort | uniq | grep -x jq) ]; then
AwsAccountId=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/dynamic/instance-identity/document" | jq -r '.accountId')
fi
else
#-----------------------------------------------------------------------
# Retrieving Metadata Using the Instance Metadata Service v1 (IMDS v1)
#-----------------------------------------------------------------------
# Instance MetaData
Az=$(curl -s "http://169.254.169.254/latest/meta-data/placement/availability-zone")
AzId=$(curl -s "http://169.254.169.254/latest/meta-data/placement/availability-zone-id")
Region=$(curl -s "http://169.254.169.254/latest/meta-data/placement/region")
InstanceId=$(curl -s "http://169.254.169.254/latest/meta-data/instance-id")
InstanceType=$(curl -s "http://169.254.169.254/latest/meta-data/instance-type")
PrivateIp=$(curl -s "http://169.254.169.254/latest/meta-data/local-ipv4")
AmiId=$(curl -s "http://169.254.169.254/latest/meta-data/ami-id")
# IAM Role & STS Information
if [ $(compgen -ac | sort | uniq | grep -x jq) ]; then
RoleArn=$(curl -s "http://169.254.169.254/latest/meta-data/iam/info" | jq -r '.InstanceProfileArn')
RoleName=$(echo $RoleArn | cut -d '/' -f 2)
fi
if [ -n "$RoleName" ]; then
StsCredential=$(curl -s "http://169.254.169.254/latest/meta-data/iam/security-credentials/$RoleName")
if [ $(compgen -ac | sort | uniq | grep -x jq) ]; then
StsAccessKeyId=$(echo $StsCredential | jq -r '.AccessKeyId')
StsSecretAccessKey=$(echo $StsCredential | jq -r '.SecretAccessKey')
StsToken=$(echo $StsCredential | jq -r '.Token')
fi
fi
# AWS Account ID
if [ $(compgen -ac | sort | uniq | grep -x jq) ]; then
AwsAccountId=$(curl -s "http://169.254.169.254/latest/dynamic/instance-identity/document" | jq -r '.accountId')
fi
fi
#-------------------------------------------------------------------------------
# Custom Package Installation [AWS-CLI v2]
# https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-linux.html
# [Linux Support Updates for AWS CLI v2]
# https://aws.amazon.com/jp/blogs/developer/linux-support-updates-for-aws-cli-v2/
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Package Uninstall AWS-CLI v1 Tools (from RPM Package)
if [ $(compgen -ac | sort | uniq | grep -x aws) ]; then
aws --version
which aws
if [ $(rpm -qa | grep awscli) ]; then
rpm -qi awscli
yum remove -y awscli
fi
fi
# Package download AWS-CLI v2 Tools (from Bundle Installer)
curl -sS "https://awscli.amazonaws.com/awscli-exe-linux-x86_64-2.17.51.zip" -o "/tmp/awscliv2.zip"
unzip -oq "/tmp/awscliv2.zip" -d /tmp/
# Package Install AWS-CLI v2 Tools (from Bundle Installer)
/tmp/aws/install -i "/opt/aws/awscli" -b "/usr/bin" --update
aws --version
# Configuration AWS-CLI tools
cat > /etc/bash_completion.d/aws_bash_completer << __EOF__
# Typically that would be added under one of the following paths:
# - /etc/bash_completion.d
# - /usr/local/etc/bash_completion.d
# - /usr/share/bash-completion/completions
complete -C aws_completer aws
__EOF__
# Setting AWS-CLI default Region & Output format
aws configure << __EOF__
${Region}
json
__EOF__
# Setting AWS-CLI Logging
aws configure set cli_history enabled
# Setting AWS-CLI Pager settings
aws configure set cli_pager ''
# Getting AWS-CLI default Region & Output format
aws configure list
cat ~/.aws/config
#------------------------------------------------------------------------------
# Getting information about AWS services
#------------------------------------------------------------------------------
# Get AWS Security Token Service (AWS STS) Information
if [ -n "$RoleName" ]; then
echo "# Get AWS Security Token Service (AWS STS) Information"
aws sts get-caller-identity --output json
fi
# Get AWS Region List
if [ -n "$RoleName" ]; then
echo "# Get AWS Region List"
aws ec2 describe-regions --region ${Region} > "/var/log/user-data_aws-cli_aws-services_describe-regions.txt"
fi
# Get Amazon EC2 Instance Type List
if [ -n "$RoleName" ]; then
echo "# Get Amazon EC2 Instance Type List"
aws ec2 describe-instance-types --query 'InstanceTypes[?Hypervisor==`nitro`]' --output json --region ${Region} > "/var/log/user-data_aws-cli_aws-services_describe-instance-types_nitro-hypervisor.txt"
aws ec2 describe-instance-types --query 'InstanceTypes[?Hypervisor==`xen`]' --output json --region ${Region} > "/var/log/user-data_aws-cli_aws-services_describe-instance-types_xen-hypervisor.txt"
fi
#------------------------------------------------------------------------------
# Getting information about Amazon EC2 Instance
#------------------------------------------------------------------------------
# Get Amazon EC2 Instance Information
if [ -n "$RoleName" ]; then
echo "# Get Amazon EC2 Instance Information"
aws ec2 describe-instances --instance-ids ${InstanceId} --output json --region ${Region} > "/var/log/user-data_aws-cli_amazon-ec2-instance_describe-instances.txt"
fi
# Get Amazon EC2 Instance Type Information
if [ -n "$RoleName" ]; then
echo "# Get Amazon EC2 Instance Type Information"
aws ec2 describe-instance-types --query "InstanceTypes[?InstanceType==\`${InstanceType}\`]" --output json --region ${Region} > "/var/log/user-data_aws-cli_amazon-ec2-instance_describe-instance-types.txt"
fi
# Get Amazon EC2 Instance attached EBS Volume Information
if [ -n "$RoleName" ]; then
echo "# Get Amazon EC2 Instance attached EBS Volume Information"
aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=${InstanceId} --output json --region ${Region} > "/var/log/user-data_aws-cli_amazon-ec2-instance_describe-volumes.txt"
fi
# Get Amazon EC2 Instance attached VPC Security Group Information
if [ -n "$RoleName" ]; then
echo "# Get Amazon EC2 Instance attached VPC Security Group Information"
aws ec2 describe-security-groups --group-ids $(aws ec2 describe-instances --instance-id ${InstanceId} --query "Reservations[].Instances[].SecurityGroups[].GroupId[]" --output text --region ${Region}) --output json --region ${Region} > "/var/log/user-data_aws-cli_amazon-ec2-instance_describe-security-groups.txt"
fi
# Get AMI information of this Amazon EC2 instance
if [ -n "$RoleName" ]; then
echo "# Get AMI information of this Amazon EC2 instance"
aws ec2 describe-images --image-ids ${AmiId} --output json --region ${Region} > "/var/log/user-data_aws-cli_amazon-ec2-instance_describe-images.txt"
fi
#------------------------------------------------------------------------------
# Getting information about Amazon EC2 Instance Attribute
# [Network Interface Performance Attribute]
#------------------------------------------------------------------------------
# - Summary of Networking and Storage Features
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html#instance-type-summary-table
# - ENA (Elastic Network Adapter)
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enhanced-networking-ena.html
# - ENA Express (Elastic Network Adapter Express)
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ena-express.html
# - Elastic Fabric Adapter (EFA)
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/efa.html
# - Single-root I/O virtualization (SR-IOV)
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/sriov-networking.html
#------------------------------------------------------------------------------
# Get EC2 Instance Attribute [Network Interface Performance Attribute]
if [ -n "$RoleName" ]; then
echo "# Get EC2 Instance Attribute [Network Interface Performance Attribute]"
# Get EC2 Instance Attribute [Network Interface Performance Attribute - ENA (Elastic Network Adapter)]
if [[ $(aws ec2 describe-instance-types --filters "Name=instance-type,Values=${InstanceType}" --query "InstanceTypes[].NetworkInfo.EnaSupport" --output text --region ${Region}) == "required" ]]; then
echo "EnaSupport is available for $InstanceType"
echo "# Get EC2 Instance Attribute [Network Interface Performance Attribute - ENA (Elastic Network Adapter)]"
aws ec2 describe-instance-types --filters "Name=instance-type,Values=${InstanceType}" --query "InstanceTypes[].NetworkInfo" --output json --region ${Region} > "/var/log/user-data_aws-cli_amazon-ec2-instance_describe-instance-types_NetworkInfo_ENA.txt"
# Get Linux Kernel Module(modinfo ena)
echo "# Get Linux Kernel Module(modinfo ena)"
if [ $(lsmod | awk '{print $1}' | grep -x ena) ]; then
modinfo ena
if [ $(command -v nmcli) ]; then
# Get Network Interface Information
ethtool -S $(nmcli -t -f DEVICE device | grep -v lo)
ethtool -c $(nmcli -t -f DEVICE device | grep -v lo)
fi
fi
fi
# Get EC2 Instance Attribute [Network Interface Performance Attribute - ENA Express (Elastic Network Adapter Express)]
if [[ $(aws ec2 describe-instance-types --filters "Name=instance-type,Values=${InstanceType}" --query "InstanceTypes[].NetworkInfo.EnaSrdSupported" --output text --region ${Region}) == "true" ]]; then
echo "EnaSrdSupported is available for $InstanceType"
echo "# Get EC2 Instance Attribute [Network Interface Performance Attribute - ENA Express (Elastic Network Adapter Express)]"
aws ec2 describe-instance-types --filters "Name=instance-type,Values=${InstanceType}" --query "InstanceTypes[].NetworkInfo" --output json --region ${Region} > "/var/log/user-data_aws-cli_amazon-ec2-instance_describe-instance-types_NetworkInfo_ENA_Express.txt"
# Get Linux Kernel Module(modinfo ena)
echo "# Get Linux Kernel Module(modinfo ena)"
if [ $(lsmod | awk '{print $1}' | grep -x ena) ]; then
modinfo ena
if [ $(command -v nmcli) ]; then
# Get Network Interface Information
ethtool -S $(nmcli -t -f DEVICE device | grep -v lo)
ethtool -c $(nmcli -t -f DEVICE device | grep -v lo)
fi
fi
fi
# Get EC2 Instance Attribute [Network Interface Performance Attribute - Elastic Fabric Adapter (EFA)]
if [[ $(aws ec2 describe-instance-types --filters "Name=instance-type,Values=${InstanceType}" --query "InstanceTypes[].NetworkInfo.EfaSupported" --output text --region ${Region}) == "true" ]]; then
echo "EfaSupported is available for $InstanceType"
echo "# Get EC2 Instance Attribute [Network Interface Performance Attribute - Elastic Fabric Adapter (EFA)]"
aws ec2 describe-instance-types --filters "Name=instance-type,Values=${InstanceType}" --query "InstanceTypes[].NetworkInfo" --output json --region ${Region} > "/var/log/user-data_aws-cli_amazon-ec2-instance_describe-instance-types_NetworkInfo_EFA.txt"
fi
# Get EC2 Instance Attribute [Network Interface Performance Attribute - Single-root I/O virtualization (SR-IOV)]
if [[ $(aws ec2 describe-instance-attribute --instance-id $InstanceId --attribute sriovNetSupport --query 'SriovNetSupport.Value' --output text --region ${Region}) == "simple" ]]; then
echo "SriovNetSupport is available for $InstanceType"
echo "# Get EC2 Instance Attribute [Network Interface Performance Attribute - Single-root I/O virtualization (SR-IOV)]"
aws ec2 describe-instance-attribute --instance-id $InstanceId --attribute sriovNetSupport --output json --region ${Region} > "/var/log/user-data_aws-cli_amazon-ec2-instance_describe-instance-types_NetworkInfo_SR-IOV.txt"
# Get Linux Kernel Module(modinfo ixgbevf)
echo "# Get Linux Kernel Module(modinfo ixgbevf)"
if [ $(lsmod | awk '{print $1}' | grep -x ixgbevf) ]; then
modinfo ixgbevf
if [ $(command -v nmcli) ]; then
# Get Network Interface Information
ethtool -S $(nmcli -t -f DEVICE device | grep -v lo)
ethtool -c $(nmcli -t -f DEVICE device | grep -v lo)
fi
fi
fi
fi
#------------------------------------------------------------------------------
# Getting information about Amazon EC2 Instance Attribute
# [Storage Interface Performance Attribute]
#------------------------------------------------------------------------------
# - Summary of Networking and Storage Features
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html#instance-type-summary-table
# - EBS Optimized Instance
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSPerformance.html
# - Nitro-based Instances
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html#ec2-nitro-instances
# - Amazon EBS and NVMe Volumes
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html
# - SSD Instance Store Volumes
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ssd-instance-store.html
#------------------------------------------------------------------------------
# Get EC2 Instance Attribute [Storage Interface Performance Attribute]
if [ -n "$RoleName" ]; then
echo "# Get EC2 Instance Attribute [Storage Interface Performance Attribute]"
# Get EC2 Instance Attribute [Storage Interface Performance Attribute - EBS Optimized Instance]
if [[ $(aws ec2 describe-instance-types --filters "Name=instance-type,Values=${InstanceType}" --query "InstanceTypes[].EbsInfo.EbsOptimizedSupport" --output text --region ${Region}) == "default" ]]; then
echo "EbsOptimizedSupport is available for $InstanceType"
echo "# Get EC2 Instance Attribute [Storage Interface Performance Attribute - EBS Optimized Instance]"
aws ec2 describe-instance-types --filters "Name=instance-type,Values=${InstanceType}" --query "InstanceTypes[].EbsInfo" --output json --region ${Region} > "/var/log/user-data_aws-cli_amazon-ec2-instance_describe-instance-types_EbsInfo.txt"
fi
# Get EC2 Instance Attribute [Storage Interface Performance Attribute - Amazon EBS and NVMe Volumes]
if [[ $(aws ec2 describe-instance-types --filters "Name=instance-type,Values=${InstanceType}" --query "InstanceTypes[].EbsInfo.NvmeSupport" --output text --region ${Region}) == "required" ]]; then
echo "NvmeSupport is available for $InstanceType"
# Get Linux Kernel Module(modinfo nvme)
echo "# Get Linux Kernel Module(modinfo nvme)"
if [ $(lsmod | awk '{print $1}' | grep -x nvme) ]; then
modinfo nvme
fi
# Get NVMe Device(nvme list)
# http://www.spdk.io/doc/nvme-cli.html
# https://github.com/linux-nvme/nvme-cli
if [ $(command -v nvme) ]; then
echo "# Get NVMe Device(nvme list)"
nvme list
fi
fi
fi
# Get PCI-Express Device(lspci -v)
if [ $(command -v lspci) ]; then
echo "# Get PCI-Express Device(lspci -v)"
lspci -v
fi
# Get Disk[MountPoint] Information (lsblk -a)
if [ $(command -v lsblk) ]; then
echo "# Get Disk[MountPoint] Information (lsblk -a)"
lsblk -a
fi
# Get Linux Block Device Read-Ahead Value(blockdev --report)
if [ $(command -v blockdev) ]; then
echo "# Get Linux Block Device Read-Ahead Value(blockdev --report)"
blockdev --report
fi
#------------------------------------------------------------------------------
# Getting information about Amazon Machine Image
#------------------------------------------------------------------------------
# Get Amazon Machine Image Information
if [ -n "$RoleName" ]; then
# Get the latest AMI information of the OS type of this EC2 instance from Public AMI
echo "# Get Amazon Machine Image Information"
NewestAmiInfo=$(aws ec2 describe-images --owner amazon --filter "Name=name,Values=amzn-ami-hvm-*-gp2" "Name=virtualization-type,Values=hvm" "Name=architecture,Values=x86_64" --query 'sort_by(Images[].{YMD:CreationDate,Name:Name,ImageId:ImageId},&YMD)|reverse(@)|[0]' --output json --region ${Region})
NewestAmiId=$(echo $NewestAmiInfo| jq -r '.ImageId')
aws ec2 describe-images --image-ids ${NewestAmiId} --output json --region ${Region} > "/var/log/user-data_aws-cli_amazon-machine-images_describe-describe-images.txt"
fi
#-------------------------------------------------------------------------------
# Custom Package Update [AWS Systems Manager agent (aka SSM agent)]
# http://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/sysman-install-ssm-agent.html
# https://github.com/aws/amazon-ssm-agent
#-------------------------------------------------------------------------------
yum localinstall --nogpgcheck -y "https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm"
rpm -qi amazon-ssm-agent
status amazon-ssm-agent
/sbin/restart amazon-ssm-agent
status amazon-ssm-agent
ssm-cli get-instance-information
#-------------------------------------------------------------------------------
# Custom Package Install [Amazon CloudWatch Agent]
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/download-cloudwatch-agent-commandline.html
# https://github.com/aws/amazon-cloudwatch-agent
#-------------------------------------------------------------------------------
yum localinstall --nogpgcheck -y "https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm"
# Package Information
rpm -qi amazon-cloudwatch-agent
cat /opt/aws/amazon-cloudwatch-agent/bin/CWAGENT_VERSION
cat /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml
# Configure Amazon CloudWatch Agent software (Monitor settings)
curl -sS ${CWAgentConfig} -o "/tmp/config.json"
cat "/tmp/config.json"
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/tmp/config.json -s
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a stop
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a start
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status
# View Amazon CloudWatch Agent config files
cat /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml
cat /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml
#-------------------------------------------------------------------------------
# Custom Package Installation [Amazon EC2 Rescue for Linux (ec2rl)]
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Linux-Server-EC2Rescue.html
# https://github.com/awslabs/aws-ec2rescue-linux
#-------------------------------------------------------------------------------
# Package Amazon EC2 Administration Tools (from S3 Bucket)
curl -sS "https://s3.amazonaws.com/ec2rescuelinux/ec2rl-bundled.tgz" -o "/tmp/ec2rl-bundled.tgz"
mkdir -p "/opt/aws"
rm -rf /opt/aws/ec2rl*
tar -xzf "/tmp/ec2rl-bundled.tgz" -C "/opt/aws"
mv --force /opt/aws/ec2rl* "/opt/aws/ec2rl"
cat > /etc/profile.d/ec2rl.sh << __EOF__
export PATH=\$PATH:/opt/aws/ec2rl
__EOF__
source /etc/profile.d/ec2rl.sh
# Check Version
/opt/aws/ec2rl/ec2rl version
/opt/aws/ec2rl/ec2rl list
# Required Software Package
/opt/aws/ec2rl/ec2rl software-check
# Diagnosis [dig modules]
# /opt/aws/ec2rl/ec2rl run --only-modules=dig --domain=amazon.com
#-------------------------------------------------------------------------------
# Custom Package Installation [Ansible]
#-------------------------------------------------------------------------------
# Package Install Amazon Linux System Administration Tools (from EPEL Repository)
yum --enablerepo="epel" install -y ansible ansible-doc
ansible --version
ansible localhost -m setup
# Package Install Amazon Linux System Administration Tools (from PIP)
# pip install ansible
# /usr/local/bin/ansible --version
# /usr/local/bin/ansible localhost -m setup
#-------------------------------------------------------------------------------
# Custom Package Clean up
#-------------------------------------------------------------------------------
yum clean all
#-------------------------------------------------------------------------------
# System information collection
#-------------------------------------------------------------------------------
# CPU Information [cat /proc/cpuinfo]
cat /proc/cpuinfo
# CPU Information [lscpu]
lscpu
lscpu --extended
# Memory Information [cat /proc/meminfo]
cat /proc/meminfo
# Memory Information [free]
free
# Disk Information(Partition) [parted -l]
parted -l
# Disk Information(MountPoint) [lsblk -f]
lsblk -f
# Disk Information(File System) [df -khT]
df -khT
# Network Information(Network Interface) [ip addr show]
ip addr show
# Network Information(Routing Table) [ip route show]
ip route show
# Network Information(Firewall Service) [chkconfig --list iptables]
chkconfig --list iptables
# Network Information(Firewall Service) [service ip6tables stop]
chkconfig --list ip6tables
#-------------------------------------------------------------------------------
# Configure Amazon Time Sync Service
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html
#-------------------------------------------------------------------------------
# Replace NTP Client software (Uninstall ntpd Package)
if [ $(chkconfig --list | awk '{print $1}' | grep -x ntpd) ]; then
chkconfig --list ntpd
service ntpd stop
fi
yum remove -y ntp*
# Replace NTP Client software (Install chrony Package)
yum install -y chrony
# Configure NTP Client software (Start Daemon chronyd)
chkconfig --list chronyd
chkconfig chronyd on
chkconfig --list chronyd
# Contents of the configuration file
ChronyConfigFile="/etc/chrony.conf"
cat ${ChronyConfigFile}
# Configure NTP Client software (Enable log settings in Chrony configuration file)
sed -i 's/#log measurements statistics tracking/log measurements statistics tracking/g' ${ChronyConfigFile}
# Configure NTP Client software (Activate Amazon Time Sync Service settings in the Chrony configuration file)
if [ $(cat ${ChronyConfigFile} | grep -ie "169.254.169.123" | wc -l) = "0" ]; then
echo "NTP server (169.254.169.123) for Amazon Time Sync Service is not configured in the configuration file."
# Configure the NTP server (169.254.169.123) for Amazon Time Sync Service in the configuration file.
if [ $(cat ${ChronyConfigFile} | grep -ie ".pool.ntp.org" | wc -l) = "0" ]; then
# Variables (for editing the Chrony configuration file)
VAR_CHRONY_NUM="1"
# Change settings (Chrony configuration file)
sed -i "${VAR_CHRONY_NUM}"'s/^/# Use the Amazon Time Sync Service.\nserver 169.254.169.123 prefer iburst minpoll 4 maxpoll 4\n\n/' ${ChronyConfigFile}
else
# Variables (for editing the Chrony configuration file)
VAR_CHRONY_STR=$(cat ${ChronyConfigFile} | grep -ie "pool" -ie "server" | tail -n 1)
VAR_CHRONY_NUM=`expr $(grep -e "$VAR_CHRONY_STR" -n ${ChronyConfigFile} | sed -e 's/:.*//g') + 1`
# Change settings (Chrony configuration file)
sed -i "${VAR_CHRONY_NUM}"'s/^/\n# Use the Amazon Time Sync Service.\nserver 169.254.169.123 prefer iburst minpoll 4 maxpoll 4\n\n/' ${ChronyConfigFile}
fi
# Contents of the configuration file
cat ${ChronyConfigFile}
fi
# Configure NTP Client software (Check the status of time synchronization by Chrony)
service chronyd restart
service chronyd status
if [ $(command -v chronyc) ]; then
sleep 3
chronyc tracking
sleep 3
chronyc sources -v
sleep 3
chronyc sourcestats -v
fi
#-------------------------------------------------------------------------------
# System Setting
#-------------------------------------------------------------------------------
# Ephemeral-Disk Auto Mount Disabled (cloud-init)
sed -i '/ephemeral0/d' /etc/cloud/cloud.cfg
# Firewall Service Disabled (iptables/ip6tables)
service iptables stop
chkconfig --list iptables
chkconfig iptables off
chkconfig --list iptables
service ip6tables stop
chkconfig --list ip6tables
chkconfig ip6tables off
chkconfig --list ip6tables
# Setting SystemClock and Timezone
if [ "${Timezone}" = "Asia/Tokyo" ]; then
echo "# Setting SystemClock and Timezone -> $Timezone"
# Setting SystemClock
cat /dev/null > /etc/sysconfig/clock
echo 'ZONE="Asia/Tokyo"' >> /etc/sysconfig/clock
echo 'UTC=false' >> /etc/sysconfig/clock
cat /etc/sysconfig/clock
# Setting TimeZone
date
/bin/cp -fp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
date
elif [ "${Timezone}" = "UTC" ]; then
echo "# Setting SystemClock and Timezone -> $Timezone"
# Setting SystemClock
cat /dev/null > /etc/sysconfig/clock
echo 'ZONE="UTC"' >> /etc/sysconfig/clock
echo 'UTC=true' >> /etc/sysconfig/clock
cat /etc/sysconfig/clock
# Setting TimeZone
date
/bin/cp -fp /usr/share/zoneinfo/UTC /etc/localtime
date
else
echo "# Default SystemClock and Timezone"
cat /etc/sysconfig/clock
cat /etc/localtime
fi
# Setting System Language
if [ "${Language}" = "ja_JP.UTF-8" ]; then
echo "# Setting System Language -> $Language"
cat /dev/null > /etc/sysconfig/i18n
echo 'LANG=ja_JP.utf8' >> /etc/sysconfig/i18n
cat /etc/sysconfig/i18n
elif [ "${Language}" = "en_US.UTF-8" ]; then
echo "# Setting System Language -> $Language"
cat /dev/null > /etc/sysconfig/i18n
echo 'LANG=en_US.utf8' >> /etc/sysconfig/i18n
cat /etc/sysconfig/i18n
else
echo "# Default Language"
cat /etc/sysconfig/i18n
fi
# Setting IP Protocol Stack (IPv4 Only) or (IPv4/IPv6 Dual stack)
if [ "${VpcNetwork}" = "IPv4" ]; then
echo "# Setting IP Protocol Stack -> $VpcNetwork"
# Disable IPv6 Kernel Module
echo "options ipv6 disable=1" >> /etc/modprobe.d/ipv6.conf
# Disable IPv6 Kernel Parameter
sysctl -a
DisableIPv6Conf="/etc/sysctl.d/99-ipv6-disable.conf"
cat /dev/null > $DisableIPv6Conf
echo '# Custom sysctl Parameter for ipv6 disable' >> $DisableIPv6Conf
echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> $DisableIPv6Conf
echo 'net.ipv6.conf.default.disable_ipv6 = 1' >> $DisableIPv6Conf
sysctl -p
sysctl -a | grep -ie "local_port" -ie "ipv6" | sort
elif [ "${VpcNetwork}" = "IPv6" ]; then
echo "# Show IP Protocol Stack -> $VpcNetwork"
echo "# Show IPv6 Network Interface Address"
ifconfig
echo "# Show IPv6 Kernel Module"
lsmod | awk '{print $1}' | grep ipv6
echo "# Show Network Listen Address and report"
netstat -an -A inet6
echo "# Show Network Routing Table"
netstat -r -A inet6
else
echo "# Default IP Protocol Stack"
echo "# Show IPv6 Network Interface Address"
ifconfig
echo "# Show IPv6 Kernel Module"
lsmod | awk '{print $1}' | grep ipv6
echo "# Show Network Listen Address and report"
netstat -an -A inet6
echo "# Show Network Routing Table"
netstat -r -A inet6
fi
#-------------------------------------------------------------------------------
# Reboot
#-------------------------------------------------------------------------------
# Instance Reboot
reboot