python - 如何从 AWS Python Lambda 获取响应
问题描述
我已经编写了一些我在 AWS Lambda 中运行的 python 代码。当我在 Lambda AWS 仪表板中测试 Lambda 并且它运行时没有任何错误时,我在“执行结果”选项卡中看到以下内容:
Response: null
Request ID: "421fd7da-20f7-4029-aa8b-f7281e7c90d9"
如果我在运行 Lambda 时遇到任何错误,我会在“执行结果”选项卡中看到 JSON 格式的输出。
这是一个例子:
Response:
{
"errorMessage": "An error occurred (DBClusterNotFoundFault) when calling the CreateDBClusterSnapshot operation: DBCluster not found: ernie-export-test-db-clusterr",
"errorType": "DBClusterNotFoundFault",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 90, in main\n response = create_snapshot(rds, snaptype, datestamp, deleteAfterDate)\n",
" File \"/var/task/lambda_function.py\", line 33, in create_snapshot\n 'Value': deleteafterdate\n",
" File \"/var/runtime/botocore/client.py\", line 272, in _api_call\n return self._make_api_call(operation_name, kwargs)\n",
" File \"/var/runtime/botocore/client.py\", line 576, in _make_api_call\n raise error_class(parsed_response, operation_name)\n"
]
}
如何将此“响应”返回到我的 Python 代码中,以便我可以使用/阅读它?特别是我想阅读“errorMessage”,以便我在 Lambda 中的 python 代码可以将其打印出来或转发到 - 可以说在 SNS 中使用。
我添加了一个带有响应的返回,但它的内容与我在“执行结果”选项卡中得到的响应不匹配。
这是我的 Python 代码 -
from __future__ import print_function
from boto3 import client
import datetime
from datetime import datetime
# Database cluster identifier that the backup will be performed on
CLUSTER_ID = "export-test-db-cluster"
# Name of the company that the script is used on
COMPANY = "Test"
# AWS region in which the db instances exist
REGION = "us-east-1"
def create_snapshot(rds, snaptype, datestamp, deleteafterdate):
snapname = COMPANY + "-" + snaptype + "-" + datestamp
response = rds.create_db_cluster_snapshot(
DBClusterSnapshotIdentifier=snapname,
DBClusterIdentifier=CLUSTER_ID,
Tags=[
{
'Key': 'Name',
'Value': snapname
},
{
'Key': 'expirationDate',
'Value': deleteafterdate
},
]
)
return response
def main(event, context):
rds = client("rds", region_name=REGION)
now = datetime.now()
# Should we leave time in the name?
datestamp = now.strftime("%m-%d-%Y-%H-%M-%S")
snaptype = "TestBackup"
deleteAfterDate = "Today-test"
create_snapshot(rds, snaptype, datestamp, deleteAfterDate)
任何帮助表示赞赏!
解决方案
应该是这样的。
from __future__ import print_function
from boto3 import client
import datetime
from datetime import datetime
# Database cluster identifier that the backup will be performed on
CLUSTER_ID = "export-test-db-cluster"
# Name of the company that the script is used on
COMPANY = "Test"
# AWS region in which the db instances exist
REGION = "us-east-1"
def create_snapshot(rds, snaptype, datestamp, deleteafterdate):
snapname = COMPANY + "-" + snaptype + "-" + datestamp
try:
response = rds.create_db_cluster_snapshot(
DBClusterSnapshotIdentifier=snapname,
DBClusterIdentifier=CLUSTER_ID,
Tags=[
{
'Key': 'Name',
'Value': snapname
},
{
'Key': 'expirationDate',
'Value': deleteafterdate
},
]
)
except Exception as e:
response = e
pass
return response
def main(event, context):
rds = client("rds", region_name=REGION)
now = datetime.now()
# Should we leave time in the name?
datestamp = now.strftime("%m-%d-%Y-%H-%M-%S")
snaptype = "TestBackup"
deleteAfterDate = "Today-test"
my_response = create_snapshot(rds, snaptype, datestamp, deleteAfterDate)
推荐阅读
- javascript - 制造商 -> 模型链选择,从 JSON 中提取数据。如何配合现有的 refreshSelect 功能?
- c# - 使用 MS-Teams 通过机器人记录团队通话
- python - 如何在 HitBox 中移动图像?游戏
- rxjs - 来自 Observables 数组的 MergeMap
- php - 调用 shell_exec() 而不等待输出 PHP
- django - Django Rest Framework:除非我重新启动服务器,否则不显示最新记录
- algorithm - 有缺陷的棋盘问题——寻找伪代码算法(分治法)
- python - 尝试使用多个连接字符串循环尝试捕获问题,直到成功
- spring-data-mongodb - Spring Data Mongo:按示例查询(QBE)不适用于没有“_class”属性的记录
- python - Django 静态图像没有加载变量