Skip to content
This repository has been archived by the owner on Jan 22, 2023. It is now read-only.

use IAM machine role instead of placing credentials on box #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions ebs-snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,20 @@ set -o pipefail

## Variable Declartions ##

# This URI is the same everywhere. Keep it DRY.
latest_metadata=http://169.254.169.254/latest/meta-data/

# Get Instance Details
instance_id=$(wget -q -O- http://169.254.169.254/latest/meta-data/instance-id)
region=$(wget -q -O- http://169.254.169.254/latest/meta-data/placement/availability-zone | sed -e 's/\([1-9]\).$/\1/g')
instance_id=$(wget -q -O- ${latest_metadata}instance-id)
region=$(wget -q -O- ${latest_metadata}placement/availability-zone | sed -e 's/\([1-9]\).$/\1/g')
iam_role=$(wget -q -O- ${latest_metadata}iam/security-credentials/)

# Export the rolling credentials for the iam role, if this instance has one.
if [ -n "$iam_role" ]; then
export AWS_ACCESS_KEY_ID=$(wget -q -O- ${latest_metadata}iam/security-credentials/${iam_role} | grep "AccessKeyId" | cut -d ":" -f 2 | tr "," " " | tr -d '"')
export AWS_SECRET_ACCESS_KEY=$(wget -q -O- ${latest_metadata}iam/security-credentials/${iam_role} | grep "SecretAccessKey" | cut -d ":" -f 2 | tr "," " " | tr -d '"')
export AWS_SECURITY_TOKEN=$(wget -q -O- ${latest_metadata}iam/security-credentials/${iam_role} | grep "Token" | cut -d ":" -f 2 | tr "," " " | tr -d '"')
fi

# Set Logging Options
logfile="/var/log/ebs-snapshot.log"
Expand Down