Skip to content

Instantly share code, notes, and snippets.

@jg75
Created January 24, 2020 05:18
Show Gist options
  • Save jg75/9a17c589aaabfd9cee026e6a2c778ecc to your computer and use it in GitHub Desktop.
Save jg75/9a17c589aaabfd9cee026e6a2c778ecc to your computer and use it in GitHub Desktop.
Automatically associate an elastic ip to the running ec2 instance
#! /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