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.
To export a stack's output value, use the Export field in the Output section of the stack's template. To import those values, use the Fn::ImportValue function in the template for the other stacks. For a walkthrough and sample templates, see Walkthrough: Refer to resource outputs in another AWS CloudFormation stack.
The aws cloudformation list-stacks command returns summary information about any of your running or deleted stacks, including the name, stack identifier, template, and status. The aws cloudformation list-stacks command returns information on deleted stacks for 90 days after they have been deleted.
The optional Outputs section declares output values that you can import into other stacks (to create cross-stack references), return in response (to describe stack calls), or view on the AWS CloudFormation console. For example, you can output the S3 bucket name for a stack to make the bucket easier to find.
To view information about your CloudFormation stack On the Stacks page of the CloudFormation console, select the stack name. CloudFormation displays the stack details for the selected stack. Select a stack details pane to view the related information about your stack.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With