簡體   English   中英

使用aws lambda使用amazon ses發送郵件的python錯誤

[英]python error sending mail with amazon ses with 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 進行通信,這些憑據可能具有更高的權限。

您使用的角色似乎沒有關於 SES 服務的相關政策

步驟 1:創建自定義策略- 例如: SES-SendEmail-Policy並為其提供以下 JSON:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ses:SendEmail",  <--- This is the action that was missing
            ],
            "Resource": "*"
        }
    ]
}

步驟 2:將SES-SendEmail-Policy附加到basic-lambda-role角色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM