share facebook facebook2 twitter menu hatena pocket slack

2016.11.07 MON

AWS CLI で EIP を付けて、ひっぺがして、また付けて

川原 洋平

WRITTEN BY川原 洋平

tl;dr

インスタンスに EIP を付与して、インスタンスからひっぺがして別のインスタンスに付与するまで。

メモ

EIP 払い出し

  • run
ALLOCATION_ID=$(aws \
  --region ap-northeast-1 \
  ec2  allocate-address \
    --domain vpc \
    --query AllocationId \
    --output text) && echo ${ALLOCATION_ID}
  • output
eipalloc-xxxxxxxx

IP アドレスを確認

  • run
EIP=$(aws \
  --region ap-northeast-1 \
  ec2  describe-addresses \
    --allocation-ids ${ALLOCATION_ID} \
    --query Addresses[].PublicIp \
    --output text) && echo ${EIP}
  • output
xx.xxx.xx.xxx

EIP 付与

  • run
REGION=ap-northeast-1
INSTANCE_ID=$(aws \
--region ${REGION} \
ec2 describe-instances \
  --filters Name=tag:Name,Values=foo \
  --query 'Reservations[].Instances[].InstanceId' \
  --output text) && echo ${INSTANCE_ID}

ASSOCIATE_ID=$(aws \
  --region ${REGION} \
  ec2 associate-address \
    --instance-id ${INSTANCE_ID} \
    --allocation-id ${ALLOCATION_ID} \
    --output text) && echo ${ASSOCIATE_ID}

aws \
  --region ${REGION} \
  ec2 describe-instances \
    --filters 'Name=tag:Name,Values=foo' \
    --query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value,PublicIpAddress]' \
    --output table
  • output
i-xxxxxxxx
eipassoc-87654321
--------------------------
|    DescribeInstances   |
+------+-----------------+
|  foo |  xx.xxx.xx.xxx  |
+------+-----------------+

EIP ひっぺがし

  • run
ENI=$(aws \
  --region ${REGION} \
  ec2 describe-instances \
    --instance-ids ${INSTANCE_ID} \
    --query 'Reservations[].Instances[].NetworkInterfaces[].NetworkInterfaceId' \
    --output text) && echo ${ENI}

ASSOCIATE_ID=$(aws \
  --region ${REGION} \
  ec2 describe-network-interfaces \
    --network-interface-ids ${ENI} \
    --query NetworkInterfaces[].Association[].AssociationId \
    --output text) && echo ${ASSOCIATE_ID}

aws --region ${REGION} ec2 disassociate-address --association-id ${ASSOCIATE_ID}
  • output
eni-xxxxxxxx
eipassoc-xxxxxxxx

EIP を別のインスタンスに付与

  • run
REGION=ap-northeast-1
ALLOCATION_ID=$(aws \
  ec2 describe-addresses \
    --public-ips ${EIP} \
    --query Addresses[].AllocationId \
    --output text) && echo ${ALLOCATION_ID}

NEW_INSTANCE_ID=$(aws \
  --region ${REGION} \
  ec2 describe-instances \
    --filters Name=tag:Name,Values=bar \
    --query 'Reservations[].Instances[].InstanceId' \
    --output text) && echo ${NEW_INSTANCE_ID}

NEW_ASSOCIATE_ID=$(aws \
  --region ${REGION} \
  ec2 associate-address \
    --instance-id ${NEW_INSTANCE_ID} \
    --allocation-id ${ALLOCATION_ID} \
    --query AssociationId \
    --output text) && echo ${NEW_ASSOCIATE_ID}

aws \
  --region ${REGION} \
  ec2 describe-instances \
    --filters 'Name=tag:Name,Values=bar' \
    --query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value,PublicIpAddress]' \
    --output table
  • output
eipalloc-zzzzzzzz
i-zzzzzzzz
eipassoc-12345678
--------------------------
|    DescribeInstances   |
+------+-----------------+
|  bar |  xx.xxx.xx.xxx  |
+------+-----------------+

以上

メモでした。

元記事はこちら

AWS CLI で EIP を付けて、ひっぺがして、また付けて