首页 > 解决方案 > python 错误使用 amazon ses 和 aws lambda 发送邮件

问题描述

我是 aws lambda 的新手。我正在尝试使用带有 aws lambda 的 aws ses 发送邮件,而没有任何触发器。这是我的代码

import boto3
from botocore.exceptions import ClientError

ses = boto3.client('ses')

email_from = 'proteeti@cloudly.io'
email_to = 'proteeti13@gmail.com'
emaiL_subject = 'Subject'
email_body = 'Body'


def lambda_handler(event, context):
    response = ses.send_email(
        Source = email_from,
        Destination={
            'ToAddresses': [
                email_to,
            ],
        },
        Message={
            'Subject': {
                'Data': emaiL_subject
            },
            'Body': {
                'Text': {
                    'Data': email_body
                }
            }
        }
    )

我创建了一个具有简单微服务权限的客户角色。事件设置为hello world。我保存并单击测试,它显示此错误

{
  "errorMessage": "An error occurred (AccessDenied) when calling the SendEmail operation: User `arn:aws:sts::990458801115:assumed-role/basic-lambda-role/sendmail' is not authorized to perform `ses:SendEmail' on resource `arn:aws:ses:us-east-1:990458801115:identity/proteeti@cloudly.io'",
  "errorType": "ClientError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      28,
      "lambda_handler",
      "'Data': email_body"
    ],
    [
      "/var/runtime/botocore/client.py",
      314,
      "_api_call",
      "return self._make_api_call(operation_name, kwargs)"
    ],
    [
      "/var/runtime/botocore/client.py",
      612,
      "_make_api_call",
      "raise error_class(parsed_response, operation_name)"
    ]
  ]
}

我从这里编写了代码,它在本地完美运行。

标签: pythonamazon-web-servicesaws-lambdaamazon-ses

解决方案


您在其中运行此代码的 Lambda 函数无权使用 SES 发送消息。您需要将操作添加ses:SendEmail到您的basic-lambda-roleIAM 角色。

当您在本地运行代码时,您将使用您自己的开发人员凭据与 SES 进行通信,该凭据可能具有更高的权限。


推荐阅读