我想检查某个实例上是否有标签列表。我感兴趣的标签是Environment,Env和environment。它们的价值是生产,生产和生产。我正在使用lambda函数进行检查,到目前为止,我已经做到了:
response = ec2.describe_tags(
Filters=[
{
'Name': 'tag:Environment|Env|environment',
'Values': [
'Production|production|Prod',
]
},
{
'Name': 'resource-id',
'Values': [
instance_id,
],
}
])
print(response)
但这不能在实例上捕获适当的标签。我相信问题是我试图捕获“ Key1 | Key2 | Key3”格式的多个标签。任何人都可以建议通过多个标签进行过滤的正确方法吗?谢谢!
似乎您正在尝试使用lambda在EC2事件上工作,然后最好根据EC2事件中可用的实例ID获取实例,获取标签并执行进一步的操作。
ec2 = boto3.client('ec2')
specificinstance = ec2.describe_instances(Filters=[
{
'Name': 'instance-id',
'Vdataalues': [
event["detail"]["EC2InstanceId"]
],
}])
TAGS=specificinstance["Reservations"][0]["Instances"][0]["Tags"]
pprint(TAGS)
或者我没有找到一种基于不同值过滤实例的方法,tag-key
但是下面的示例可能会有所帮助,该实例基于不同的值进行过滤。
import boto3
client = boto3.client('ec2')
filters = [{
'Name': "tag:environment",
'Values': ['prod','production','Production']
}]
response = client.describe_instances(Filters=filters)
print response
如果容器environment
像这样会过滤prod,production,Production
用实例IP或保留ID进行过滤
import boto3
client = boto3.client('ec2')
filters = [{
'Name': "tag:environment",
'Values': ['prod,production,Production']
},
{
'Name': 'instance-id',
'Values': [
'i-123456789',
],
}]
response = client.describe_instances(Filters=filters)
print response
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句