2

My (python) code runs inside a docker container.

The container is deployed on AWS EC2 for our production and testing purposes, but sometimes on our local machines or other cloud vendors for development and CICD purposes.

For some functionality, I want my python code to be able to distinguish between an EC2 deployment and non-EC2. Is this possible?

I found this answer which uses the EC2 instance metadata endpoint, But I'm wondering:

a) Would this also work from within a docker container?

b) Isn't there a more elegant solution? Issuing an HTTP request and waiting for it seems a bit too much.

(I'm aware that a simple solution is probably to add some proprietary environment variable or flag, trying to find a more native to check this)

Yossi Vainshtein
  • 3,845
  • 4
  • 23
  • 39

1 Answers1

3

I recommend you to go with a custom environment variable. This way you will be able to easily reproduce the required behaviour outside of AWS (on your workstation or using other cloud provider).

Using curl or checking for presence of /etc/cloud would make your application behaviour dependent on third-party services/tools. Beside logic complexity (you'd have to handle possible curl errors, like invalid response codes) that can lead to bugs you surely don't want to meet.

anemyte
  • 17,618
  • 1
  • 24
  • 45