#!/bin/bash

val=$(aws ssm get-parameter --name "/$ENVIRONMENT/common/CONSUL_ENCRYPTION_KEY" --with-decryption --output json | jq -r '.Parameter.Value' || true)
echo "Number of characters returned from SSM=$(echo $val | wc -c)"
if [ -z "${val}" ]; then
	echo "Creatign Consul Encryption key"
    consul_key=$(consul keygen)
	aws ssm put-parameter --name "/$ENVIRONMENT/common/CONSUL_ENCRYPTION_KEY" --value "$consul_key" --key-id "$KMS_KEY_ID" --type "SecureString" --tier "Advanced"
else
	echo "Encryption key Exsist in SSM (/$ENVIRONMENT/common/CONSUL_ENCRYPTION_KEY)"
fi