#! /bin/bash set -eu # declare an associative array, the -A defines the array of this type declare -A cfnOutputs # The output of jq is separated by '|' so that we have a valid delimiter # to read our keys and values. The read command processes one line at a # time and puts the values in the variables 'key' and 'value' while IFS='|' read -r key value; do # Strip out the text until the last occurrence of '/' strippedKey="${key##*/}" # Putting the key/value pair in the array cfnOutputs["$strippedKey"]="$value" done< <(jq -r 'keys[] as $k | "\($k)|\(.[$k])"' cfn-output.json) # Print the array using the '-p' or do one by one #declare -p cfnOutputs cp bootstrap/docker/service-template.json service.json sed -i -e 's/CLUSTER_NAME/'"${cfnOutputs[EcsClusterName]}"'/' \ -e 's/SERVICE_NAME/'"${cfnOutputs[StackName]}_Core-Service"'/' \ -e 's~TARGET_GROUP_ARN~'"${cfnOutputs[MythicalServiceTargetGroupArn]}"'~' \ -e 's/CONTAINER_NAME/service/' \ -e 's/PRIVATE_SUBNET_ONE/'"${cfnOutputs[PrivateSubnetOne]}"'/' \ -e 's/PRIVATE_SUBNET_TWO/'"${cfnOutputs[PrivateSubnetTwo]}"'/' \ -e 's/FARGATE_CONTAINER_SECURITY_GROUP/'"${cfnOutputs[FargateContainerSecurityGroup]}"'/' service.json cp bootstrap/docker/service-template.json like-service.json sed -i -e 's/CLUSTER_NAME/'"${cfnOutputs[EcsClusterName]}"'/' \ -e 's/SERVICE_NAME/'"${cfnOutputs[StackName]}_Like-Service"'/' \ -e 's~TARGET_GROUP_ARN~'"${cfnOutputs[LikeServiceTargetGroupArn]}"'~' \ -e 's/CONTAINER_NAME/like-service/' \ -e 's/PRIVATE_SUBNET_ONE/'"${cfnOutputs[PrivateSubnetOne]}"'/' \ -e 's/PRIVATE_SUBNET_TWO/'"${cfnOutputs[PrivateSubnetTwo]}"'/' \ -e 's/FARGATE_CONTAINER_SECURITY_GROUP/'"${cfnOutputs[FargateContainerSecurityGroup]}"'/' like-service.json