- Newest
- Most votes
- Most comments
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.
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:
-
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.
-
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.
-
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
-
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.
-
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'
-
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
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
Relevant content
asked 2 years ago
asked 2 years ago
asked 3 years ago
asked 7 years ago

Thank you for the update