Balasekhar Nelli
Balasekhar Nelli

Reputation: 1295

How to attach Elastic IP to EC2 instance during bootstrapping in aws CLI?

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?

Upvotes: 12

Views: 4152

Answers (2)

Jon Watte
Jon Watte

Reputation: 7198

The desire I read in the question is "how to start an instance with a given known IP address (from an elastic IP pool,) without first starting it with another temporary IP address."

The way to do this that I've found, is to first allocate a NetworkInterface, and then allocate the IP address, and then bind the IP address to the NetworkInterface, and then bind the pre-allocated NetworkInterface to the eth0 interface as part of the NetworkInterface launch parameters. Yup, four steps, just because you want your instance to start out with an IP address that won't change for the duration of its lifetime!

For "infrastructure as cattle" behind a NAT/load balancer of some sort, this doesn't matter of course. But for "cloud developer hosts" that you SSH to or "open remote" to from your IDE, keeping the IP address the same all the way from the beginning is a pretty important requirement.

Upvotes: 2

helloV
helloV

Reputation: 52375

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

Upvotes: 10

Related Questions