Reputation: 879
I have a VPC. Now I have a script which creates ec2-instances and configures them. The configuration is dependent of the public IP during the installation. The problem is that the configuration is wrong for my instance after rebooting it. Because the public ip changed but the config contains the old public IP. Is there a way to define an automatic allocation of an Elastic IP to instances in a specific VPC? (assign an elastic ip immediatley after the creation)
Upvotes: 1
Views: 3363
Reputation: 10701
As answered by mootmoot I would say there is no features inside VPC to make an EIP automatic attach to a specific EC2 instance. In addition, stopping the instance also disassociates the EIP from it. So EC2 Instances will not keep elastic IP.
There is an option that does not require an Elastic IP.to be assigned by using a service called DynamicURL that change IP Address on A of your domain follow to the Public IP that is assigned to your instance. So whenever the IP is changed your domain is keep associated with your instance.
Upvotes: 0
Reputation: 7380
I usually set the Elastic IP from within the EC2 instance, instead of from whatever created the instance to begin with, with userdata, though I don't see why you can't do the same thing from an external script.
Here is my script that I pass in to userdata when launching an application.
Note, this script relies on variables created by other (parent) scripts e.g.:
REGION = a string holding the region you launched the instance into
RESOURCE_ID = a string holding the ID of the newly launched instance
IP = a string holding the original public IP address
You should be able to get those variables within the response from the initial launch.
You will need to create additional functionality and this assumes you already have launched the instance, however this should get you mostly there.
Note, there is no need to reboot the instance in-between launching and assigning the elastic IP address. In fact, doing so might, as you mentioned, "lose" the public IP address (though I BELIEVE that a simple reboot from the console holds on to the public IP).
#!/bin/bash
EIPID=`aws ec2 allocate-address --domain vpc --region ${REGION} | grep -m 1 'AllocationId' | awk -F : '{print $2}' | sed 's|^ "||' | sed 's|"||'`
IP=`ec2metadata --public-ipv4`
EIP=${IP}
if [ -n "$EIPID" ]
then
conf=`aws ec2 associate-address --instance-id ${RESOURCE_ID} --allocation-id ${EIPID} --region ${REGION} | grep -m 1 'AssociationId' | awk -F : '{print $2}' | sed 's|^ "||' | sed 's|"||'`
if [ -n "$conf" ]
then
while [ "$IP" == "$EIP" ]
do
EIP=`ec2metadata --public-ipv4`
sleep 2
done
echo "Elastic IP ${EIPID} successfully mapped";
echo "ELASTIC_IP=\"${EIP}\"" | sudo tee -a /etc/environment
else
echo "Failed to map Elastic IP Address: ${EIPID}";
fi
else
echo "Failed to acquire Elastic IP address: ${EIPID}";
fi
Upvotes: 3
Reputation: 13166
There is no features inside VPC to make EIP automatic attach to EC2 instance.
You need to use the AWS API language you are familiar with to write own script to do your own automation.
1) Preparation: Allocate an elastic IP, write down the EIP-id
2) Associate EIP-id to EC2 IP allocation work : Use AWS API script to create and launch your EC2 instance, capture the EC2-instance ID (or the Instance interface ID) . Then in the same script, use the API ec2 associate-address(naming is vary slight depends on the AWS API language you use) to attach EIP-id to the EC2 isntance ID(or instance Interface ID)
If you are using API, you may actually use "ec2 describe_addresse" to dynamically find the idle EIP-id that not associated to any instance.
And you need to think ahead of the automation if you want to stop the EC2 from time to time and relaunch them : EC2 Instances will not keep elastic IP
Upvotes: 1
Reputation: 34327
There are two methods
1) use a the API method that is exposed in the aws ec2 associate-address
, see http://docs.aws.amazon.com/cli/latest/reference/ec2/associate-address.html
2) use cloudformation instead of a script see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-ec2.html#scenario-ec2-eip
Upvotes: 0