1

我正在使用boto3 SDK创建 AWS Elastic File System 资源。

在 EFS 的 boto3 文档(上面链接)中,没有服务员(与启动 EC2 实例等其他操作不同)。因此,在创建资源之前,我不能调用服务员来暂停执行,并且必须自己编写。还有一堆边缘案例浮现在脑海中,我找不到处理它们的例子。

client = # Attach credentials and create an efs boto3 client

def find_or_create_file_system(self, a_token):

    fs = self.client.create_file_system(CreationToken=a_token, PerformanceMode='generalPurpose')
    # Returns either:
    # {
    #     'OwnerId': 'string',
    #     'CreationToken': 'string',
    #     'FileSystemId': 'string',
    #     'CreationTime': datetime(2015, 1, 1),
    #     'LifeCycleState': 'creating'|'available'|'deleting'|'deleted',
    #     'Name': 'string',
    #     'NumberOfMountTargets': 123,
    #     'SizeInBytes': {
    #         'Value': 123,
    #         'Timestamp': datetime(2015, 1, 1)
    #     },
    #     'PerformanceMode': 'generalPurpose'|'maxIO'
    # }

    # Or, if an FS is available with that creation token already, the above returns 
    # an error. According to boto3 docs, the error will contain the existing fs id. 
    # Is this an error I need to manage with try/catch? What is the syntax to get 
    # the id out of the error?
    if there_is_an_error
        # EFS already exists
        if fs['LifeCycleState'] == 'creating'
            # Need to wait until it's created then return its id
        elif fs['LifeCycleState'] != 'available'
            # It is being / has been deleted.
            # What now? Is that token never usable again? Does it eventually disappear so I can reuse it? How long do I have to wait before recreating it?

    #  Wait until available
    fs_desc = self.client.describe_file_systems(FileSystemId=fs.id)

    # TODO figure out whether there's a waiter for this
    while fs_desc['FileSystems'][0]['LifeCycleState'] == 'creating':
        time.sleep(5)
        fs_desc.update() # Updates metadata
        print("EFS state: {0}".format(fs_desc['FileSystems'][0]['LifeCycleState']))

    return fs.id

问题 1我必须写自己的服务员是正确的吗?我可以从 API 的其他地方劫持/重新利用服务员,还是有无证服务员?

问题 2如何捕获具有该令牌的实例已存在时发生的错误?以及如何从错误消息中获取 id 来处理这种情况?

问题 3一旦文件系统被删除,令牌是否可以重复使用(即 AWS 最终会清除,还是该令牌会持续存在)?

我问 Q3 的原因是 client.describe_file_systems() 中没有 Filter={} 选项。所以目前,我正在使用包含一个简单的唯一文本句柄的令牌来创建并稍后检索客户唯一的 EFS。我可以使用一个随机的 UUID 令牌,然后用组织名称标记...但不能基于标记检索!!!

问题 4那个 while 循环健壮吗?即是否存在 AWS 将永久返回“正在创建”状态的情况(这会使我陷入无限循环)?

谢谢你的帮助!

4

0 回答 0