Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign EIP to Autoscaling Group of VPC in Cloudformation template

I want to assign one of my reserved Elastic IP's(ec2 classic ip) to Autoscaling group in VPC. Using AWS Cli I moved ip to vpc:

$ aws ec2 move-address-to-vpc --public-ip 23.23.23.23

And saw in aws concole, that this IP passed to VPC. And Assigned in tags of AutoscalingGroup in Cloudformation template in Resources:

"Process": {
        "Type" : "AWS::AutoScaling::AutoScalingGroup",
        "Properties": {
            "LaunchConfigurationName": {"Ref": "PreprocessorLC"},
            "LoadBalancerNames": [{"Ref": "ProcessELB"}],
            "VPCZoneIdentifier" : [{ "Fn::Join" : [",", [ { "Ref" : "PublicSubnet1"}, { "Ref" : "PublicSubnet2"} ]]}],
            "AvailabilityZones": {"Ref": "AZs"},
            "MinSize" : "1",
            "MaxSize" : "1",
            "HealthCheckGracePeriod": 300,
            "Tags" : [
                {"Key": "Name", "Value": {"Fn::Join": ["", [{"Ref": "Env"}, "-Process"]]}, "PropagateAtLaunch": true},
                {"Key": "WorkersScalingGroup", "Value": {"Fn::Join": ["", ["Offering-", {"Ref": "Env"},  "-Process-Worker"]]}, "PropagateAtLaunch": true},
                {"Key": "EIP", "Value": {"Ref": "ProcessIP"}, "PropagateAtLaunch": true},
                {"Key": "Environment", "Value": {"Ref": "Env"}, "PropagateAtLaunch": true}
            ]
        }
    }

And added value of "ProcessIP" in Parameters:

"ProcessIP":{
            "Description": "DEV: 23.23.23.23",
            "Type": "String",
            "Default": "23.23.23.23",
            "AllowedValues": ["23.23.23.23"]
}

And it doesn't worked. Still get random IP. If someone can tell where I'm wrong or what should to add for make it work?

Thanks!

like image 416
muzafarow Avatar asked Jan 18 '16 07:01

muzafarow


People also ask

How do you assign an Elastic IP to Auto Scaling group?

Click on “Create Template from Instance” and choose the option as “Create New Template Version from an Instance”. Select the Launch Template and scroll down to the User Data of Advance Details Section towards the end. Modify the IP address and Allocation Id as per your requirement.

Can we assign Elastic IP to ASG?

With the elastic IP manager and the CloudFormation tag provider, you can dynamically associate EIP addresses with auto scaling group instances. If you are looking for binding static private IP addresses, you can use the EC2 network interface manager.

How do I add a load balancer to Auto Scaling group?

On the Details tab, choose Load balancing, Edit. Under Load balancing, do one of the following: For Application, Network or Gateway Load Balancer target groups, select its check box and choose a target group. For Classic Load Balancers, select its check box and choose your load balancer.


3 Answers

In my case, I needed to keep a bank of unassigned EIPs and randomly assign them to the EC2 when they boot. That way I always know my servers will be using a specific list of IPs that I can whitelist in other places.

If you create several EIPs named "prod-pool" you can then use this script.

apt install -y jq awscli
ALLOCATION_ID=`aws ec2 describe-addresses --filters="Name=tag:Name,Values=prod-pool" | jq -r '.Addresses[] | "\(.InstanceId) \(.AllocationId)"' | grep null | awk '{print $2}' | xargs shuf -n1 -e`

if [ ! -z $ALLOCATION_ID ]; then
    aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id $ALLOCATION_ID --allow-reassociation
fi

You can attached this policy to your IAM user

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowEIPAttachment",
      "Effect": "Allow",
      "Resource": [
        "*"
      ],
      "Action": [
        "ec2:AssociateAddress",
        "ec2:DisassociateAddress"
      ]
    }
  ]
}
like image 182
Steve Robbins Avatar answered Oct 19 '22 07:10

Steve Robbins


Here is simple bash script:

#!/bin/sh
# Region in Which instance is running
EC2_REGION='us-east-1'
AWS_ACCESS_KEY='xxxxxxxxxxxx'
AWS_SECRET_ACCESS_KEY='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

#Instance ID captured through Instance meta data
InstanceID=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id`

#Elastic IP captured through the EIP instance tag
Elastic_IP=`/opt/aws/apitools/ec2/bin/ec2-describe-tags -O $AWS_ACCESS_KEY -W $AWS_SECRET_ACCESS_KEY --filter resource-id=$InstanceID --filter key='EIP' | cut -f5`
Allocate_ID=`/opt/aws/apitools/ec2/bin/ec2-describe-tags -O $AWS_ACCESS_KEY -W $AWS_SECRET_ACCESS_KEY --filter resource-id=$InstanceID --filter key="AllocationID" | cut -f5`

#Assigning Elastic IP to Instance
aws ec2 associate-address --instance-id $InstanceID --allocation-id $Allocate_ID
like image 23
muzafarow Avatar answered Oct 19 '22 05:10

muzafarow


You need to explicitly associate the Elastic IP address with your desired EC2 instance. You can do this in a userdata script at launch time, or externally through other scripting or Configuration Management.

PropagateAtLaunch simply propagates tags from the Auto Scaling Group to any instances that are launched as a result of Auto Scaling actions. I'm not aware of any magic that would cause a tagged Elastic IP address to be associated with a launched instance.

See more discussion and examples of launch time scripting with EIPs here.

like image 32
jarmod Avatar answered Oct 19 '22 05:10

jarmod