Search code examples
amazon-web-servicesaws-cliuser-dataelastic-ip

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?


Solution

  • 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