Skip to content

curl command is not work in ubantu EC2 instance connect

0

i am using command curl 169.254.169.254 to fetch api verson but its not working ,i am not getting any data .Tried all possible way but still not working.

asked 2 years ago895 views

3 Answers
0
Accepted Answer

Hello.

Instance metadata is accessed slightly differently between V1 and V2.
If you are using V2, please obtain the token as follows.
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html#instance-metadata-retrieval-examples

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \
&& curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/

By the way, it is not recommended to use V1 for security reasons.
I recommend using V2 if possible.

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

  • Thank you for the update

0

If you're experiencing issues with the curl command not working in your Ubuntu EC2 instance when using EC2 Instance Connect, there are a few things to consider:

  1. First, ensure that your EC2 instance has the necessary network access. The IP address 169.254.169.254 is the link-local address for the EC2 instance metadata service. If you can't reach this address, it could indicate a networking issue within your instance.

  2. Check that your security group settings allow outbound traffic. While the metadata service is typically accessible without explicit configuration, it's worth verifying that there are no overly restrictive outbound rules.

  3. Verify that the curl package is installed on your Ubuntu instance. You can try updating your package list and installing curl if it's not already present:

    sudo apt update sudo apt install curl

  4. If you're still having issues, it could be related to the EC2 Instance Connect configuration. EC2 Instance Connect uses specific IP ranges to connect to your instance. To troubleshoot this, you may need to update your security group's inbound rules to allow access from the EC2 Instance Connect IP ranges for your region.

  5. You can find the EC2 Instance Connect IP ranges for your region by running the following command (replace "us-east-1" with your region if different):

    curl -s https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[] | select(.region=="us-east-1") | select(.service=="EC2_INSTANCE_CONNECT") | .ip_prefix'

  6. Once you have the IP ranges, update your security group to allow inbound traffic on port 22 (SSH) from these IP ranges.

If after trying these steps you're still unable to use curl or connect to the metadata service, you may need to contact AWS support for further assistance, as there could be an underlying issue with your instance or the EC2 service in your region.
Sources
Troubleshoot connection with EC2 Instance Connect | AWS re:Post
AWS Instance Issue | AWS re:Post
SSH access to ec2 fails. | AWS re:Post

answered 2 years ago

EXPERT

reviewed 2 years ago

0

I've narrowed the reason to the curl to 169.254.169.254 failing for a 401 Unauthorized, due to all of our servers now requiring IMDSv2. The setting EC2 > Instances > Select Instance > Actions > Instance Settings > Modify Instance Metadata Options > IMDSv2 is set to 'Required'. Setting it to 'Optional' fixes my issue and allows the curl command to get the instance ID of the server, and therefore leads to a working create-image.

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.