- 最新
- 最多得票
- 最多評論
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:
相關內容
- 已提問 1 年前
- AWS 官方已更新 3 年前
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