10
votes

I can able to create an instace with follwoing command

aws ec2 run-instances --image-id $AMI_ID --count 1 --instance-type ${INSTANCE_TYPE} --key-name KEY_NAME --region us-east-1 --security-groups MYSECURITY_GROUP

But I did not find any option to attach elastic IP address to it. Is it possible to attach a Elastic IP during bootstrapping? Or post bootstrapping?

1

1 Answers

9
votes

You can use --user-data (string) option to run-instances. The user data that you pass will contain the CLI to associate the elastic IP. The CLI command is given below. To get the instance-id in user data, use the metadata server:

curl instance-data/latest/meta-data/instance-id

You can also attach an elastic IP after you launch. Use associate-address to attach an elastic IP.

More examples in: associate-address

This example uses the new style (longer) instance id.

aws ec2 associate-address --instance-id i-0b263919b6498b123 --allocation-id eipalloc-64d5890a

You can get the allocation id from

aws ec2 describe-addresses

describe-addresses