EIP"handson-cli-ec2-eip-eip"を取得します。
手順に必要な設定値を変数に格納をします。
0. リージョンの指定
リージョンを指定します。
環境変数の設定: export AWS_DEFAULT_REGION='ap-northeast-1'
環境変数の設定:
export AWS_DEFAULT_REGION='ap-northeast-1'
1. EIPタグ名
EIPタグ名を指定します。
変数の設定: EC2_EIP_TAG_NAME='handson-cli-ec2-eip-eip'
変数の設定:
EC2_EIP_TAG_NAME='handson-cli-ec2-eip-eip'
各変数に正しい設定値が格納されていることを確認しながら保存します。
変数の確認: cat << END # 0. AWS_DEFAULT_REGION:"ap-northeast-1" AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}" # 1. EC2_EIP_TAG_NAME:"handson-cli-ec2-eip-eip" EC2_EIP_TAG_NAME="${EC2_EIP_TAG_NAME}" END
変数の確認:
cat << END # 0. AWS_DEFAULT_REGION:"ap-northeast-1" AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}" # 1. EC2_EIP_TAG_NAME:"handson-cli-ec2-eip-eip" EC2_EIP_TAG_NAME="${EC2_EIP_TAG_NAME}" END
下段の変数が入っていない、もしくは上段と同等の値が入っていない場合は、それぞれの手順番号に戻って変数の設定を行います。
タグ文字列を生成します。
変数の設定: STRING_EC2_EIP_TAG="ResourceType=elastic-ip,Tags=[{Key=Name,Value=${EC2_EIP_TAG_NAME}}]" \ && echo ${STRING_EC2_EIP_TAG} 結果(例): ResourceType=elastic-ip,Tags=[{Key=Name,Value=handson-cli-ec2-eip-eip}]
STRING_EC2_EIP_TAG="ResourceType=elastic-ip,Tags=[{Key=Name,Value=${EC2_EIP_TAG_NAME}}]" \ && echo ${STRING_EC2_EIP_TAG}
結果(例):
ResourceType=elastic-ip,Tags=[{Key=Name,Value=handson-cli-ec2-eip-eip}]
EIPを取得します。
変数の確認: cat << END # STRING_EC2_EIP_TAG:"ResourceType=elastic-ip,Tags=[{Key=Name,Value=handson-cli-ec2-eip-eip}]" STRING_EC2_EIP_TAG="${STRING_EC2_EIP_TAG}" END コマンド: aws ec2 allocate-address \ --domain vpc \ --tag-specifications ${STRING_EC2_EIP_TAG} 結果(例): { "PublicIp": "xxx.xxx.xxx.xxx", "Domain": "vpc", "AllocationId": "eipalloc-xxxxxxxxxxxxxxxxx", "PublicIpv4Pool": "amazon", "NetworkBorderGroup": "ap-northeast-1" }
cat << END # STRING_EC2_EIP_TAG:"ResourceType=elastic-ip,Tags=[{Key=Name,Value=handson-cli-ec2-eip-eip}]" STRING_EC2_EIP_TAG="${STRING_EC2_EIP_TAG}" END
コマンド:
aws ec2 allocate-address \ --domain vpc \ --tag-specifications ${STRING_EC2_EIP_TAG}
{ "PublicIp": "xxx.xxx.xxx.xxx", "Domain": "vpc", "AllocationId": "eipalloc-xxxxxxxxxxxxxxxxx", "PublicIpv4Pool": "amazon", "NetworkBorderGroup": "ap-northeast-1" }
「EIPタグ名"handson-cli-ec2-eip-eip"が存在する。」ことを確認します。
コマンド: aws ec2 describe-addresses \ --filters Name=tag:Name,Values=${EC2_EIP_TAG_NAME} \ --query "Addresses[].Tags[].Value" \ --output text 結果(例): handson-cli-ec2-eip-eip
aws ec2 describe-addresses \ --filters Name=tag:Name,Values=${EC2_EIP_TAG_NAME} \ --query "Addresses[].Tags[].Value" \ --output text
handson-cli-ec2-eip-eip