AWS StepFunction:将错误作为“错误”:“ KeyError”

时间:2018-10-30 02:27:12

标签: amazon-web-services aws-lambda boto3 aws-step-functions

我是AWS和Step功能的新手。我正在尝试使用Lambda函数从Step函数运行简单程序。我已经尝试过下面的代码来运行Step函数。

代码:

import boto3
import json
import os



def lambda_handler(event, context):


    subject = event['Mail']['subject']
    toList = event['Mail']['mailTo'] 
    message = event['MailMessage']['message']
    status = ""


    body = message 

    subject="["+status+"]"+subject


    for to in toList.split(","):
        sendMail(to, ADMIN_EMAIL, subject, body)

    return event


def sendMail(to, reply, subject, body):
    client = boto3.client('ses', region_name=region_name)
    response = client.send_email(
        Source=reply,
        Destination={
            'ToAddresses': [
                to,
            ]
        },
        Message={
            'Subject': {
                'Data': subject,
            },
            'Body': {
                'Text': {
                    'Data': body,
                },
            }
        },
        ReplyToAddresses=[
            reply,
        ],
        ReturnPath=reply
    )
    return response

运行“步进”功能后,出现以下错误。

{
  "error": "KeyError",
  "cause": {
    "errorMessage": "'Mail'",
    "errorType": "KeyError",
    "stackTrace": [
      [
        "/var/task/lambda_function.py",
        11,
        "lambda_handler",
        "subject = event['Mail']['subject']"
      ]
    ]
  }
}

我的步进功能:

{
  "Comment": "A Sample program to send an email",
  "StartAt": "SampleMail",
  "States": {
    "SampleMail": {
      "Type": "Task",
     "Resource": "arn:aws:lambda:us-west-1:000000123:function:TestEmail", 

      "End": true
    }
  }
}

能告诉我我在这里想念的吗?

请帮助我。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

传递给您的event函数的lambda_handler没有名为“邮件”的键,因此此行

subject = event['Mail']['subject']

失败。在使用该键检索任何值之前,应验证event参数具有“邮件”属性。