How can I tell if my container is running on AWS ECS or not?

If I run my container on docker on AWS ECS (EC2 Container Service), is there a way that I can tell inside the application if my container is running on AWS ECS or not? This is necessary because my docker container can run on any platform, but when running on AWS ECS I need to do some extra steps.

+3


source to share


2 answers


Maybe you can use Amazon ECS Container Agent Introspection :

ECAS Container Agent Amazon provides an API for collecting information about the container instance that the agent is running on and the associated tasks that run on that instance.

You can use a command curl

from a container instance to query the Amazon ECS container agent (port 51678) and return the container instance metadata or task information.

For example, from your container:

[ec2-user ~]$ curl http://localhost:51678/v1/metadata

      

Output:

{
  "Cluster": "default",
  "ContainerInstanceArn": "<container_instance_ARN>",
  "Version": "Amazon ECS Agent - v1.14.1 (467c3d7)"
}

      


Other criteria mentioned by the OP in the comments are MetaData Instance and User Data



Instance metadata is data about your instance that can be used to configure or manage a running instance. Instance metadata is categorized.

To view all categories of instance metadata from a running instance, use the following URI:

http://169.254.169.254/latest/meta-data/

      

Note that you are not billed for the HTTP requests used to retrieve instance metadata and user data.

You can use a tool like cURL, or if your instance supports it, the GET command; eg:

[ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/

      

So, successful is curl

enough:

 curl -sL -w "%{http_code}\\n" "http://169.254.169.254/latest/meta-data/" -o /dev/null

      

It will display 200 if OK.
See " Linux curled script to verify that the webservice is running ."

+3


source


After a lot of trial and error, I found the following to be the most helpful:
Invoke http://169.254.169.254/latest/meta-data/
If you get 200 OK, you can assume that you are running inside AWS EC2 / ECS.
But if you don't get 200 OK, then you are not working for AWS EC2 / ECS.



+2


source







All Articles