how to allocate elastip ip to autoscaling group with 1 instance - amazon-web-services

I have 1 instance in auto scaling group with min = 1 max = 1 and desired = 1
I also have an elastic ip which i want to assign single instance and also when this once instance goes down, the elastic ip should be released and allocated to the new instance. I have attached admin policy with the role which is attached in the launch configuration for ASG. I have added below information in user data in launch template but my elastic ip is still not getting associated with new instance. I really need help with this please
#!/bin/bash
InstanceID=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id`
Allocate_ID= 'eipalloc-0d54643260cd69141'
aws ec2 associate-address --instance-id $InstanceID --allocation-id $Allocate_ID

You'll need to disassociate the address from the instance to which the EIP is attached to before associating it again.
This will do the job:
#!/bin/bash
ALLOCATE_ID="eipalloc-0d54643260cd69141"
# Release the EIP if it is currently associated with an instance
aws ec2 disassociate-address --association-id "$ALLOCATE_ID" || true
# Associate address to this instance
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 associate-address --instance-id "$INSTANCE_ID" --allocation-id "$ALLOCATE_ID"

Related

EC2 auto scaling with elastic IP

I have deployed an auto scaling EC2 and has associated an Elastic IP address with it. I'm not using a load balancer, because the total number of users doesn't exceed 20. Therefore, my current settings are to have 1 minimum and 1 maximum servers.
If the EC2 server fails, another one is created instead, which is what i'm trying to do. However, the elastic IP is not automatically remapped to the newly created server.
How can i assign the elastic IP automatically to the newly created EC2 instance? Is there a workaround this issue?
UPDATE:
I've added the following to User Data, but the new EC2 is created without a public ip still.
#!/bin/bash
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id=eipalloc-**.***.***.***
Without an ELB to manage your Elastic IPs, you'll need to use the User Data field on your EC2 instance to call the aws ec2 associate-address API endpoint upon instance creation:
aws ec2 associate-address --instance-id <instance id> --allocation-id <eip-alloc-id>
The EIP allocation ID can be found using the AWS Console. You can obtain the Instance ID by making this call in the User Data:
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
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 :)

Auto Scaling, Elastic IP

When I use Network Load Balancer with Auto Scaling, everytime an instance is spawned it gets a new public IP.
I would like to have an EIP for each instance. I was hoping that when I assigned an EIP to availability zones in the Network Loadbalancer configuration would do the trick.
Is there a a way to have autoscale and static IPs for the instances spawned?
https://aws.amazon.com/blogs/aws/new-network-load-balancer-effortless-scaling-to-millions-of-requests-per-second/
According to this it looks it's not possible:
Unfortunately, there is no way to make autoscaling automatically
assign an Elastic IP address to newly launched instances
Static IP for Auto Scale in AWS
and according to this:
Assigning static IPs to auto scaled EC2 instance
Write a script and put it on your startup script in launch configuration for your autoscale group, that script can do anything you want, range from assigning the new EIP to check other services for the white/blacklist.
For more info read
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
As others have mentioned this can be accomplished by provisioning new EIP and using UserData to associate the instance with newly issued EIP. However, following setup would have the following issues:
1) EIP have limits by default its 5 per VPC, and even you can increase the limit you need to know the maximum number of EIP your VPC will use
2) When instance gets terminated you will need to create a process to delete staled EIP or figure out how to reassociated previously allocated EIP
Having said that I do use a static EIP in my ASG but its only for HA rather than scalability, so in the following example I'm reusing existing EIP each time I launch a new instance
#!/bin/bash -xe
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 disassociate-address --association-id ${OpenPVNEIP.AllocationId} --region ${AWS::Region} || true
aws ec2 associate-address --instance-id "${!INSTANCE_ID}" --allocation-id ${OpenPVNEIP.AllocationId} --region ${AWS::Region}
I created a Lambda inside a private subnet.
I linked the private subnet to a NAT Gateway and thus I managed to get a static IP for my Lambda. The limit of 5 EIPs was an issue for me.
https://aws.amazon.com/premiumsupport/knowledge-center/internet-access-lambda-function/
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 :)

Associate elastic ip address to instance via user-data

I'm trying to associate an elastic IP address to instance via the user-data using the AWS cli
INSTANCE_ID=$(curl --silent http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)
aws ec2 associate-address --region "$REGION" --instance-id "$INSTANCE_ID" --public-ip *.*.*.*
This user-data is set in launch configuration for auto scaling group that set to running only 1 instance
Any idea why i can't associate elastic IP via the user-data?
Are you using an ec2 classic instance, or an instance in a VPC? (Unless you have a very old account, it is probably not an ec2 classic instance)
The command you are using is for assocating an elastic ip address for a ec2-classic instance. If you are in a VPC it would be more like:
aws ec2 associate-address --instance-id "$INSTANCE_ID" --AllocationId=eipalloc-xxxxxx

How to register an ec2 instance to AWS ELB without ec2 id ?

Is there anyway to register an ec2 in a elb without knowing the instance id only the dns name ?
I want to have a dns record point to a emr cluster that lives on a vpc
If you want to use AWS CLI aws elb register-instances-with-load-balancer command, you must provide the following arguments
--instances (list)
The IDs of the instances.
The registration of an ec2 instance can only be done through the ec2 instance ID
If you only know your dns name you can find out the instanceId by running the following
aws ec2 describe-instances --filters "Name=dns-name,Values=<your_duns>" --output text --query Reservations[].Instances[].InstanceId
That will at least give you the instance Id for your ec2 instance so you can use it for the other command.
You should even be able to pipe the 2, I did not try but something like this should work
aws ec2 describe-instances --filters "Name=dns-name,Values=<your_duns>" --output text --query Reservations[].Instances[].InstanceId \
xargs -I {} aws elb register-instances-with-load-balancer --load-balancer-name <name> --instances {}
Nope you cannot register or de-register EC2 instance programmatically without the instance ID. Instance ID is mandatory if you are doing the process programmatically.

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?
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
The desire I read in the question is "how to start an instance with a given known IP address (from an elastic IP pool,) without first starting it with another temporary IP address."
The way to do this that I've found, is to first allocate a NetworkInterface, and then allocate the IP address, and then bind the IP address to the NetworkInterface, and then bind the pre-allocated NetworkInterface to the eth0 interface as part of the NetworkInterface launch parameters. Yup, four steps, just because you want your instance to start out with an IP address that won't change for the duration of its lifetime!
For "infrastructure as cattle" behind a NAT/load balancer of some sort, this doesn't matter of course. But for "cloud developer hosts" that you SSH to or "open remote" to from your IDE, keeping the IP address the same all the way from the beginning is a pretty important requirement.