Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 913
Balasekhar Nelli Avatar asked Apr 28 '16 04:04

Balasekhar Nelli


People also ask

How do I associate Elastic IP with EC2 instance?

To associate an Elastic IP address with an instance Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Elastic IPs. Select an Elastic IP address and choose Actions, Associate address. Select the instance from Instance and then choose Associate.

How do I connect to an elastic IP?

Choose Instances, and then select the instance that you're trying to connect to. On the Networking tab, select the elastic network interface that has the attached Elastic IP address. Then, select the elastic network interface ID. On the Details tab, select the associated Subnet ID.

How do I associate an elastic IP to a subnet?

To tag an Elastic IP address Open the Amazon VPC console at https://console.aws.amazon.com/vpc/ . In the navigation pane, choose Elastic IPs. Select the Elastic IP address and choose Tags. Choose Manage tags, enter the tag keys and values as required, and choose Save.


1 Answers

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

like image 110
helloV Avatar answered Oct 17 '22 03:10

helloV