【问题标题】:Delet old Manual Cluster Snaphots RDS using AWS LAMBDA [closed]使用 AWS LAMBDA 删除旧的手动集群快照 RDS [关闭]
【发布时间】:2021-01-29 03:11:37
【问题描述】:

在我的 AWS Lambda 的 python 代码下面,我想通过使用它来删除 OLD MANUAL RDS SNAPSHOTS,但它仍然无法解决错误,我需要帮助来调试和更正这个 lambda 脚本。非常感谢。

import boto3
from os import getenv
import datetime
from datetime import date
client = boto3.client('rds')
ClientName = getenv('CLIENT_NAME')
today = date.today()

def lambda_handler(event, context):
    delete_db_cluster_snapshot():
    snapshots_marker = ""
    while snapshots_marker != None:
        snapshots = client.describe_db_cluster_snapshots(Marker=snapshots_marker)
        
        if 'Marker' in snapshots:
            snapshots_marker = snapshots['Marker']
        else:
            snapshots_marker = None
            
        for snapshot in snapshots['DBClusterSnapshots']:
            if snapshot["SnapshotType"] == "manual" and ClientName in snapshot["DBClusterIdentifier"] and snapshot ["SnapshotCreateTime"].date() < today:
                client.delete_db_cluster_snapshot(DBClusterSnapshotIdentifier=snapshot["DBClusterSnapshotIdentifier"])
                
delete_db_cluster_snapshot()

【问题讨论】:

  • 错误是什么?

标签: python amazon-web-services aws-lambda amazon-rds aws-serverless


【解决方案1】:

您的代码看起来不错,但您应该删除 delete_db_cluster_snapshot() 子函数:

import boto3
from os import getenv
import datetime
from datetime import date

client = boto3.client('rds')
ClientName = getenv('CLIENT_NAME')
today = date.today()

def lambda_handler(event, context):
    snapshots_marker = ""
    while snapshots_marker != None:
        snapshots = client.describe_db_cluster_snapshots(Marker=snapshots_marker)
        
        if 'Marker' in snapshots:
            snapshots_marker = snapshots['Marker']
        else:
            snapshots_marker = None
            
        for snapshot in snapshots['DBClusterSnapshots']:
            if snapshot["SnapshotType"] == "manual" and ClientName in snapshot["DBClusterIdentifier"] and snapshot ["SnapshotCreateTime"].date() < today:
                client.delete_db_cluster_snapshot(DBClusterSnapshotIdentifier=snapshot["DBClusterSnapshotIdentifier"])

【讨论】:

  • 你好约翰,我把函数删除删除 delete_db_cluster_snapshot() 我有这个错误:{“errorMessage”:“'in '需要字符串作为左操作数,而不是NoneType”,“errorType” : "TypeError", "stackTrace": [ " File \"/var/task/lambda_function.py\", line 21, in lambda_handler\n if snapshot[\"SnapshotType\"] == \"manual\" and ClientName在快照 [\"DBClusterIdentifier\"] 和快照 [\"SnapshotCreateTime\"].date()
  • 错误是说ClientName是空的(NoneType)。
  • 所以我应该用我的 rds 标识符替换('CLIENT_NAME')?
  • getenv('CLIENT_NAME') 正在从环境变量中检索一个值,您可以在 Lambda 函数本身(在控制台中)配置该值。如果您没有提供该值,那么这将解释代码失败的原因。
  • 嗨约翰,最后我的 lambda 工作正常,非常感谢您的帮助!!!!!!!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 2018-06-15
相关资源
最近更新 更多