等到使用 boto3 和 lambda 完成快照

wait until the snapshot is completed with boto3 and lambda

我在 lambda 中编写了 python 代码来创建快照并等待它完成以执行其余代码,但我有错误 错误

"errorMessage": "Parameter validation failed:\nUnknown parameter in input: \"SnapshotsId\", must be one of: Filters, MaxResults, NextToken, OwnerIds, RestorableByUserIds, SnapshotIds, DryRun",
  "errorType": "ParamValidationError",

代码

from __future__ import print_function
import botocore
import boto3
import urllib.request

def lambda_handler(event, context):
        ec2_client = boto3.client('ec2', region_name='eu-west-1')
        snapshot1 = ec2_client.create_snapshot(VolumeId='vol-054c95927bb8ed4a9', Description='Created by Lambda backup function ebs-snapshots')

       try:
            snapshot_id = snapshot1['SnapshotId']
            snapshot_complete_waiter = ec2_client.get_waiter('snapshot_completed')
            snapshot_complete_waiter.wait(SnapshotsId=['snapshot_id'])

        except botocore.exceptions.WaiterError as e:
            if "max attempts exceeded" in e.message:
                print("snapshot not completed")
            else:
                print(e.message)

您必须使用 SnapshotIds 而不是 SnapshotsId:

from __future__ import print_function
import botocore
import boto3
import urllib.request

def lambda_handler(event, context):
        ec2_client = boto3.client('ec2', region_name='eu-west-1')
        snapshot1 = ec2_client.create_snapshot(VolumeId='vol-054c95927bb8ed4a9', Description='Created by Lambda backup function ebs-snapshots')

       try:
            snapshot_id = snapshot1['SnapshotId']
            snapshot_complete_waiter = ec2_client.get_waiter('snapshot_completed')
            snapshot_complete_waiter.wait(SnapshotIds=[snapshot_id])

        except botocore.exceptions.WaiterError as e:
                print(e)

使用这个函数:

snapshot.wait_until_completed()

参考:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Snapshot.wait_until_completed