python - 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)"
]
]
}
我从这里编写了代码,它在本地完美运行。
解决方案
您在其中运行此代码的 Lambda 函数无权使用 SES 发送消息。您需要将操作添加ses:SendEmail
到您的basic-lambda-role
IAM 角色。
当您在本地运行代码时,您将使用您自己的开发人员凭据与 SES 进行通信,该凭据可能具有更高的权限。
推荐阅读
- html - 将长 html 分成几个部分并使用 PHP 或 AJAX 加载它们会有所帮助吗
- c# - 我可以在我的 WPF 项目中控制 Windows 设置焦点辅助(开/关)吗
- cocoapods - xcodebuild:错误:意外重复任务:CopyStringsFile
- laravel - 总是不授权 Jwt-auth Laravel
- python - 索引未重置
- scala - 哪个是更好的多案例类来映射多级json或在读取方法中手动读取它
- c# - DropDownList DataBind 不能与 UpdatePanel 一起使用?
- angular - 在执行以下代码执行之前,如何等待函数以 Angular 6 完成其执行?
- c# - .NET 线程和垃圾收集/幻象进程
- css - 引导模式未正确弹出