2023-07-03 19:57:25 +08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# tell bash to exit if any subcommand fails
|
|
|
|
set -eo pipefail
|
2023-07-01 16:52:19 +08:00
|
|
|
|
|
|
|
# Validate required commands
|
|
|
|
if ! [ -x "$(command -v aws)" ]; then
|
|
|
|
echo 'Error: aws is not installed.' >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if ! [ -x "$(command -v jq)" ]; then
|
|
|
|
echo 'Error: jq is not installed.' >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-07-03 19:57:25 +08:00
|
|
|
# Process inputs
|
2023-07-01 16:52:19 +08:00
|
|
|
TERRAFORM_QUERY=$(jq -Mc .)
|
|
|
|
AWS_CLI_COMMANDS=$(echo "${TERRAFORM_QUERY}" | jq -r '.aws_cli_commands')
|
|
|
|
access_key=$(echo "${TERRAFORM_QUERY}" | jq -r '.access_key')
|
|
|
|
secret_key=$(echo "${TERRAFORM_QUERY}" | jq -r '.secret_key')
|
|
|
|
session_token=$(echo "${TERRAFORM_QUERY}" | jq -r '.session_token')
|
|
|
|
|
2023-07-03 19:57:25 +08:00
|
|
|
# Set temp credentials if provided
|
2023-07-01 16:52:19 +08:00
|
|
|
if [ -n "${access_key}" ]; then
|
|
|
|
export AWS_ACCESS_KEY_ID=$access_key
|
|
|
|
export AWS_SECRET_ACCESS_KEY=$secret_key
|
|
|
|
export AWS_SESSION_TOKEN=$session_token
|
|
|
|
fi
|
|
|
|
|
2023-07-03 19:57:25 +08:00
|
|
|
# awscli options
|
|
|
|
export AWS_PAGER="" # disable pager
|
|
|
|
export AWS_RETRY_MODE=standard # adaptive causes throttling, use standard for now
|
|
|
|
export AWS_MAX_ATTEMPTS=3 # default is 2
|
2023-07-03 08:24:24 +08:00
|
|
|
|
2023-07-03 19:57:25 +08:00
|
|
|
# Run the awscli command, encapsulate output in base64
|
|
|
|
jq -n --arg jqarg1 "$(aws ${AWS_CLI_COMMANDS})" '{ "awscliout" : $jqarg1 | @base64 }'
|