Retrieving AWS Metadata from Environment Variables

When working with AWS EC2, it’s often handy to be able to reference certain information about an instance. The obvious solution is the AWS Metadata service, accessible with a simple cURL command. For example, to get the private IP address of an instance:

curl -s

Since I hate writing boilerplate code, and don’t need to make an HTTP request each time I want to know something about my environment, I put together a Docker image that fetches most of the available metadata and outputs environment variable settings. Also, I’ve been itching to write something in Go.

My primary use case is to create /etc/aws.env when a new CoreOS instance starts, but this should work on any system with systemd. To do this, aws-env.service should be installed and configured to run at startup. One way to do this is via cloud-config (EC2 User Data):

    - name: aws-env.service
      command: start
      enable: yes
      content: |
         # Contents from 'aws-env.service' go here

Alternatively, you can use it with eval:

eval $(docker run --rm -t cmattoon/aws-env)



Retrieving AWS Metadata from Environment Variables