AWS Lambda函数快照start_time与快照启动时间不同

时间:2017-03-16 14:46:44

标签: amazon-web-services aws-lambda snapshot boto3

我使用AWS Lambda函数创建快照,并在快照描述中添加了创建的值。现在我尝试删除基于已启动值的快照,但是当我使用boto3 SDK请求此值时,实际上是不同的

| Snapshot ID            | Description                  | Started                            |
|:----------------------:|:----------------------------:|:----------------------------------:|
| snap-09829e2235d1c6ac9 | Description....at 02-03-2017 | March 1, 2017 at 11:53:04 PM UTC-5 |

所以我的问题是如何获取快照的 Started 值。我需要该值,但boto3始终是具有不同时间的日期,您可以在说明中看到日期时间与“已开始”列中的日期不同。

快照实施

这是我的代码。如果有什么不对的,请告诉我。也许我错过了一些东西,但我不知道它是什么。

AWS_ACCESS_KEY = os.environ['ACCESS_KEY']
AWS_SECRET_KEY = os.environ['SECRET_ACCESS_KEY']
REGION_NAME = os.environ['REGION_NAME']

period = 'hourly'

# Creating the client using access_key and secret key. 
client = boto3.client(
    'ec2',
    aws_access_key_id = AWS_ACCESS_KEY,
    aws_secret_access_key = AWS_SECRET_KEY ,
    region_name = REGION_NAME
)

rds_client = boto3.client(
    'rds',
    aws_access_key_id = AWS_ACCESS_KEY,
    aws_secret_access_key = AWS_SECRET_KEY ,
    region_name = REGION_NAME
)

ses_client = boto3.client(
    'ses',
    aws_access_key_id = AWS_ACCESS_KEY,
    aws_secret_access_key = AWS_SECRET_KEY ,
    region_name = REGION_NAME
)

ec2 = boto3.resource('ec2')

def get_volume_tags(volume_tags):
    result_tags = {}
    for tag in volume_tags:
            if not tag['Key'].startswith('aws:'):
                result_tags[tag['Key']] = tag['Value']
    return result_tags

def set_snapshot_tags(snapshot, tags):
    for tag_key, tag_value in tags.iteritems():
        snapshot.create_tags(
            Tags=[
                {
                'Key': tag_key,
                'Value': tag_value
                }
            ])

def get_volumes():
    return client.describe_volumes(
        Filters=[{
            'Name': 'tag:MakeSnapshot',
            'Values': [ 'True']
        }])['Volumes']


def create_volumes_snapshots():

    volumes = get_volumes()

    for volume in volumes:
        volume_id = volume['VolumeId']
        volume_tags = get_volume_tags(volume['Tags'])

        try:
            description = '%(period)s_snapshot %(vol_id)s by script at %(date)s' % {
            'period': period,
            'vol_id': volume_id,
            'date': datetime.today().strftime('%d-%m-%Y %H:%M:%S')
            }

            volume_resource = ec2.Volume(volume_id)
            snapshot = volume_resource.create_snapshot(Description=description)
            if snapshot:
                set_snapshot_tags(snapshot, volume_tags)
                print('Snapshot created with description: %(description)s' % { 'description': description })

        except Exception, e:
            error = 'Error creating volume: %(vol_id) at %(date)s' % {
            'vol_id': volume_id,
            'date': datetime.today().strftime('%d-%m-%Y %H:%M:%S')
            }
            print(error)
            # print(e)
            pass

def create_hourly_snapshot(event, context):

    # try:
        # creating volume snapshot process
    create_volumes_snapshots()

1 个答案:

答案 0 :(得分:0)

“已启动”字段为七分钟到午夜 UTC 。大概是你在那个东部的时区,并在描述中使用当地时间?

作为一般建议,除了最终用户表示层之外,使用UTC可以避免处理代码中的大量复杂问题。