Search code examples
amazon-web-servicesdockeramazon-ecs

Self describe regions with ECS/EC2 Instance


I want to securely fetch configuration files from S3 using a secure VPC from my Docker container. But I want to determine inside the application which configuration file to fetch and use based on the region I am on. Is there a good/best practice to go on about describing the current container's region?

I understand that you can use the AWS SDK/CLI to describe the ECS instances, but that doesn't tell me which one the container is specifically deployed on.


Solution

  • Within EC2, you can retrieve the instance metadata using a simple curl command to a local (internal) web API. Region and AZ are some of the data points you can get:

    http://169.254.169.254/latest/meta-data/services/domain
    http://169.254.169.254/latest/meta-data/placement/availability-zone
    

    See this page for full details about instance metadata.

    Within ECS, I'd be interested to see if these might still work -- my hunch is they would, as the container should query the host machine's API for the answer, and the ECS host is most certainly an EC2 instance.

    Let us know if that works?