输出到 CloudFormation 查询参数

Outputs to CloudFormation query params

假设我有以下查询 CloudFormation 堆栈的输出:

$ 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"
    }
]

我想将此输出格式化为可在 aws cloudformation create-stack 命令中使用的字符串,如下所示:

aws cloudformation create-stack \
  ...
  --parameters "ParameterKey=VPC,ParameterValue=vpc-123abcd ParameterKey=SubnetAZ2,..."

问题:如何使用 JMESPath 将对象列表(如上述)转换为格式化字符串(如上述)?

似乎可以通过使用 map 来实现:

--query Stacks[0].Outputs[*].{ParameterKey: OutputKey, ParameterValue: OutputValue} | map([&ParameterKey,&ParameterValue], @)

您可以尝试以下方法:

aws cloudformation describe-stacks --stack-name <stack-name> --query "Stacks[0].Outputs[*].[join(',', [join('=',['ParameterKey', OutputKey]), join('=',['ParameterValue', OutputValue])])] | join(' ', [])" --output text

它相当难读,但 JMESPath 语法远非漂亮。基本上,想法是使用 join 以所需格式收缩整个字符串。

首先构建内部部分(例如 ParameterKey=VPCParameterValue=vpc-123abcd),然后将其加入 ParameterKey=VPC,ParameterValue=vpc-123abcd。最后,您将所有这些连接在一起以构建 ParameterKey=VPC,ParameterValue=vpc-123abcd ParameterKey=SubnetAZ2,ParameterValue=subnet-456efgh