Search code examples
amazon-web-servicesaws-cloudformationaws-clijmespath

How do I filter CloudFormation stacks by tag value?


I want to get the stack name of all CloudFormation stacks with the following tag/value:

elasticbeanstalk:environment-name : myenvironment

Is there any way to do this? I tried this query but it throws a syntax error:

aws cloudformation describe-stacks --query 'Stacks[].Tags[?Key == `elasticbeanstalk:environment-name` && ?Value == `myenvironment`]'

Edit

I figured out the filtering part but I can't figure out how to get the stack name. This returns null, how do I retrieve the StackName?

aws cloudformation describe-stacks --query 'Stacks[].Tags[?Key == `elasticbeanstalk:environment-name` && Value == `myenv`].{MyStack: StackName}'

Solution

  • This was quite an ass pain. I look forward to writing jmespath queries less than regex

    aws cloudformation describe-stacks --query 'Stacks[?Tags[?Key == `elasticbeanstalk:environment-name` && Value == `myenvname`]].{StackName: StackName}' --output text