- 我必须删除没有标签Retain=true的文件系统。
- 对于将此标记设置为false的文件系统,目标是
- 自从删除挂载资源以来这需要时间,我需要等到我可以删除我的EFS。所以我自定义等待,直到挂载目标被移除。
以下代码的输出是
服务员不存在:TargetDeleted
这是我的代码
Thanks in advance
import boto3
from botocore.waiter import WaiterModel
from botocore.waiter import create_waiter_with_client
client = boto3.client('efs')
waiter_name = "TargetDeleted"
waiter_config = {
"version": 2,
"waiters": {
"MountTargetDeleted": {
"delay": 30,
"operation": "DescribeMountTargets",
"maxAttempts": 90,
"acceptors": [
{
"expected": "deleted",
"matcher": "pathAll",
"state": "success",
"argument": "MountTargets[].LifeCycleState"
},
{
"expected": "updating",
"matcher": "pathAll",
"state": "failure",
"argument": "MountTargets[].LifeCycleState"
},
{
"expected": "creating",
"matcher": "pathAll",
"state": "failure",
"argument": "MountTargets[].LifeCycleState"
}
]
}
}
}
def lambda_handler(event, context):
response = client.describe_file_systems()
fs=response['FileSystems']
for details in fs:
fid=details['FileSystemId']
tag_response= client.describe_tags(
FileSystemId=fid
)
alltags=tag_response['Tags']
if {'Key': 'Retain', 'Value': 'True'} not in alltags:
try:
client.delete_file_system(FileSystemId=fid)
print("The file", details['FileSystemId'],"deleted")
except:
print(client.describe_file_systems(FileSystemId=fid))
response1 = client.describe_mount_targets(
FileSystemId=fid
)
fs1=response1['MountTargets']
for detail in fs1:
mid=detail['MountTargetId']
response3 = client.delete_mount_target(
MountTargetId=mid
)
try:
waiter_model = WaiterModel(waiter_config)
backup_waiter = create_waiter_with_client(waiter_name, waiter_model, client)
client.delete_file_system(FileSystemId=fid)
print("The file", details['FileSystemId'],"deleted")
except Exception as e:
print(e)
else:
print("Retain tag set to True. The file", details['FileSystemId'],"cannot be deleted")
配置中的服务员名称应该与服务员的名称匹配。
waiter_name = "MountTargetDeleted"
waiter_config = {
"version": 2,
"waiters": {
"MountTargetDeleted": {
"delay": 30,
"operation": "DescribeMountTargets",
"maxAttempts": 90,
"acceptors": [
{
"expected": "deleted",
"matcher": "pathAll",
"state": "success",
"argument": "MountTargets[].LifeCycleState"
},
{
"expected": "updating",
"matcher": "pathAll",
"state": "failure",
"argument": "MountTargets[].LifeCycleState"
},
{
"expected": "creating",
"matcher": "pathAll",
"state": "failure",
"argument": "MountTargets[].LifeCycleState"
}
]
}
}
}
同样,您可以考虑pathAny作为失败条件。