【发布时间】: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