From 4ee1938ee8ae0a50aa0c4fd07d793915bea15b0f Mon Sep 17 00:00:00 2001 From: Lee Maguire Date: Fri, 10 Jun 2022 16:05:03 +0100 Subject: [PATCH] Command to retrive a single environment variable This is particularly useful for multiline variables that can get broken in the list-environment-variabes output ``` % dalmatian service get-environment-variable -e staging -i dxw-govpress -s example -k BASIC_AUTH_HTPASSWD ==> Assuming role to provide access to dxw-govpress infrastructure account ... ==> getting environment variable BASIC_AUTH_HTPASSWD for dxw-govpress/example/staging user1:$apr1$Gdq4r3Yb$lOtYoqBWMnJnfzwFtqzfu. user2:$apr1$UAzD00fp$C8deX/nCkruePbUbaMTnz. ``` --- bin/service/get-environment-variable | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 bin/service/get-environment-variable diff --git a/bin/service/get-environment-variable b/bin/service/get-environment-variable new file mode 100755 index 0000000..34cc355 --- /dev/null +++ b/bin/service/get-environment-variable @@ -0,0 +1,60 @@ +#!/bin/bash + +# exit on failures +set -e +set -o pipefail + +usage() { + echo "Usage: $(basename "$0") [OPTIONS]" 1>&2 + echo "This command can set environment variables for a service" + echo " -h - help" + echo " -i - infrastructure name" + echo " -s - service name " + echo " -e - environment name (e.g. 'staging' or 'prod')" + echo " -k - key e.g SMTP_HOST" + exit 1 +} + +if [ $# -lt 1 ] +then + usage +fi + +while getopts "i:e:s:k:h" opt; do + case $opt in + i) + INFRASTRUCTURE_NAME=$OPTARG + ;; + e) + ENVIRONMENT=$OPTARG + ;; + s) + SERVICE_NAME=$OPTARG + ;; + k) + KEY=$OPTARG + ;; + h) + usage + ;; + *) + usage + ;; + esac +done + +if [[ + -z "$INFRASTRUCTURE_NAME" + || -z "$SERVICE_NAME" + || -z "$ENVIRONMENT" + || -z "$KEY" +]] +then + usage +fi + +echo "==> getting environment variable $KEY for $INFRASTRUCTURE_NAME/$SERVICE_NAME/$ENVIRONMENT" + +aws ssm get-parameter --with-decryption \ + --name "/$INFRASTRUCTURE_NAME/$SERVICE_NAME/$ENVIRONMENT/$KEY" \ + | jq -r '.Parameter.Value'