AWS RDS:如何使用boto3和jmespath获取最新快照? [英] AWS RDS: how to get latest snapshot with boto3 and jmespath?

查看:91
本文介绍了AWS RDS:如何使用boto3和jmespath获取最新快照?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试过的:

#!/usr/bin/env python3
import boto3
import jmespath
from datetime import datetime, timedelta

now = datetime.utcnow()
yesterday = now - timedelta(days=1)

boto3.setup_default_session(profile_name='profilename')
rds_client = boto3.client('rds')

response = rds_client.describe_db_snapshots(DBInstanceIdentifier='instanseid')

snaplist=jmespath.search("DBSnapshots[?SnapshotCreateTime >`2016-10-24 06:11:30`].[DBSnapshotIdentifier]", response)

print(snaplist)

我得到的是:

TypeError: unorderable types: datetime.datetime() < str()

我尝试创建日期(脚本中的昨天)并将其传递给jmepath搜索,但是我不知道如何将日期对象传递给搜索. "+"在日期时间对象上不起作用,如果我用str()将其转换为字符串,我将返回到上面发布的错误.

I tried creating date (yesterday in the script) and passing it to jmepath search but I couldn't figure out how to pass that date object to the search. "+" doesn't work on datetime objects and if I convert it to sting with str() I return to the error posted above.

推荐答案

首先,

First, describe_db_snapshot response for SnapShotCreateTime is a datetime object.

'SnapshotCreateTime': datetime(2015, 1, 1)

因此,您正在尝试将日期时间与字符串进行比较.要解决此问题,您需要将字符串转换为日期时间,反之亦然.

So you are trying to compare datetime with string. To fix it, you need to convert the string to datetime or vice-versa.

search_date = datetime.strptime('2016-10-24 06:11:30', '%Y-%m-%d %H:%M:%s')
snaplist=jmespath.search(
    "DBSnapshots[?SnapshotCreateTime > search_date].[DBSnapshotIdentifier]",
    response)

您应该能够使用实现JMESpath的AWS CLI查询类似的内容,但是AWS CLI已经处理了字符串转换.日期值必须包含在反引号"中.

You should be able to query similar stuff using AWS CLI that implement JMESpath, however AWS CLI already taken care of the string conversion. And the date value must be embraced in `backticks`.

更多阅读:使用过滤器启动时间"来查找所有比X更新的实例日期

aws rds describe-db-snapshots --query 'DBSnapshots[?SnapshotCreateTime >`2016-10-24 06:11:30`].[DBSnapshotIdentifier]'

这篇关于AWS RDS:如何使用boto3和jmespath获取最新快照?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆