簡體   English   中英

帶有過濾器的 boto3 中的 Describe_instances 不起作用

[英]Describe_instances in boto3 with filters is not working

我正在 Python 中開發一個腳本,用於刪除舊的 AMI 及其快照。 出於測試目的,我一直在嘗試創建並在刪除 AMI 之后立即進行。 我創建實例的代碼如下(包括最后添加的標簽):

import boto3
from datetime import datetime, timedelta
import time

today = datetime.utcnow().strftime('%Y%m%d')

remove_on = (datetime.utcnow() + timedelta(days=3)).strftime('%Y%m%d')

session = boto3.session.Session(region_name='eu-west-1')
client = session.client('ec2')
ec2 = session.resource('ec2')

instance_info = client.describe_instances(Filters=[{'Name': 'tag:Name', 
'Values': ['Airflow']}]) #This filter DOES work
instance_id = instance_info['Reservations'][0]['Instances'][0]['InstanceId']
instance = ec2.Instance(instance_id)

image = instance.create_image(InstanceId=instance_id, Name=f"Airflow_{today}")
time.sleep(2)
image.create_tags(Tags=[{'Key': 'RemoveOn', 'Value': remove_on},
                        {'Key': 'BackupOf', 'Value': 'Airflow'}])

但是,當我嘗試獲取最近創建的 AMI 的信息時,我沒有得到任何數據:

instances_to_delete = client.describe_instances(Filters=[{'Name': 'tag:RemoveOn', 
                                                      'Values':[remove_on]}])

我試圖在 Values 中顯式放置一個字符串,但它也不起作用。 此外,即使它沒有多大意義(因為我之前已經有一個過濾器在工作),我也在客戶端中指定了區域(因為這些答案Boto3 ec2 describe_instances 總是返回 empty )並且它不起作用。 在此處輸入圖像描述 正如我們在以下屏幕截圖中看到的那樣,標簽就在那里

您的代碼似乎正在創建一個圖像 (AMI),然后在 AMI 上放置一個標簽。

然后,您說它無法找到帶有該標簽的實例 這是有道理的,因為只有圖像被標記,而不是實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM