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.
create 169.254.169.254
on loopback interface
sudo ip a add 169.254.169.254 dev lo
sudo ifconfig lo0 alias 169.254.169.254/32 up
Proxy to your EC2 server
sudo ssh -N -L169.254.169.254:80:169.254.169.254:80 ${username}@${ec2_intance}
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