forked from usui-tk/amazon-ec2-userdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3rd-Bootstrap_AlmaLinux-v8-HVM.sh
1058 lines (810 loc) · 38.2 KB
/
3rd-Bootstrap_AlmaLinux-v8-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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/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_AlmaLinux-v8-HVM.json"
#-------------------------------------------------------------------------------
# Acquire unique information of Linux distribution
# - AlmaLinux 8
#
# https://almalinux.org/
# https://wiki.almalinux.org/
#
# https://wiki.almalinux.org/cloud/AWS.html
# https://aws.amazon.com/marketplace/pp/prodview-mku4y3g4sjrye
#
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/
#-------------------------------------------------------------------------------
# Cleanup repository information
dnf --enablerepo="*" --verbose 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/almalinux-release
cat /etc/redhat-release
# Default installation package [rpm command]
rpm -qa --qf="%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n" | sort > /tmp/command-log_rpm_installed-package.txt
# Default installation package [dnf command]
dnf list installed > /tmp/command-log_dnf_installed-package.txt
# Default repository package [dnf command]
dnf list all > /tmp/command-log_dnf_repository-package-list.txt
# Default repository package group [dnf command]
dnf group list -v > /tmp/command-log_dnf_repository-package-group-list.txt
# Default repository list [dnf command]
dnf repolist all > /tmp/command-log_dnf_repository-list.txt
# Default repository module [dnf command]
dnf module list > /tmp/command-log_dnf_module-list.txt
# systemd unit files
systemctl list-unit-files --all --no-pager > /tmp/command-log_systemctl_list-unit-files.txt
# systemd service config
systemctl list-units --type=service --all --no-pager > /tmp/command-log_systemctl_list-service-config.txt
#-------------------------------------------------------------------------------
# Default Package Update
#-------------------------------------------------------------------------------
# Yum Repositories (AlmaLinux 8)
# https://mirrors.almalinux.org/
#-------------------------------------------------------------------------------
# Cleanup repository information and Update dnf tools
dnf --enablerepo="*" --verbose clean all
dnf update -y dnf dnf-data
# Checking repository information
dnf repolist all
dnf module list
# Package Install AlmaLinux yum repository Files (from AlmaLinux Community Repository)
find /etc/yum.repos.d/
dnf search release
dnf install -y almalinux-release
dnf --enablerepo="*" --verbose clean all
find /etc/yum.repos.d/
# Cleanup repository information
dnf --enablerepo="*" --verbose clean all
# Checking repository information
dnf repolist all
dnf module list
# Get Dnf/Yum Repository List (Exclude Dnf/Yum repository related to "beta, debug, source, test)
repolist=$(dnf repolist all --quiet | grep -ie "enabled" -ie "disabled" | grep -ve "beta" -ve "debug" -ve "source" -ve "test" | awk '{print $1}' | awk '{ sub("/.*$",""); print $0; }' | sort)
# Enable Dnf/Yum Repository Data from AlmaLinux Community Repository
for repo in $repolist
do
echo "[Target repository Name (Enable dnf/yum repository)] :" $repo
dnf config-manager --set-enabled ${repo}
sleep 3
done
# Checking repository information
dnf repolist all
dnf module list
# Checking repository information (repository configuration file)
find /etc/yum.repos.d -type f -print | xargs egrep '^\[|enabled'
# Cleanup repository information
dnf --enablerepo="*" --verbose clean all
# Default Package Update
dnf update -y
#-------------------------------------------------------------------------------
# Custom Package Installation
#-------------------------------------------------------------------------------
# Package Install AlmaLinux System Administration Tools (from AlmaLinux Community Repository)
dnf install -y abrt abrt-cli acpid arptables bash-completion bc bcc bcc-tools bind-utils blktrace bpftool bpftrace crash-trace-command crypto-policies curl dnf-data dnf-plugins-core dnf-utils dstat ebtables ethtool expect fio gdisk git gnutls-utils hdparm intltool iotop ipcalc iperf3 iproute-tc ipset iptraf-ng iptraf-ng jq kexec-tools libicu linuxptp lsof lvm2 lzop man-pages mc mcelog mdadm mlocate mtr nc ncompress net-snmp-utils net-tools nftables nmap nmap-ncat nmstate numactl nvme-cli nvmetcli parted patchutils pmempool psacct psmisc python3-dnf-plugin-versionlock rsync smartmontools sos stalld strace symlinks sysfsutils sysstat tcpdump time tlog tmpwatch traceroute tree tzdata unzip usermode util-linux util-linux-user vdo vim-enhanced wget wireshark-cli xfsdump xfsprogs yum-utils zip zsh
dnf install -y cifs-utils nfs-utils nfs4-acl-tools
dnf install -y iscsi-initiator-utils lsscsi sg3_utils stratisd stratis-cli
dnf install -y "selinux-policy*" checkpolicy policycoreutils policycoreutils-python-utils policycoreutils-restorecond setools-console setools-console-analyses setroubleshoot-server udica
dnf install -y pcp pcp-export-pcp2json pcp-manager "pcp-pmda*" pcp-selinux pcp-system-tools pcp-zeroconf
# Package Install AlmaLinux support tools (from AlmaLinux Community Repository)
dnf install -y redhat-lsb-core
#-------------------------------------------------------------------------------
# Custom Package Installation [kernel live-patching tools]
# https://access.redhat.com/solutions/2206511
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/managing_monitoring_and_updating_the_kernel/applying-patches-with-kernel-live-patching_managing-monitoring-and-updating-the-kernel
#-------------------------------------------------------------------------------
# Package Install AlmaLinux kernel live-patching tools (from AlmaLinux Community Repository)
# dnf install -y kpatch kpatch-dnf
# rpm -qi kpatch
# systemctl daemon-reload
# systemctl restart kpatch
# systemctl status -l kpatch
# # Configure kpatch software (Start Daemon kpatch)
# if [ $(systemctl is-enabled kpatch) = "disabled" ]; then
# systemctl enable kpatch
# systemctl is-enabled kpatch
# fi
# # kpatch information
# kpatch list
# # Package List (kernel live-patch)
# dnf list installed | grep kpatch
#-------------------------------------------------------------------------------
# Custom Package Installation [Cockpit]
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/managing_systems_using_the_rhel_8_web_console/index
#-------------------------------------------------------------------------------
# Package Install AlmaLinux Web-Based support tools (from AlmaLinux Community Repository)
dnf install -y cockpit cockpit-dashboard cockpit-packagekit cockpit-session-recording cockpit-storaged cockpit-system cockpit-ws
rpm -qi cockpit
systemctl daemon-reload
systemctl restart cockpit
systemctl status -l cockpit
# Configure cockpit.socket
if [ $(systemctl is-enabled cockpit.socket) = "disabled" ]; then
systemctl enable --now cockpit.socket
systemctl is-enabled cockpit.socket
fi
#-------------------------------------------------------------------------------
# Custom Package Installation [Python 3.6]
#-------------------------------------------------------------------------------
# DNF-Module Enable Python 3.6 Runtime (from AlmaLinux Community Repository)
dnf module list | grep python3
dnf install -y @python36
dnf module list | grep python3
# Package Install Python 3.6 Runtime (from AlmaLinux Community Repository)
dnf install -y python3 python3-pip python3-rpm-generators python3-rpm-macros python3-setuptools python3-test python3-virtualenv python3-wheel
dnf install -y python3-asn1crypto python3-dateutil python3-docutils python3-humanize python3-jmespath python3-pyasn1 python3-pyasn1-modules python3-pyyaml python3-six python3-urllib3
dnf install -y python3-argcomplete
# Version Information (Python 3.6)
python3 -V
pip3 -V
# Python package introduction and setting
alternatives --list
alternatives --set python "/usr/bin/python3"
alternatives --list
which python
python --version
# Python package setting (python3-argcomplete)
activate-global-python-argcomplete
#-------------------------------------------------------------------------------
# Custom Package Installation [Python 3.9]
#-------------------------------------------------------------------------------
# DNF-Module Enable Python 3.9 Runtime (from AlmaLinux Community Repository)
# dnf module list | grep python3
# dnf install -y @python39
# dnf module list | grep python3
# Package Install Python 3.9 Runtime (from AlmaLinux Community Repository)
# dnf install -y python39 python39-devel python39-pip python39-rpm-macros python39-setuptools python39-test python39-wheel
# dnf install -y python39-pyyaml python39-six python39-urllib3
# Version Information (Python 3.9)
# python3.9 -V
# pip3.9 -V
#-------------------------------------------------------------------------------
# Custom Package Installation [EPEL]
#-------------------------------------------------------------------------------
# Package Install EPEL(Extra Packages for Enterprise Linux) Repository Package
dnf install -y epel-release
# Disable EPEL yum repository
egrep '^\[|enabled' /etc/yum.repos.d/epel*
sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/epel.repo
sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/epel-*.repo
egrep '^\[|enabled' /etc/yum.repos.d/epel*
# Cleanup repository information
dnf --enablerepo="*" --verbose clean all
# EPEL repository package [dnf command]
dnf repository-packages epel list > /tmp/command-log_dnf_repository-package-list_epel.txt
dnf repository-packages epel-playground list > /tmp/command-log_dnf_repository-package-list_epel-playground.txt
# Package Install AlmaLinux System Administration Tools (from EPEL Repository)
dnf --enablerepo="epel" install -y atop bcftools bpytop byobu collectd collectd-utils colordiff dateutils fping glances htop httping iftop inotify-tools ipv6calc ncdu nload screen srm tcping yamllint zstd
# dnf --enablerepo="epel" install -y atop bcftools bpytop byobu collectd collectd-utils colordiff dateutils fping glances htop httping iftop inotify-tools ipv6calc moreutils moreutils-parallel ncdu nload screen srm tcping yamllint zstd
# Package Install EC2 instance optimization tools (from EPEL Repository)
dnf --enablerepo="epel" install -y ec2-hibinit-agent
# Package Install AlmaLinux System Administration Tools (from EPEL-Playground Repository)
# dnf --enablerepo="epel-playground" install -y jnettop wdiff
#-------------------------------------------------------------------------------
# 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
#-------------------------------------------------------------------------------
# Package download AWS-CLI v2 Tools (from Bundle Installer)
curl -sS "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.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
# Get AWS Region Information
if [ -n "$RoleName" ]; then
echo "# Get AWS Region Infomation"
aws ec2 describe-regions --region ${Region}
fi
# Get AMI information of this EC2 instance
if [ -n "$RoleName" ]; then
echo "# Get AMI information of this EC2 instance"
aws ec2 describe-images --image-ids ${AmiId} --output json --region ${Region}
fi
# Get the latest AMI information of the OS type of this EC2 instance from Public AMI
if [ -n "$RoleName" ]; then
echo "# Get Newest AMI Information from Public AMI"
NewestAmiInfo=$(aws ec2 describe-images --owner "679593333241" --filter "Name=name,Values=AlmaLinux OS 8.*" "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}
fi
# Get EC2 Instance Information
if [ -n "$RoleName" ]; then
echo "# Get EC2 Instance Information"
aws ec2 describe-instances --instance-ids ${InstanceId} --output json --region ${Region}
fi
# Get EC2 Instance attached EBS Volume Information
if [ -n "$RoleName" ]; then
echo "# Get EC2 Instance attached EBS Volume Information"
aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=${InstanceId} --output json --region ${Region}
fi
# Get EC2 Instance attached VPC Security Group Information
if [ -n "$RoleName" ]; then
echo "# Get 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}
fi
# Get 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
# - SR-IOV
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/sriov-networking.html
#
if [ -n "$RoleName" ]; then
if [[ "$InstanceType" =~ ^(a1.*|c5.*|c5a.*|c5ad.*|c5adn.*|c5an.*|c5d.*|c5n.*|e3.*|f1.*|g3.*|g3s.*|g4dn.*|h1.*|i3.*|i3en.*|i3p.*|m5.*|m5a.*|m5ad.*|m5d.*|m5dn.*|m5n.*|p2.*|p3.*|p3dn.*|r4.*|r5.*|r5a.*|r5ad.*|r5d.*|r5dn.*|r5n.*|t3.*|t3a.*|x1.*|x1e.*|z1d.*|m4.16xlarge|u-*tb1.metal)$ ]]; then
# Get EC2 Instance Attribute(Elastic Network Adapter Status)
echo "# Get EC2 Instance Attribute(Elastic Network Adapter Status)"
aws ec2 describe-instances --instance-id ${InstanceId} --query Reservations[].Instances[].EnaSupport --output json --region ${Region}
# Get Linux Kernel Module(modinfo ena)
echo "# Get Linux Kernel Module(modinfo ena)"
if [ $(lsmod | awk '{print $1}' | grep -x ena) ]; then
modinfo ena
fi
elif [[ "$InstanceType" =~ ^(c3.*|c4.*|d2.*|i2.*|r3.*|m4.*)$ ]]; then
# Get EC2 Instance Attribute(Single Root I/O Virtualization Status)
echo "# Get EC2 Instance Attribute(Single Root I/O Virtualization Status)"
aws ec2 describe-instance-attribute --instance-id ${InstanceId} --attribute sriovNetSupport --output json --region ${Region}
# Get Linux Kernel Module(modinfo ixgbevf)
echo "# Get Linux Kernel Module(modinfo ixgbevf)"
if [ $(lsmod | awk '{print $1}' | grep -x ixgbevf) ]; then
modinfo ixgbevf
fi
else
echo "# Not Target Instance Type :" $InstanceType
fi
fi
# Get 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
#
if [ -n "$RoleName" ]; then
if [[ "$InstanceType" =~ ^(a1.*|c1.*|c3.*|c4.*|c5.*|c5a.*|c5ad.*|c5adn.*|c5an.*|c5d.*|c5n.*|d2.*|e3.*|f1.*|g2.*|g3.*|g3s.*|g4dn.*|h1.*|i2.*|i3.*|i3en.*|i3p.*|m1.*|m2.*|m3.*|m4.*|m5.*|m5a.*|m5ad.*|m5d.*|m5dn.*|m5n.*|p2.*|p3.*|p3dn.*|r3.*|r4.*|r5.*|r5a.*|r5ad.*|r5d.*|r5dn.*|r5n.*|t3.*|t3a.*|x1.*|x1e.*|z1d.*|u-*tb1.metal)$ ]]; then
# Get EC2 Instance Attribute(EBS-optimized instance Status)
echo "# Get EC2 Instance Attribute(EBS-optimized instance Status)"
aws ec2 describe-instance-attribute --instance-id ${InstanceId} --attribute ebsOptimized --output json --region ${Region}
# Get Linux Block Device Read-Ahead Value(blockdev --report)
echo "# Get Linux Block Device Read-Ahead Value(blockdev --report)"
blockdev --report
else
# Get Linux Block Device Read-Ahead Value(blockdev --report)
echo "# Get Linux Block Device Read-Ahead Value(blockdev --report)"
blockdev --report
fi
fi
# Get EC2 Instance attached NVMe Device Information
#
# - Summary of Networking and Storage Features
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html#instance-type-summary-table
#
# - 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
#
if [ -n "$RoleName" ]; then
if [[ "$InstanceType" =~ ^(a1.*|c5.*|c5a.*|c5ad.*|c5adn.*|c5an.*|c5d.*|c5n.*|f1.*|g4dn.*|i3.*|i3en.*|i3p.*|m5.*|m5a.*|m5ad.*|m5d.*|m5dn.*|m5n.*|p3dn.*|r5.*|r5a.*|r5ad.*|r5d.*|r5dn.*|r5n.*|t3.*|t3a.*|z1d.*|u-*tb1.metal)$ ]]; then
# 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 [ $(lsmod | awk '{print $1}' | grep -x nvme) ]; then
if [ $(command -v nvme) ]; then
echo "# Get NVMe Device(nvme list)"
nvme list
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
else
echo "# Not Target Instance Type :" $InstanceType
fi
fi
#-------------------------------------------------------------------------------
# Custom Package Installation [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
#-------------------------------------------------------------------------------
dnf install --nogpgcheck -y "https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm"
rpm -qi amazon-ssm-agent
systemctl daemon-reload
systemctl restart amazon-ssm-agent
systemctl status -l amazon-ssm-agent
# Configure AWS Systems Manager Agent software (Start Daemon awsagent)
if [ $(systemctl is-enabled amazon-ssm-agent) = "disabled" ]; then
systemctl enable amazon-ssm-agent
systemctl is-enabled amazon-ssm-agent
fi
ssm-cli get-instance-information
#-------------------------------------------------------------------------------
# Custom Package Installation [Amazon Inspector Agent]
# https://docs.aws.amazon.com/inspector/latest/userguide/inspector_installing-uninstalling-agents.html
#-------------------------------------------------------------------------------
# # Variable initialization
# InspectorInstallStatus="0"
# # Run Amazon Inspector Agent installer script
# curl -fsSL "https://inspector-agent.amazonaws.com/linux/latest/install" | bash -ex || InspectorInstallStatus=$?
# # Check the exit code of the Amazon Inspector Agent installer script
# if [ $InspectorInstallStatus -eq 0 ]; then
# rpm -qi AwsAgent
# systemctl daemon-reload
# systemctl restart awsagent
# systemctl status -l awsagent
# # Configure Amazon Inspector Agent software (Start Daemon awsagent)
# if [ $(systemctl is-enabled awsagent) = "disabled" ]; then
# systemctl enable awsagent
# systemctl is-enabled awsagent
# fi
# sleep 15
# /opt/aws/awsagent/bin/awsagent status
# else
# echo "Failed to execute Amazon Inspector Agent installer script"
# fi
#-------------------------------------------------------------------------------
# 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
#-------------------------------------------------------------------------------
dnf install --nogpgcheck -y "https://s3.amazonaws.com/amazoncloudwatch-agent/centos/amd64/latest/amazon-cloudwatch-agent.rpm"
rpm -qi amazon-cloudwatch-agent
cat /opt/aws/amazon-cloudwatch-agent/bin/CWAGENT_VERSION
cat /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml
systemctl daemon-reload
# Configure Amazon CloudWatch Agent software (Start Daemon awsagent)
if [ $(systemctl is-enabled amazon-cloudwatch-agent) = "disabled" ]; then
systemctl enable amazon-cloudwatch-agent
systemctl is-enabled amazon-cloudwatch-agent
fi
# 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
systemctl status -l amazon-cloudwatch-agent
/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 AlmaLinux System Administration Tools (from AlmaLinux Community Repository)
# dnf install -y ansible ansible-doc
# Package Install Ansible (from EPEL Repository)
dnf --enablerepo="epel" install -y ansible ansible-doc
ansible --version
ansible localhost -m setup
#-------------------------------------------------------------------------------
# Custom Package Installation [fluentd]
# https://docs.fluentd.org/installation/install-by-rpm
#-------------------------------------------------------------------------------
curl -fsSL "https://toolbelt.treasuredata.com/sh/install-redhat-td-agent4.sh" | sh
rpm -qi td-agent
systemctl daemon-reload
systemctl restart td-agent
systemctl status -l td-agent
# Configure fluentd software (Start Daemon td-agent)
if [ $(systemctl is-enabled td-agent) = "disabled" ]; then
systemctl enable td-agent
systemctl is-enabled td-agent
fi
# Package bundled ruby gem package information
/opt/td-agent/bin/fluent-gem list
#-------------------------------------------------------------------------------
# Custom Package Installation [Terraform]
# https://www.terraform.io/docs/cli/install/yum.html
#-------------------------------------------------------------------------------
# Repository Configuration (HashiCorp Linux Repository)
dnf config-manager --add-repo "https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo"
cat /etc/yum.repos.d/hashicorp.repo
# Cleanup repository information
dnf clean all
# HashiCorp Linux repository package [dnf command]
dnf repository-packages hashicorp list > /tmp/command-log_dnf_repository-package-list_hashicorp.txt
# Package Install Infrastructure as Code (IaC) Tools (from HashiCorp Linux Repository)
dnf --enablerepo="hashicorp" -y install terraform
rpm -qi terraform
terraform version
# Configure terraform software
## terraform -install-autocomplete
cat > /etc/profile.d/terraform.sh << __EOF__
if [ -n "\$BASH_VERSION" ]; then
complete -C /usr/bin/terraform terraform
fi
__EOF__
source /etc/profile.d/terraform.sh
#-------------------------------------------------------------------------------
# Custom Package Clean up
#-------------------------------------------------------------------------------
dnf 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) [firewalld]
if [ $(command -v firewall-cmd) ]; then
# Network Information(Firewall Service) [systemctl status -l firewalld]
systemctl status -l firewalld
# Network Information(Firewall Service) [firewall-cmd --list-all]
# firewall-cmd --list-all
fi
# Linux Security Information(SELinux) [getenforce] [sestatus]
getenforce
sestatus
#-------------------------------------------------------------------------------
# Configure Amazon Time Sync Service
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html
#-------------------------------------------------------------------------------
# Configure NTP Client software (Install chrony Package)
dnf install -y chrony
rpm -qi chrony
if [ $(systemctl is-active chronyd) = "active" ]; then
systemctl daemon-reload
systemctl restart chronyd
sleep 3
systemctl status -l chronyd
else
systemctl daemon-reload
systemctl start chronyd
sleep 3
systemctl status -l chronyd
fi
# Configure NTP Client software (Start Daemon chronyd)
if [ $(systemctl is-enabled chronyd) = "disabled" ]; then
systemctl enable chronyd
systemctl is-enabled chronyd
fi
# 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)
if [ $(systemctl is-active chronyd) = "active" ]; then
systemctl daemon-reload
systemctl restart chronyd
else
systemctl daemon-reload
systemctl start chronyd
fi
if [ $(command -v chronyc) ]; then
sleep 3
chronyc tracking
sleep 3
chronyc sources -v
sleep 3
chronyc sourcestats -v
fi
#-------------------------------------------------------------------------------
# Configure Tuned
#-------------------------------------------------------------------------------
# Package Install Tuned (from AlmaLinux Community Repository)
dnf install -y tuned tuned-utils tuned-profiles-oracle tuned-profiles-mssql
rpm -qi tuned
systemctl daemon-reload
systemctl restart tuned
systemctl status -l tuned
# Configure Tuned software (Start Daemon tuned)
if [ $(systemctl is-enabled tuned) = "disabled" ]; then
systemctl enable tuned
systemctl is-enabled tuned
fi
# Configure Tuned software (select profile - throughput-performance)
tuned-adm list
tuned-adm active
tuned-adm profile throughput-performance
tuned-adm active
#-------------------------------------------------------------------------------
# Configure ACPI daemon (Advanced Configuration and Power Interface)
#-------------------------------------------------------------------------------
# Configure ACPI daemon software (Install acpid Package)
dnf install -y acpid
rpm -qi acpid
systemctl daemon-reload
systemctl restart acpid
systemctl status -l acpid
# Configure NTP Client software (Start Daemon chronyd)
if [ $(systemctl is-enabled acpid) = "disabled" ]; then
systemctl enable acpid
systemctl is-enabled acpid
fi
#-------------------------------------------------------------------------------
# System Setting
#-------------------------------------------------------------------------------
# Setting SELinux permissive mode
getenforce
sestatus
cat /etc/selinux/config
sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
cat /etc/selinux/config
if [ $(getenforce) = "Enforcing" ]; then
setenforce 0
getenforce
fi
# Setting System crypto policy (Default -> FUTURE)
update-crypto-policies --show
# update-crypto-policies --set FUTURE
# update-crypto-policies --is-applied
# Setting SystemClock and Timezone
if [ "${Timezone}" = "Asia/Tokyo" ]; then
echo "# Setting SystemClock and Timezone -> $Timezone"
date
timedatectl status --all --no-pager
timedatectl set-timezone Asia/Tokyo
timedatectl status --all --no-pager
date
elif [ "${Timezone}" = "UTC" ]; then
echo "# Setting SystemClock and Timezone -> $Timezone"
date
timedatectl status --all --no-pager
timedatectl set-timezone UTC
timedatectl status --all --no-pager
date
else
echo "# Default SystemClock and Timezone"
timedatectl status --all --no-pager
date
fi
# Setting System Language
if [ "${Language}" = "ja_JP.UTF-8" ]; then
# Custom Package Installation
dnf install -y langpacks-ja glibc-langpack-ja google-noto-sans-cjk-ttc-fonts google-noto-serif-cjk-ttc-fonts
echo "# Setting System Language -> $Language"
locale
localectl status --no-pager
localectl list-locales --no-pager | grep ja_
localectl set-locale LANG=ja_JP.utf8
localectl status --no-pager
locale
strings /etc/locale.conf
source /etc/locale.conf
elif [ "${Language}" = "en_US.UTF-8" ]; then
echo "# Setting System Language -> $Language"
locale
localectl status --no-pager
localectl list-locales --no-pager | grep en_
localectl set-locale LANG=en_US.utf8
localectl status --no-pager
locale