60

I am using the below to get the stack information I want via AWS Cli:

aws cloudformation --region ap-southeast-2 describe-stacks --stack-name mystack

It's returning result 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
        }
    ]
}

But I do not know how to return only the value of OutputValue which is myUrl

As I do not need the rest, just myUrl.

Is that possible via aws cloudformation describe-stacks?

Edit

I just realize I can use --query:

--query "Stacks[0].Outputs[1].OutputValue"

will get exactly what I want but I would like to use DbUrl else if the number of Outputs changes, my result will be unexpected.

Steven Yong
  • 3,775
  • 5
  • 25
  • 47

3 Answers3

110

I got the answer, use the below:

--query "Stacks[0].Outputs[?OutputKey=='DbUrl'].OutputValue" --output text

Hope this will help someone.

lsouza
  • 2,283
  • 4
  • 24
  • 35
Steven Yong
  • 3,775
  • 5
  • 25
  • 47
  • 2
    Thank you, this is exactly what I was looking for today. – user1432403 Mar 07 '17 at 13:37
  • 2
    Just for anyone else, I had to use `"` rather than `'` – Tobin Nov 20 '17 at 12:37
  • 1
    If you want to fetch multiple outputs (tab separated in one line) you can use `--query 'Stacks[0].Outputs[?OutputKey==\`ParamOne\` || OutputKey==\`ParamTwo\` || OutputKey==\`ParamThree\`].OutputValue' --output text` – Koka May 11 '18 at 16:42
  • 1
    However, the order in which parameters are returned is random and you can't rely on their index to extract them. For multiple parameters to be retrieved reliably return them as json and use jq to extract them, ie. `--query 'Stacks[0].Outputs[?OutputKey==\`ParamOne\` || OutputKey==\`ParamTwo\` || OutputKey==\`ParamThree\`]' >> params.txt; one=$(cat params.txt | jq -r '.[] | select(.OutputKey=="ParamOne") | .OutputValue'); two=$(cat params.txt | jq -r '.[] | select(.OutputKey=="ParamTwo") | .OutputValue'); three=$(cat params.txt | jq -r '.[] | select(.OutputKey=="ParamThree") | .OutputValue')` – Koka May 11 '18 at 16:53
  • @Koka I'm gonna shamelessly promote my own alternative to `jq` here: https://github.com/jedwards1211/lambduh – Andy Oct 02 '18 at 15:27
  • `--query 'Stacks[0].Outputs' | duh 'json => require("lodash").fromPairs(json.map(({OutputKey, OutputValue}) => [OutputKey, OutputValue]))'` – Andy Oct 02 '18 at 15:27
  • 1
    For OP's example it would output `{"Ec2Sg": "sg-97e13dff", "DbUrl": "myUrl"}`. Much better to get the values by name than by index. – Andy Oct 02 '18 at 15:28
  • more useful examples in the docs: https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-output.html#cli-usage-output-filter – Stefan Oct 31 '20 at 02:53
18

While querying works, it may prove problematic if you have multiple stacks. Realistically, you should probably be leveraging exports for things that are distinct and authoritative.

By way of example - if you modified your CloudFormation snippet to look like this:

"Outputs" : {
  "DbUrl" : {
    "Description" : "My Database Url",
    "Value" : "myUrl",
    "Export" : {
      "Name" : "DbUrl"
    }
  }
}

Then you could use:

aws cloudformation list-exports --query "Exports[?Name==\`DbUrl\`].Value" --no-paginate --output text

to retrieve it. Exports are required to be unique - only one stack can export any given name. This way, you're assured that you get the right value, every time. If you attempt to create a new stack that exports a name that already exists elsewhere, that stack creation will fail.

g.d.d.c
  • 41,737
  • 8
  • 91
  • 106
0

Using the Windows AWS CLI I had to ensure the --query param was doubled quoted.

aws cloudformation describe-stacks --stack-name <stack_name> --query "Stacks[0].Outputs[?OutputKey==`<key_we_want>`].OutputValue" --output text

Failing to use double quotes, resulted in the query returning:

Stacks[0].Outputs[?OutputKey==].OutputValue

Not so helpful.