Skip to content

Instantly share code, notes, and snippets.

@mambroziak
Last active November 1, 2023 22:34
Show Gist options
  • Save mambroziak/33ff9cb5e9f22267280c to your computer and use it in GitHub Desktop.
Save mambroziak/33ff9cb5e9f22267280c to your computer and use it in GitHub Desktop.
AWS EC2 Instance Metadata to BASH Script Variables
#!/bin/bash
# JQ is required to more easily parse json.
AWS_IAM_ROLE=`curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/`
AWS_ACCESS_KEY_ID=`curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/$AWS_IAM_ROLE/ | jq -r '.AccessKeyId'`
AWS_SECRET_ACCESS_KEY=`curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/$AWS_IAM_ROLE/ | jq -r '.SecretAccessKey'`
AWS_TOKEN=`curl -sL http://169.254.169.254/latest/meta-data/iam/security-credentials/$AWS_IAM_ROLE/ | jq -r '.Token'`
AWS_AZ=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
AWS_DEFAULT_REGION="`echo \"$AWS_AZ\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
LOCAL_IP=`curl -sL http://169.254.169.254/latest/meta-data/local-ipv4`
PUBLIC_IP=`curl -sL http://169.254.169.254/latest/meta-data/public-ipv4`
@wlodi83
Copy link

wlodi83 commented Oct 28, 2018

Thanks, nice script, I think AWS_TOKEN should be updated to AWS_SESSION_TOKEN

@thomasdpage
Copy link

Really useful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment