【问题标题】:AWS Lambda function snapshot start_time different to snapshot started timeAWS Lambda 函数快照 start_time 与快照开始时间不同
【发布时间】:2017-03-16 14:46:44
【问题描述】:

我使用 AWS Lambda 函数创建快照,并在快照描述中添加了已创建值。现在我正在尝试根据 Started 值删除快照,但是当我使用 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 始终是具有不同时间的日期,您可以在描述中看到日期时间与 Started 列中的日期不同。

快照实现

这是我的代码。让我知道是否有任何问题。也许我错过了什么,但我不知道它是什么。

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()

【问题讨论】:

  • 看起来Description字段的值是本地时间,而Started字段的值是UTC。
  • Started 字段实际上是 UTC-5(对应于最近时间更改之前的 3 月 1 日的 America/New_York)。但是由于没有显示代码,很难猜出它是如何做到的。

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


【解决方案1】:

“开始”字段是距离午夜 UTC 的 7 分钟。大概您位于该时区以东的时区,并在说明中使用了当地时间?

作为一般建议,对除最终用户表示层之外的所有内容使用 UTC 可以使您免于处理代码中的大量复杂问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 2017-03-18
    • 1970-01-01
    • 2022-01-18
    • 2011-10-22
    • 1970-01-01
    • 2017-10-31
    • 2017-09-03
    相关资源
    最近更新 更多