How to Get Custom Tags with AWS CLI
The aim of this page📝 is to explain how to list and filter custom tags in AWS CLI based on the particular example of listing custom tags for EC2 and Lambda resources.
2 min readJul 20, 2023
- You can list custom tags in AWS CLI using the
describe-tags
command for EC2 resources or thelist-tags
command for Lambda resources. - You can use the
get-resources
command of theresourcegroupstaggingapi
to return all the tagged or previously tagged resources that are located in the specified Region for the AWS account. - You can redirect the output of the AWS CLI command to a file using the
>
operator. - The output of the
get-resources
command is organized into sections, with each section representing a resource and its associated tags. - You can filter the output of the
get-resources
command by tag using the--tag-filters
option. - You can use the
--query
option of theget-resources
command to filter the output using a JMESPath query expression. - In Cost Explorer, you can filter your AWS costs by tag key and value.
CODE
- Some examples here:
aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=key,Values=Purpose" "Name=value,Values="
aws resourcegroupstaggingapi get-resources --tag-filters Key=system,Values=foo*
<!-- EXAMPLE: filter only tags with system key -->
aws resourcegroupstaggingapi get-resources --query 'ResourceTagMappingList[].Tags[?Key==`system`]'
RUBBERDUCK
MERMAID
graph LR
A[aws] --> B[ec2 describe-tags]
A --> C[resourcegroupstaggingapi get-resources]
B --> D[EC2 Resources]
C --> E[All Tagged Resources]
E --> F[Filtered by Tag Key and Value]