如何从AWS Python Lambda获取响应

如何从AWS Python Lambda获取响应,python,aws-lambda,Python,Aws Lambda,我已经编写了一些在AWS Lambda中运行的python代码。当我在Lambda AWS仪表板中测试Lambda时,它运行时没有任何错误,我在“执行结果”选项卡中看到以下内容: 如果在运行Lambda时出现任何错误,我会在“执行结果”选项卡中看到JSON格式的输出 以下是一个例子: Response: { "errorMessage": "An error occurred (DBClusterNotFoundFault) when calling the CreateDBClusterS

我已经编写了一些在AWS Lambda中运行的python代码。当我在Lambda AWS仪表板中测试Lambda时,它运行时没有任何错误,我在“执行结果”选项卡中看到以下内容:

如果在运行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)

你能分享你收到的
response code
response data
吗?你不能在try catch中添加集群快照的创建并存储错误吗?我该怎么做?
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)