Can I use Get-EC2Tag or Get-EC2Instance AWSPowershell cmdlets to match multiple tags and return results only matching those two tags?

0

I want to return the resource/instance ID of a specific instance matching two tags:

$awsEx = (Get-EC2Tag -Filter @{name='tag:Status';values=""},@{name='tag:Site';values="SITENAME"}).ResourceId

$awsEx returns nothing. Running this:

$awsExIpv4 = (Get-EC2Instance -InstanceId $awsEx).Instances.PrivateIpAddress

$awsExIpv4 returns a list of instance IP addresses. The IPs I've crosschecked have matched the Site value. So it's simply returning instances that only match the Site tag and not the Status tag. If it did match both tags, it would return only one instance. The instance that contains no value in Status tag and is tagged with SITENAME in the Site tag.

When I run: $awsEx = (Get-EC2Tag -Filter @{name='tag:Status';values=""}).ResourceId

$awsEx returns the instance Id I need.

**How do I run this query to return results only matching both tags? **

While this works now to return only the instance I need, in the future there could be other instances with no value in the Status tag but differing values in the site tag that should not be returned, as the returned instance Id in the script I am running will be terminated.

RESOLUTION

I was able to resolve this by using different appends and the correct cmdlet. Please see below:

$awsEx = (Get-EC2Instance -Filter @{name='tag:Status';values=""},@{name='tag:Site';values="SITENAME"}).Instances.InstanceId

asked 2 years ago887 views
1 Answer
1

Hi,

Thank you for your rePost question. My name is RJ - an AWS Support Engineer who will be addressing your inquiry today.

I understand from the examples provided, you are looking to return a list of EC2 Instances leveraging multiple tags as filters. Furthermore, I understand that one of the tags will have varying values. To meet your use-case, please consider the following DescribeInstances API example:

## PowerShell
## Creating first filter object, setting 'Status' tag to Wildcard 
## Therefore, return any EC2 Instance with the tag 'Status' with any value
$filterOne = New-Object -TypeName 'Amazon.EC2.Model.Filter' -ArgumentList 'tag:Status','*'

## Creating second filter object, setting 'Site' tag to 'SITENAME'
$filterTwo = New-Object -TypeName 'Amazon.EC2.Model.Filter' -ArgumentList 'tag:Site','SITENAME'

$awsEx = (Get-EC2Instance -Filter @($filterOne,$filterTwo)).Instances.InstanceId

Looking more deeply into the DescribeTags API, I found the following. At this time, the DescribeTags API does not support multiple tags as filters, and therefore leveraging Python, PowerShell, and the AWS CLI render the same Null result. This behavior is expected since DescribeTags is intended to target a single tag, and then return the associated resources.

That being said, DescribeTags does support multiple filters, such as Resource-Type, and Resource-Id. In order to leverage multiple filters with the DescribeTags API, please consider the following example:

## PowerShell
## Creating second filter object, setting 'Site' tag to 'SITENAME'
$filterOne = New-Object -TypeName 'Amazon.EC2.Model.Filter' -ArgumentList 'tag:Site','Sitename'

## Creating first filter object, setting 'Resource-Type' to 'Instance'
$filterTwo = New-Object -TypeName 'Amazon.EC2.Model.Filter' -ArgumentList 'resource-type','instance'

$awsEx = (Get-EC2Tag -Filter @($filterOne,$filterTwo)).ResourceId

In the above example, the first filter instructs the DescribeTags API to target only the tag key/value pair 'Site'='Sitename'. After the API identifies that specific tag, the second filter instructs the API to return only EC2Instance resource types; ignoring VPCs, RouteTables, NatGateways etc.


References:

AWS
SUPPORT ENGINEER
RJ-D
answered 2 years ago
  • Hi RJ,

    Thanks much for your response on my question. DescribeTags API is interesting and I will read the documentation regarding this, however, I was able to workout my solution with a colleague!

    It appears my initial solution was working but, it was only working to retrieve the overarching container containing the instances. Once I narrowed it down and used the correct cmdlet, it worked. Please see my solution below:

    $awsEx = (Get-EC2Instance -Filter @{name='tag:Status';values=""},@{name='tag:Site';values="SITENAME"}).Instances.InstanceId

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions