Use DescribeTags with an AWS SDK or CLI - Amazon Elastic Compute Cloud

Use DescribeTags with an AWS SDK or CLI

The following code examples show how to use DescribeTags.

CLI
AWS CLI

Example 1: To describe all tags for a single resource

The following describe-tags example describes the tags for the specified instance.

aws ec2 describe-tags \ --filters "Name=resource-id,Values=i-1234567890abcdef8"

Output:

{ "Tags": [ { "ResourceType": "instance", "ResourceId": "i-1234567890abcdef8", "Value": "Test", "Key": "Stack" }, { "ResourceType": "instance", "ResourceId": "i-1234567890abcdef8", "Value": "Beta Server", "Key": "Name" } ] }

Example 2: To describe all tags for a resource type

The following describe-tags example describes the tags for your volumes.

aws ec2 describe-tags \ --filters "Name=resource-type,Values=volume"

Output:

{ "Tags": [ { "ResourceType": "volume", "ResourceId": "vol-1234567890abcdef0", "Value": "Project1", "Key": "Purpose" }, { "ResourceType": "volume", "ResourceId": "vol-049df61146c4d7901", "Value": "Logs", "Key": "Purpose" } ] }

Example 3: To describe all your tags

The following describe-tags example describes the tags for all your resources.

aws ec2 describe-tags

Example 4: To describe the tags for your resources based on a tag key

The following describe-tags example describes the tags for your resources that have a tag with the key Stack.

aws ec2 describe-tags \ --filters Name=key,Values=Stack

Output:

{ "Tags": [ { "ResourceType": "volume", "ResourceId": "vol-027552a73f021f3b", "Value": "Production", "Key": "Stack" }, { "ResourceType": "instance", "ResourceId": "i-1234567890abcdef8", "Value": "Test", "Key": "Stack" } ] }

Example 5: To describe the tags for your resources based on a tag key and tag value

The following describe-tags example describes the tags for your resources that have the tag Stack=Test.

aws ec2 describe-tags \ --filters Name=key,Values=Stack Name=value,Values=Test

Output:

{ "Tags": [ { "ResourceType": "image", "ResourceId": "ami-3ac336533f021f3bd", "Value": "Test", "Key": "Stack" }, { "ResourceType": "instance", "ResourceId": "i-1234567890abcdef8", "Value": "Test", "Key": "Stack" } ] }

The following describe-tags example uses alternate syntax to describe resources with the tag Stack=Test.

aws ec2 describe-tags \ --filters "Name=tag:Stack,Values=Test"

The following describe-tags example describes the tags for all your instances that have a tag with the key Purpose and no value.

aws ec2 describe-tags \ --filters "Name=resource-type,Values=instance" "Name=key,Values=Purpose" "Name=value,Values="

Output:

{ "Tags": [ { "ResourceType": "instance", "ResourceId": "i-1234567890abcdef5", "Value": null, "Key": "Purpose" } ] }
  • For API details, see DescribeTags in AWS CLI Command Reference.

PowerShell
Tools for PowerShell

Example 1: This example fetches the tags for resource-type 'image'

Get-EC2Tag -Filter @{Name="resource-type";Values="image"}

Output:

Key ResourceId ResourceType Value --- ---------- ------------ ----- Name ami-0a123b4ccb567a8ea image Win7-Imported auto-delete ami-0a123b4ccb567a8ea image never

Example 2: This example fetches all the tags for all the resources and groups them by resource type

Get-EC2Tag | Group-Object resourcetype

Output:

Count Name Group ----- ---- ----- 9 subnet {Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription...} 53 instance {Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription...} 3 route-table {Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription} 5 security-group {Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription...} 30 volume {Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription...} 1 internet-gateway {Amazon.EC2.Model.TagDescription} 3 network-interface {Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription} 4 elastic-ip {Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription} 1 dhcp-options {Amazon.EC2.Model.TagDescription} 2 image {Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription} 3 vpc {Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription, Amazon.EC2.Model.TagDescription}

Example 3: This example displays all the resources with tag 'auto-delete' with value 'no' for the given region

Get-EC2Tag -Region eu-west-1 -Filter @{Name="tag:auto-delete";Values="no"}

Output:

Key ResourceId ResourceType Value --- ---------- ------------ ----- auto-delete i-0f1bce234d5dd678b instance no auto-delete vol-01d234aa5678901a2 volume no auto-delete vol-01234bfb5def6f7b8 volume no auto-delete vol-01ccb23f4c5e67890 volume no

Example 4: This example obtains all the resources with tag 'auto-delete' with 'no' value and further filters in the next pipe to parse only 'instance' resource types and eventually creates 'ThisInstance' tag for each instance resources with value being the instance id itself

Get-EC2Tag -Region eu-west-1 -Filter @{Name="tag:auto-delete";Values="no"} | Where-Object ResourceType -eq "instance" | ForEach-Object {New-EC2Tag -ResourceId $_.ResourceId -Tag @{Key="ThisInstance";Value=$_.ResourceId}}

Example 5: This example fetches tags for all the instance resources as well as 'Name' keys and displays them in a table format

Get-EC2Tag -Filter @{Name="resource-type";Values="instance"},@{Name="key";Values="Name"} | Select-Object ResourceId, @{Name="Name-Tag";Expression={$PSItem.Value}} | Format-Table -AutoSize

Output:

ResourceId Name-Tag ---------- -------- i-012e3cb4df567e1aa jump1 i-01c23a45d6fc7a89f repro-3
  • For API details, see DescribeTags in AWS Tools for PowerShell Cmdlet Reference.

For a complete list of AWS SDK developer guides and code examples, see Create Amazon EC2 resources using an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.