ex-aws / ex_aws

A flexible, easy to use set of clients AWS APIs for Elixir
https://hex.pm/packages/ex_aws
MIT License
1.26k stars 521 forks source link

InstanceMeta bug in v2.3.0 #874

Closed rubysolo closed 2 years ago

rubysolo commented 2 years ago

Environment

* hackney 1.18.1 (Hex package) (rebar3)
  locked at 1.18.1 (hackney) a4ecdaff

Current behavior

Version 2.3.0 does not work as expected with our S3 configuration. When we hit this line, we get a timeout trying to generate an IMDSv2 token.

Expected behavior

IMDSv2 should not be required, as AWS states that IMDSv1 will be supported indefinitely.

If you would like, I could submit a PR that would only use IMDSv2 if required (i.e. if a v1 request returns a 401)

bernardd commented 2 years ago

Hi @rubysolo - thanks for the report. A PR is most welcome, but before you do just double-check that it's not already been resolved by the fix in #873. Cheers.

rubysolo commented 2 years ago

I reproduced this issue on c8015be. I'm testing a fix and will PR once verified.

SteffenDE commented 2 years ago

If somebody stumbles across timeouts: Currently, using ex_aws on an EC2 instance inside docker using the instance role fails for me in 2.3.1.

ExAws.Request.Hackney.request(:put, "http://169.254.169.254/latest/api/token", "", [{"x-aws-ec2-metadata-token-ttl-seconds", 12800}], follow_redirect: true)
{:error, %{reason: :timeout}}

This is because of the default hop limit, see https://stackoverflow.com/questions/71884350/using-imds-v2-with-token-inside-docker-on-ec2.

Can be fixed by using

aws ec2 modify-instance-metadata-options \
               --instance-id <instance-id> \
               --http-put-response-hop-limit 2 \
               --http-endpoint enabled

It took me quite a while to figure this out...