Say I have the following output from querying a CloudFormation stack:
$ aws cloudformation describe-stacks --stack-name my-stack --query "Stacks[0].Outputs[*]"
[
{
"OutputKey": "VPC",
"OutputValue": "vpc-123abcd"
},
{
"OutputKey": "SubnetAZ2",
"OutputValue": "subnet-456efgh"
},
{
"OutputKey": "SubnetAZ1",
"OutputValue": "subnet-789ijkl"
},
{
"OutputKey": "PrivateSubnetAZ2",
"OutputValue": "subnet-012mnop"
},
{
"OutputKey": "PrivateSubnetAZ1",
"OutputValue": "subnet-345qrst"
}
]
I would like to format this output into a string which can be used in a aws cloudformation create-stack
command, like this:
aws cloudformation create-stack \
...
--parameters "ParameterKey=VPC,ParameterValue=vpc-123abcd ParameterKey=SubnetAZ2,..."
Question: How can I convert a list of objects (like the one above) to a formatted string (like the one above) with JMESPath?
It seems like it should be possible by using map, something down the lines of:
--query Stacks[0].Outputs[*].{ParameterKey: OutputKey, ParameterValue: OutputValue} | map([&ParameterKey,&ParameterValue], @)
You can try the following:
aws cloudformation describe-stacks --stack-name <stack-name> --query "Stacks[0].Outputs[*].[join(',', [join('=',['ParameterKey', OutputKey]), join('=',['ParameterValue', OutputValue])])] | join(' ', [])" --output text
Its rather unreadable, but JMESPath syntax is far from pretty. Basically, the idea is that using join
you contract the entire string in the format required.
First you construct inner parts (e.g. ParameterKey=VPC
and ParameterValue=vpc-123abcd
), which then you join into ParameterKey=VPC,ParameterValue=vpc-123abcd
. Finally you join all them together to construct ParameterKey=VPC,ParameterValue=vpc-123abcd ParameterKey=SubnetAZ2,ParameterValue=subnet-456efgh