How to identify whether my container is running on AWS ECS or not?
Maybe you can use the Amazon ECS Container Agent Introspection:
The Amazon ECS container agent provides an API for gathering details about the container instance that the agent is running on and the associated tasks that are running on that instance.
You can use the
curl
command from within the container instance to query the Amazon ECS container agent (port 51678) and return container instance metadata or task information.
For instance, from within 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)"
}
Another criteria, as mention by the OP in the comments, is the Instance MetaData and User Data
Instance metadata is data about your instance that you can use to configure or manage the running instance. Instance metadata is divided into categories.
To view all categories of instance metadata from within a running instance, use the following URI:
http://169.254.169.254/latest/meta-data/
Note that you are not billed for HTTP requests used to retrieve instance metadata and user data.
You can use a tool such as cURL, or if your instance supports it, the GET command; for example:
[ec2-user ~]$ curl http://169.254.169.254/latest/meta-data/
So a successful curl
is enough:
curl -sL -w "%{http_code}\\n" "http://169.254.169.254/latest/meta-data/" -o /dev/null
That will display 200 if OK.
See "Linux script with curl to check webservice is up"
After a lot of trial and error, I found the following most helpful:
Invoke http://169.254.169.254/latest/meta-data/
If you get 200 OK, then you can assume you are running inside AWS EC2/ECS.
But if you don't get 200 OK, then you are not running in AWS EC2/ECS.