UPD: changed to bash, added pipeline option, and switched to jq @base64

This commit is contained in:
xpk 2023-07-03 19:57:25 +08:00
parent c3d8b0a030
commit 25b517b676
Signed by: xpk
GPG Key ID: CD4FF6793F09AB86
5 changed files with 30 additions and 37 deletions

View File

@ -1,4 +1,7 @@
#!/usr/bin/env sh #!/usr/bin/env bash
# tell bash to exit if any subcommand fails
set -eo pipefail
# Validate required commands # Validate required commands
if ! [ -x "$(command -v aws)" ]; then if ! [ -x "$(command -v aws)" ]; then

View File

@ -1,11 +1,7 @@
locals {
assume_role_arn = "arn:aws:iam::${var.account_id}:role/${var.role_name}"
}
data "external" "awscli" { data "external" "awscli" {
program = [format("%s/assumeRole.sh", path.module)] program = [format("%s/assumeRole.sh", path.module)]
query = { query = {
assume_role_arn = local.assume_role_arn assume_role_arn = "arn:aws:iam::${var.account_id}:role/${var.role_name}"
role_session_name = var.role_session_name role_session_name = var.role_session_name
} }
} }

View File

@ -1,9 +1,5 @@
#locals {
# joined_aws_cli_command = join(" ", var.aws_cli_commands)
#}
data "external" "awscli_program" { data "external" "awscli_program" {
program = [format("%s/awsWithAssumeRole.sh", path.module)] program = [format("%s/run_awscli.sh", path.module)]
query = { query = {
access_key = var.access_key access_key = var.access_key
secret_key = var.secret_key secret_key = var.secret_key
@ -12,6 +8,7 @@ data "external" "awscli_program" {
} }
} }
# decode encapsulated string back to original
output awscliout { output awscliout {
value = jsondecode(base64decode(data.external.awscli_program.result.awscliout)) value = jsondecode(base64decode(data.external.awscli_program.result.awscliout))
} }

View File

@ -1,4 +1,7 @@
#!/usr/bin/env sh #!/usr/bin/env bash
# tell bash to exit if any subcommand fails
set -eo pipefail
# Validate required commands # Validate required commands
if ! [ -x "$(command -v aws)" ]; then if ! [ -x "$(command -v aws)" ]; then
@ -10,33 +13,24 @@ if ! [ -x "$(command -v jq)" ]; then
exit 1 exit 1
fi fi
# Get the query # Process inputs
TERRAFORM_QUERY=$(jq -Mc .) TERRAFORM_QUERY=$(jq -Mc .)
# Extract the query attributes
AWS_CLI_COMMANDS=$(echo "${TERRAFORM_QUERY}" | jq -r '.aws_cli_commands') AWS_CLI_COMMANDS=$(echo "${TERRAFORM_QUERY}" | jq -r '.aws_cli_commands')
AWS_CLI_QUERY=$(echo "${TERRAFORM_QUERY}" | jq -r '.aws_cli_query')
access_key=$(echo "${TERRAFORM_QUERY}" | jq -r '.access_key') access_key=$(echo "${TERRAFORM_QUERY}" | jq -r '.access_key')
secret_key=$(echo "${TERRAFORM_QUERY}" | jq -r '.secret_key') secret_key=$(echo "${TERRAFORM_QUERY}" | jq -r '.secret_key')
session_token=$(echo "${TERRAFORM_QUERY}" | jq -r '.session_token') session_token=$(echo "${TERRAFORM_QUERY}" | jq -r '.session_token')
# Do we need to assume a role? # Set temp credentials if provided
if [ -n "${access_key}" ]; then if [ -n "${access_key}" ]; then
export AWS_ACCESS_KEY_ID=$access_key export AWS_ACCESS_KEY_ID=$access_key
export AWS_SECRET_ACCESS_KEY=$secret_key export AWS_SECRET_ACCESS_KEY=$secret_key
export AWS_SESSION_TOKEN=$session_token export AWS_SESSION_TOKEN=$session_token
fi fi
# Disable any assigned pager # awscli options
export AWS_PAGER="" 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
# Configure adaptive retry mode # Run the awscli command, encapsulate output in base64
# export AWS_RETRY_MODE=adaptive jq -n --arg jqarg1 "$(aws ${AWS_CLI_COMMANDS})" '{ "awscliout" : $jqarg1 | @base64 }'
export AWS_RETRY_MODE=standard
export AWS_MAX_ATTEMPTS=3
# Run the AWS_CLI command
# aws sts get-caller-identity --query Arn > /tmp/awscli.log
# echo '{"awscliout" : "'$(aws ${AWS_CLI_COMMANDS} | base64 -w0)'"}' | tee -a /tmp/awscli.log
jq -n --arg jqarg1 $(aws ${AWS_CLI_COMMANDS} | base64 -w0) '{ "awscliout" : $jqarg1 }'

View File

@ -2,14 +2,17 @@ variable "aws_cli_commands" {
type = string type = string
} }
variable access_key { variable "access_key" {
type = string type = string
sensitive = true
} }
variable secret_key { variable "secret_key" {
type = string type = string
sensitive = true
} }
variable session_token { variable "session_token" {
type = string type = string
sensitive = true
} }