Govind Kumar
Govind Kumar

Reputation: 139

Assign a static elastic IP to an instance in Autoscaling Group

I have one instance in ASG, I need to assign an elastic ip that instance. Now when the instance health check fails, the newly launched instance should have the same elastic IP. The IAM role and everything is in the correct order.

Upvotes: 0

Views: 4279

Answers (4)

Ezequiel Zurita
Ezequiel Zurita

Reputation: 1

The solution for multiple elastic ip is execute this script in user_data of launch configuration associated to auto scaling group.

#!/bin/bash
LOG_FILE="/var/log/elasticipasociation.log"
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
AWS_REGION=us-east-1
ALLOC_ID=("eipalloc-XXXXX" "eipalloc-XXXXX" "eipalloc-XXXX")
exec > >(tee "$LOG_FILE") 2>&1
for id in "${ALLOC_ID[@]}"; do
        ISFREE=$(aws ec2 describe-addresses --allocation-ids $id --query Addresses[].InstanceId --output text --region $AWS_REGION )
        echo -e "$(date +'%Y-%m-%d %H:%M:%S')""- Iniciando Asociacion de IP Elastica..."
        echo -e "Chequeando si la Elastic IP "$id" esta libre..."
        if [ -z "$ISFREE" ]; then
                echo -e "La IP Elastica "$id" esta libre..."
                echo -e "Realizando Asociacion de IP "$id" Elastica con Instancia "$ISFREE"..."
                aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id $ALLOC_ID --allow-reassociation --region $AWS_REGION
                echo -e "---------------------------------------------------"
                exit
        else
                echo -e "La IP Elastica "$id" esta en uso por la instancia id "$ISFREE"..."
                echo -e "Vamos a intentar con la siguiente IP Elastica..."
                echo -e "---------------------------------------------------"
        fi
done

Upvotes: 0

Alex
Alex

Reputation: 4266

With EC2 & Auto scaling, You need using user data in EC2 to Auto Attach Elastic IP to EC2 Instance For Auto scaling

#!/bin/bash
aws configure set aws_access_key_id "XYZ..."
aws configure set aws_secret_access_key "ABC..."
aws configure set region "ap-..."
aws ec2 associate-address --instance-id "$(curl -X GET "http://169.254.169.254/latest/meta-data/instance-id")" --public-ip your_elastic_IP

Note: you should create new user & IAM have only permission associate-address to create/get aws key

Hope it be help you :)

Upvotes: 1

Govind Kumar
Govind Kumar

Reputation: 139

            INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
            MAXWAIT=3
            ALLOC_ID=${IPAddresses}
            echo "Checking if EIP with ALLOC_ID[$ALLOC_ID] is free...."
            ISFREE=$(aws ec2 describe-addresses --allocation-ids $ALLOC_ID --query Addresses[].InstanceId --output text --region ${AWS::Region})
            STARTWAIT=$(date +%s)
            while [ ! -z "$ISFREE" ]; do
              if [ "$(($(date +%s) - $STARTWAIT))" -gt $MAXWAIT ]; then
                echo "WARNING: We waited 30 seconds, we're forcing it now."
                ISFREE=""
              else
                echo "Waiting for EIP with ALLOC_ID[$ALLOC_ID] to become free...."
                sleep 3
                ISFREE=$(aws ec2 describe-addresses --allocation-ids $ALLOC_ID --query Addresses[].InstanceId --output text --region ${AWS::Region})
              fi
            done
            echo Running: aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id $ALLOC_ID --allow-reassociation --region ${AWS::Region}}
            aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id $ALLOC_ID --allow-reassociation --region ${AWS::Region}
            yum install  jq -y

Upvotes: 2

Marcin
Marcin

Reputation: 238081

Not sure how to take that IP from the resource itself and pass it as a user data in Launch configuration.

In the CFN, it would look similar to the following:

Resources:

    MyEIP:
        Type: AWS::EC2::EIP
        Properties: {}

    MyLaunchTemplate:
        Type: AWS::EC2::LaunchTemplate
        Properties:           
            UserData:
              Fn::Base64: !Sub |
                #!/bin/bash -xe

                EIP_IP=${MyEIP}

                echo ${!EIP_IP}

                # use aws cli to attach EIP_IP to the instance

Instance role would be required as well with permissions to attach the EIP.

From docs about !Ref which will be used when EIP_IP=${MyEIP}:

When you pass the logical ID of this resource to the intrinsic Ref function, Ref returns the Elastic IP address.

Upvotes: 1

Related Questions