从aws cloudformation describe-stack获取输出

Ste*_*ong 39 aws-cloudformation aws-cli

我使用以下内容通过AWS Cli 获取我想要的堆栈信息:

aws cloudformation --region ap-southeast-2 describe-stacks --stack-name mystack
Run Code Online (Sandbox Code Playgroud)

它返回结果OK:

{
    "Stacks": [
        {
            "StackId": "arn:aws:mystackid", 
            "LastUpdatedTime": "2017-01-13T04:59:17.472Z", 
            "Tags": [], 
            "Outputs": [
                {
                    "OutputKey": "Ec2Sg", 
                    "OutputValue": "sg-97e13dff"
                }, 
                {
                    "OutputKey": "DbUrl", 
                    "OutputValue": "myUrl"
                }
            ], 
            "CreationTime": "2017-01-13T03:27:18.893Z", 
            "StackName": "mystack", 
            "NotificationARNs": [], 
            "StackStatus": "UPDATE_ROLLBACK_COMPLETE", 
            "DisableRollback": false
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何只返回OutputUall的值,即myUrl

因为我不需要休息,只需我的.

这可能通过aws cloudformation describe-stacks吗?

编辑

我才意识到我可以使用 - 查询:

--query "Stacks[0].Outputs[1].OutputValue"
Run Code Online (Sandbox Code Playgroud)

将得到我想要的,但我想使用DbUrl else如果输出的数量改变,我的结果将是意外的.

Ste*_*ong 61

我得到了答案,请使用以下内容:

--query "Stacks[0].Outputs[?OutputKey=='DbUrl'].OutputValue" --output text
Run Code Online (Sandbox Code Playgroud)

希望这会对某人有所帮助.

  • 谢谢,这正是我今天要找的。 (2认同)
  • 对于其他任何人,我不得不使用 `"` 而不是 `'` (2认同)

g.d*_*d.c 12

查询有效时,如果您有多个堆栈,可能会证明是有问题的。实际上,您可能应该利用出口来实现独特而权威的产品。

举例来说-如果您将CloudFormation片段修改为如下所示:

"Outputs" : {
  "DbUrl" : {
    "Description" : "My Database Url",
    "Value" : "myUrl",
    "Export" : {
      "Name" : "DbUrl"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用:

aws cloudformation list-exports --query "Exports[?Name==\`DbUrl\`].Value" --no-paginate --output text
Run Code Online (Sandbox Code Playgroud)

检索它。导出必须是唯一的-只有一个堆栈可以导出任何给定的名称。这样,您可以确保每次获得正确的价值。如果尝试创建一个新的堆栈,该堆栈导出一个在其他位置已经存在的名称,则该堆栈创建将失败。


sit*_*olf 5

避免使用硬编码索引,例如 [0]。当您有多个堆栈时,这将导致不可预测的查询返回。尝试更动态的查询,如下所示:

aws cloudformation describe-stacks --region my_region --query "Stacks[?StackName=='my_stack_name'][].Outputs[?OutputKey=='my_output_key'].OutputValue" --output text
Run Code Online (Sandbox Code Playgroud)

对于您的示例,这将是:

aws cloudformation describe-stacks --region ap-southeast-2 --query "Stacks[?StackName=='mystack'][].Outputs[?OutputKey=='Ec2Sg'].OutputValue" --output text
Run Code Online (Sandbox Code Playgroud)

记笔记[]。没有它,您的查询将不会返回任何内容。