Skip to content

Instantly share code, notes, and snippets.

@roening
Created November 7, 2017 17:36
Show Gist options
  • Save roening/ad1a7b922b648b16c112747883695d3f to your computer and use it in GitHub Desktop.
Save roening/ad1a7b922b648b16c112747883695d3f to your computer and use it in GitHub Desktop.
function email {
instance_profile=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/`
AWS_ACCESS_KEY=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | grep AccessKeyId | cut -d':' -f2 | sed 's/[^0-9A-Z]*//g'`
AWS_SECRET_KEY=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | grep SecretAccessKey | cut -d':' -f2 | sed 's/[^0-9A-Za-z/+=]*//g'`
TOKEN=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | grep Token | cut -d':' -f2 | sed 's/[^0-9A-Za-z/+=]*//g'`
TO="CHANGE@ME"
FROM="CHANGE@ME"
SUBJECT="CHANGE ME"
MESSAGE="CHANGE ME"
date="$(date -R)"
priv_key="$AWS_SECRET_KEY"
access_key="$AWS_ACCESS_KEY"
signature="$(echo -n "$date" | openssl dgst -sha256 -hmac "$priv_key" -binary | base64 -w 0)"
auth_header="X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=$access_key,Algorithm=HmacSHA256,Signature=$signature"
token_header="X-Amz-Security-Token: $TOKEN"
endpoint="https://email.us-east-1.amazonaws.com/"
action="Action=SendEmail"
source="Source=$FROM"
to="Destination.ToAddresses.member.1=$TO"
subject="Message.Subject.Data=$SUBJECT"
message="Message.Body.Text.Data=$MESSAGE"
curl -s -X POST -H "Date: $date" -H "$auth_header" -H "$token_header" --data-urlencode "$message" --data-urlencode "$to" --data-urlencode "$source" --data-urlencode "$action" --data-urlencode "$subject" "$endpoint" > /dev/null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment