Created
October 31, 2020 08:06
-
-
Save stefango/97ac17490f8e7060f56560638974dccf to your computer and use it in GitHub Desktop.
autochange elastic ips by aws cli 2
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
# ref: https://wangfanggang.com/AWS/aws-cli-renew-ip/ | |
FilterStr="Name=tag-key,Values=ec2" | |
# get ec2 instance id by tag | |
InstanceId=$(aws ec2 describe-instances --filters $FilterStr --query "Reservations[*].Instances[*].[InstanceId]" --output text) | |
OldPublicIpAddress=$(aws ec2 describe-instances --filters $FilterStr --query "Reservations[*].Instances[*].[PublicIpAddress]" --output text) | |
# generate an elastic ip address | |
NewPublicIpAddress=$(aws ec2 allocate-address --domain vpc --query "PublicIp" --output text) | |
# associate an elastic ip to an existed ec2 instance | |
result=$(aws ec2 associate-address --instance-id $InstanceId --public-ip $NewPublicIpAddress) | |
# release an existed elastic ip address | |
aws ec2 release-address --allocation-id $(aws ec2 describe-addresses --filters "Name=public-ip,Values=$OldPublicIpAddress" --query "Addresses[*].[AllocationId]" --output text) | |
echo "New Elastic IP: "$NewPublicIpAddress |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment