Last active
December 16, 2015 07:08
-
-
Save na0AaooQ/440761480838103a5136 to your computer and use it in GitHub Desktop.
AWS EC2インスタンス起動時 cloud-initによりホスト名をNameタグ_インスタンスID_IPアドレスに設定する ref: http://qiita.com/na0AaooQ/items/d18a0df8d147e51a04a2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": "ec2:Describe*", | |
"Resource": "*" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ec2-user@ip-172-31-4-80 ~]$ hostname | |
ip-172-31-4-80 | |
[ec2-user@ip-172-31-4-80 ~]$ sudo vi /var/lib/cloud/scripts/per-boot/001_setting_hostname.sh | |
#!/bin/sh | |
##### リージョン名を取得 | |
REGION=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/ | sed 's/.$//'` | |
##### インスタンス自身のインスタンスIDを取得 | |
INSTANCE_ID=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id` | |
##### インスタンス自身のIPアドレスを取得 | |
IP_ADDRESS=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/local-ipv4` | |
##### Nameタグを取得 | |
NAME_TAG=`/usr/bin/aws ec2 describe-instances --region ${REGION} --instance-ids ${INSTANCE_ID=} --query 'Reservations[].Instances[].Tags[?Key==\`Name\`].Value' --output text` | |
##### ホスト名の一部に設定するIPアドレスの.を-に変換 | |
IP_ADDRESS=`echo ${IP_ADDRESS} | sed -e "s/\./-/g"` | |
##### ホスト名を変更する | |
INSTANCE_NAME="${NAME_TAG}_${INSTANCE_ID}_${IP_ADDRESS}" | |
hostname ${INSTANCE_NAME} | |
sed -i -e "s/^HOSTNAME=.*\$/HOSTNAME=${INSTANCE_NAME}/g" /etc/sysconfig/network |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ec2-user@ip-172-31-4-80 ~]$ sudo chmod 755 /var/lib/cloud/scripts/per-boot/001_setting_hostname.sh | |
[ec2-user@ip-172-31-4-80 ~]$ | |
[ec2-user@ip-172-31-4-80 ~]$ ll /var/lib/cloud/scripts/per-boot/001_setting_hostname.sh | |
-rwxr-xr-x 1 root root 969 Dec 28 00:32 /var/lib/cloud/scripts/per-boot/001_setting_hostname.sh | |
[ec2-user@ip-172-31-4-80 ~]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ec2-user@ip-172-31-4-80 ~]$ hostname | |
ip-172-31-4-80 | |
[ec2-user@ip-172-31-4-80 ~]$ grep -i hostname /etc/sysconfig/network | |
HOSTNAME=localhost.localdomain | |
[ec2-user@ip-172-31-4-80 ~]$ sudo reboot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ec2-user@web-server-002_i-9b6f5f82_172-31-4-80 ~]$ hostname | |
web-server-002_i-9b6f5f82_172-31-4-80 | |
[ec2-user@web-server-002_i-9b6f5f82_172-31-4-80 ~]$ grep -i hostname /etc/sysconfig/network | |
HOSTNAME=web-server-002_i-9b6f5f82_172-31-4-80 | |
[ec2-user@web-server-002_i-9b6f5f82_172-31-4-80 ~]$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment