3

From within a Docker container, how can I detect that I am running inside an AWS environment? I want the same container to optionally execute some AWS commands on startup IF running from within AWS, but skip those if running in a local environment.

Currently, I am thinking that the simple way is to set an environment variable when running in AWS.

Is there another way?

Inquisitor Shm
  • 1,433
  • 5
  • 17
  • 26
  • See https://stackoverflow.com/questions/22409367/fetching-aws-instance-metadata-from-within-docker-container. – jarmod Jun 16 '17 at 21:25

3 Answers3

2

If your Docker container lacks curl or wget, you may use this trick in Bash:

if ( exec 2>/dev/null ; echo > /dev/tcp/169.254.169.254/80 ) ; then
   echo "AWS"
fi
Ricardo Branco
  • 5,740
  • 1
  • 21
  • 31
1

For instance:

curl -i http://169.254.169.254/ | grep "200 OK"

Docs: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html

Robert
  • 33,429
  • 8
  • 90
  • 94
1

Crude way of checking if you are running on AWS. All instances that are running on AWS have access to an internal metadata server with IP: 169.254.169.254. If you can connect to it, then you are on AWS. Otherwise you are not.

$ curl -s --connect-timeout 2 169.254.169.254 > /dev/null
$ echo $?
0

On non AWS machine:

$ curl -s --connect-timeout 2 169.254.169.254 > /dev/null
$ echo $?
28
helloV
  • 50,176
  • 7
  • 137
  • 145