Open2

awsさわってやってみたことまとめ

xianglishanxianglishan

以下を参照してEC2の動的なパブリックIPアドレスを仮想マシン起動時に静的なドメインにroute53使って登録
ドメインはお名前ドットコムで用意した。
➡️IPはstop/startで変わるけどいつものドメインでいつでもsshとかhttpとかできる
https://dev.classmethod.jp/articles/route53-record-ip-change-by-aws-cli/

以下自分のシェルスクリプトのコード

/home/ubuntu/route/iproute.sh
#!/bin/bash

DOMAIN_NAME="{ドメイン名}"
SUB_NAME="{サブドメイン}"
IP_ADDRESS=`curl -s inet-ip.info`
HOSTED_ZONE_ID="該当ホストゾーンID"

BATCH_JSON='{
        "Comment": "UPSERT a record ",
        "Changes": [{
                "Action": "UPSERT",
                        "ResourceRecordSet": {
                        "Name": "'${SUB_NAME}'.'${DOMAIN_NAME}'",
                        "Type": "A",
                        "TTL": 300,
                        "ResourceRecords": [{ "Value": "'${IP_ADDRESS}'"}]
        }}]
}'
aws route53 change-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}" --change-batch "${BATCH_JSON}"

以下はsystemdのためのserviceファイル

/etc/systemd/system/example.service
[Unit]
Description=Route53 Set And Timer
After=network-online.target

[Service]
ExecStart=/bin/bash /home/ubuntu/route/iproute.sh
WorkingDirectory=/home/ubuntu/route
Type=oneshot
User=ubuntu
Group=ubuntu

[Install]
WantedBy=multi-user.target