【问题标题】:Getting error while testing AWS Lambda function: "Invalid database identifier"测试 AWS Lambda 函数时出错:“无效的数据库标识符”
【发布时间】:2022-09-27 17:37:43
【问题描述】:

嗨,我在测试 lambda 函数时遇到错误,例如:

{
  \"errorMessage\": \"An error occurred (InvalidParameterValue) when calling the DescribeDBInstances operation: Invalid database identifier:  <RDS instance id>\",
  \"errorType\": \"ClientError\",
  \"stackTrace\": [
    \"  File \\\"/var/task/lambda_function.py\\\", line 25, in lambda_handler\\n    db_instances = rdsClient.describe_db_instances(DBInstanceIdentifier=rdsInstanceId)[\'DBInstances\']\\n\",
    \"  File \\\"/var/runtime/botocore/client.py\\\", line 391, in _api_call\\n    return self._make_api_call(operation_name, kwargs)\\n\",
    \"  File \\\"/var/runtime/botocore/client.py\\\", line 719, in _make_api_call\\n    raise error_class(parsed_response, operation_name)\\n\"
  ]
}

这是我的 lambda 代码:

import json
import boto3
import logging
import os

#Logging
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)

#Initialise Boto3 for RDS
rdsClient = boto3.client(\'rds\')

def lambda_handler(event, context):

    #log input event
    LOGGER.info(\"RdsAutoRestart Event Received, now checking if event is eligible. Event Details ==> \", event)

    #Input event from the SNS topic originated from RDS event notifications
    snsMessage = json.loads(event[\'Records\'][0][\'Sns\'][\'Message\'])
    rdsInstanceId = snsMessage[\'Source ID\']
    stepFunctionInput = {\"rdsInstanceId\": rdsInstanceId}
    rdsEventId = snsMessage[\'Event ID\']

    #Retrieve RDS instance ARN
    db_instances = rdsClient.describe_db_instances(DBInstanceIdentifier=rdsInstanceId)[\'DBInstances\']
    db_instance = db_instances[0]
    rdsInstanceArn = db_instance[\'DBInstanceArn\']

    # Filter on the Auto Restart RDS Event. Event code: RDS-EVENT-0154. 

    if \'RDS-EVENT-0154\' in rdsEventId:

        #log input event
        LOGGER.info(\"RdsAutoRestart Event detected, now verifying that instance was tagged with auto-restart-protection == yes\")

        #Verify that instance is tagged with auto-restart-protection tag. The tag is used to classify instances that are required to be terminated once started. 

        tagCheckPass = \'false\'
        rdsInstanceTags = rdsClient.list_tags_for_resource(ResourceName=rdsInstanceArn)
        for rdsInstanceTag in rdsInstanceTags[\"TagList\"]:
            if \'auto-restart-protection\' in rdsInstanceTag[\"Key\"]:
                if \'yes\' in rdsInstanceTag[\"Value\"]:
                    tagCheckPass = \'true\'
                    #log instance tags
                    LOGGER.info(\"RdsAutoRestart verified that the instance is tagged auto-restart-protection = yes, now starting the Step Functions Flow\")
                else:
                    tagCheckPass = \'false\'


        #log instance tags
        LOGGER.info(\"RdsAutoRestart Event detected, now verifying that instance was tagged with auto-restart-protection == yes\")

        if \'true\' in tagCheckPass:

            #Initialise StepFunctions Client
            stepFunctionsClient = boto3.client(\'stepfunctions\')

            # Start StepFunctions WorkFlow
            # StepFunctionsArn is stored in an environment variable
            stepFunctionsArn = os.environ[\'STEPFUNCTION_ARN\']
            stepFunctionsResponse = stepFunctionsClient.start_execution(
            stateMachineArn= stepFunctionsArn,
            name=event[\'Records\'][0][\'Sns\'][\'MessageId\'],
            input= json.dumps(stepFunctionInput)

        )

    else:

        LOGGER.info(\"RdsAutoRestart Event detected, and event is not eligible\")

    return {
            \'statusCode\': 200
        }

我正在尝试停止 7 天后自动启动的 Amazon RDS 数据库。我正在关注此 AWS 文档:Field Notes: Stopping an Automatically Started Database Instance with Amazon RDS | AWS Architecture Blog

谁能帮我?

  • 我用过上面的文件

标签: amazon-web-services aws-lambda amazon-rds amazon-sns aws-step-functions


【解决方案1】:

错误信息是:Invalid database identifier: <RDS instance id>"

它似乎来自这条线:

db_instances = rdsClient.describe_db_instances(DBInstanceIdentifier=rdsInstanceId)['DBInstances']

错误消息是说rdsInstanceId 变量包含<RDS instance id>,这似乎是一个示例值而不是实际值。

在查看Field Notes: Stopping an Automatically Started Database Instance with Amazon RDS | AWS Architecture Blog 上的代码时,它要求您创建测试事件包括此消息:

"Message": "{\"Event Source\":\"db-instance\",\"Event Time\":\"2020-07-09 15:15:03.031\",\"Identifier Link\":\"https://console.aws.amazon.com/rds/home?region=<region>#dbinstance:id=<RDS instance id>\",\"Source ID\":\"<RDS instance id>\",\"Event ID\":\"http://docs.amazonwebservices.com/AmazonRDS/latest/UserGuide/USER_Events.html#RDS-EVENT-0154\",\"Event Message\":\"DB instance started\"}",

如果您仔细查看该行,它包含用于识别 Amazon RDS 实例的这一部分:

dbinstance:id=<RDS instance id>

我认为你应该修改提供的测试事件&lt;angled brackets&gt; 中的任何内容填写您自己的值(例如您的 Amazon RDS 实例的实例 ID)。

【讨论】:

    猜你喜欢
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多