Getting Outputs from aws cloudformation describe-stacks

Amazon CloudformationAws Cli

Amazon Cloudformation Problem Overview


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.

Amazon Cloudformation Solutions


Solution 1 - Amazon Cloudformation

I got the answer, use the below:

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

Or

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

Or

--query 'Stacks[?StackName=='mystack'][].Outputs[?OutputKey==`DbUrl`].OutputValue' --output text

Solution 2 - Amazon Cloudformation

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.

Solution 3 - Amazon Cloudformation

To clarify the correct way of using list-exports: There was a little bit issue on above post, we cannot use \' or \` around output name. The correct syntax is to use single quote ' without using escape character.

Example:

Using escape of tilde

C:\>aws cloudformation list-exports --query "Exports[?Name==\`my-output-name4\`].Value" --no-paginate --output text

Bad value for --query Exports[?Name==\`my-output-name4\`].Value: Bad jmespath expression: Unknown token \:
Exports[?Name==\`my-output-name4\`].Value

Using escape of single quote:

C:\>aws cloudformation list-exports --query "Exports[?Name==\'my-output-name4\'].Value" --no-paginate --output text

Bad value for --query Exports[?Name==\'my-output-name4\'].Value: Bad jmespath expression: Unknown token \:
Exports[?Name==\'my-output-name4\'].Value

And finally, the correct syntax:

C:\>aws cloudformation list-exports --query "Exports[?Name=='myexportname'].Value" --no-paginate --output text

If you had errors or unexpected empty output, please be aware all CLI is case sensitive. For example, if you used

--query "Exports[?Name=='myexportname'].value"

The output would be empty.

Solution 4 - Amazon Cloudformation

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.

Solution 5 - Amazon Cloudformation

Avoid using hardcoded indexes (stack names for example). This will lead to unpredicted query returns when you have multiple stacks. Try a more dynamic query such as the following:

aws cloudformation describe-stacks --region my_region --query "Stacks[?StackName=='my_stack_name'][].Outputs[?OutputKey=='my_output_key'].OutputValue" --output text

For your example this would be:

aws cloudformation describe-stacks --region ap-southeast-2 --query "Stacks[?StackName=='mystack'][].Outputs[?OutputKey=='Ec2Sg'].OutputValue" --output text

Take note of []. Without it, your query will not return anything.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteven YongView Question on Stackoverflow
Solution 1 - Amazon CloudformationSteven YongView Answer on Stackoverflow
Solution 2 - Amazon Cloudformationg.d.d.cView Answer on Stackoverflow
Solution 3 - Amazon CloudformationSQL WarriorView Answer on Stackoverflow
Solution 4 - Amazon CloudformationSelfishCosmonautView Answer on Stackoverflow
Solution 5 - Amazon CloudformationScriptCodeView Answer on Stackoverflow