amazon-web-servicesdockeramazon-iam

Pass AWS credentials (IAM role credentials) to code running in Docker container


When running code on an EC2 instance, the SDK I use to access AWS resources, automatically talks to a locally linked web server on 169.254.169.254 and gets that instances AWS credentials (access_key, secret) that are needed to talk to other AWS services.

Also there are other options, like setting the credentials in environment variables or passing them as command line args.

What is the best practice here? I really prefer to let the container access the 169.254.169.254 (by routing the requests) or even better run a proxy container that mimics the behavior of the real server at 169.254.169.254.

Is there already a solution out there?


Solution

  • The EC2 metadata service will usually be available from within docker (unless you use a more custom networking setup - see this answer on a similar question).

    If your docker network setup prevents it from being accessed, you might use the ENV directive in your Dockerfile or pass them directly during run, but keep in mind that credentials from IAM roles are automatically rotated by AWS.