Skip to content

Instantly share code, notes, and snippets.

@anapsix
Created December 13, 2017 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anapsix/350939a94648cd62e535a5fb45b21c14 to your computer and use it in GitHub Desktop.
Save anapsix/350939a94648cd62e535a5fb45b21c14 to your computer and use it in GitHub Desktop.
How to proxy EC2 metadata

How to proxy to EC2 Metadata

Sometimes, you need to use your EC2 instance's credentials to access the AWS resources, for testing, development, etc.. This is how you do it.

Step 1

create 169.254.169.254 on loopback interface

linux

sudo ip a add 169.254.169.254 dev lo

Mac

sudo ifconfig lo0 alias 169.254.169.254/32 up

Step 2

Proxy to your EC2 server

sudo ssh -N -L169.254.169.254:80:169.254.169.254:80 ${username}@${ec2_intance}

Step 3

If you are using AWS SDK, it should just use the instance metadat that is now available Curl Example:

$ curl 169.254.169.254/latest/meta-data/instance-id
i-0aa4062a456626e34

Docker Example:

$ docker run -it --rm alpine:3.6 wget -qO- 169.254.169.254/latest/meta-data/instance-id
i-0aa4062a456626e34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment