Abhishek
Abhishek

Reputation: 107

When we lauch EC2 Instance , is it possible to assign Elastic IP at a time of Launching?

I used DescribeAddressRequest & DescribeAddressResult class to find Elastic IP address on AWS Account ?

Now when I launch new EC2 Instance , is it possible to Assign that Elastic IP address at a time of Launching or Run time ?

Upvotes: 1

Views: 1336

Answers (4)

Viral Prajapati
Viral Prajapati

Reputation: 85

you can try like these After Launch Instance ..AS C# Code..

var associateRequest = new AssociateAddressRequest
          {
              PublicIp = Your Elastic Ip,
              InstanceId = Your InstanceId
          };
          amazonEc2client.AssociateAddress(associateRequest);

Upvotes: 0

Brooks
Brooks

Reputation: 7380

Yes, simply write a script executing those same commands and pass it in via userdata. This does mean the instance has already been launched by the time the Elastic IP has been assigned, however it's still a part of your automated launch process.

I do that when allocating and assigning new elastic IP addresses, however I wonder about your use case here. You're holding on to elastic IP's in order to assign them to new instances?

If the new instance is the same application as the old one, why not autoscale it (I think autoscale will re-map the elastic IP from the old instance to the new one). Otherwise, if the new instance is a different application, then why bother holding on to re-mapping the elastic IP? Why not simply release that IP and reallocate a new one when you need it?

Here is my script that I pass in to userdata when launching an application. We're in development mode, so we haven't gotten to ridding ourselves of elastic IP's and going to CNAME's, so it works for now.

Note, this script relies on variables created by other (parent) scripts, so you will need to create additional functionality, however this should get you mostly there.

#!/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: 2

E.J. Brennan
E.J. Brennan

Reputation: 46839

You can automate the assignment of the EIP after the machine starts. This blog post should get you most of the way there:

https://confluence.atlassian.com/display/BAMKB/Automatically+associating+Elastic+IP+addresses+to+Elastic+Agents

Upvotes: 0

helloV
helloV

Reputation: 52375

No. You can let AWS assign a public address automatically to your instance (at the time of launching) which is not an elastic IP address. But the elastic IP can be associated with an instance only after it is launched.

Upvotes: 0

Related Questions