Created
January 24, 2020 05:18
-
-
Save jg75/9a17c589aaabfd9cee026e6a2c778ecc to your computer and use it in GitHub Desktop.
Automatically associate an elastic ip to the running ec2 instance
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
#! /bin/bash | |
allocation_id=<ALLOCATION_ID> | |
metadata=169.254.169.254/latest/meta-data | |
get-region() { | |
curl -s http://$metadata/placement/availability-zone | awk '{ | |
print(substr($0, 0, length($0)-1)) | |
}' | |
} | |
get-instance() { | |
curl -s http://$metadata/instance-id | |
} | |
aws configure set region $(get-region) | |
aws ec2 associate-address \ | |
--instance-id $(get-instance) \ | |
--allocation-id $allocation_id \ | |
--allow-reassociation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment