【问题标题】:ClientError: An error occurred (AccessDenied) when calling the SendEmail operation:ClientError:调用 SendEmail 操作时发生错误 (AccessDenied):
【发布时间】:2020-04-24 13:24:35
【问题描述】:

每当文件到达 s3 存储桶的特定区域时,我想使用 lambda 发送自动电子邮件。当我将文件放在给定位置的存储桶中并尝试运行时,我在 cloudwatch 日志中收到以下错误:

[ERROR] ClientError: An error occurred (AccessDenied) when calling the SendEmail operation: User `arn:aws:sts::123456789012:assumed-role/my-bot-role-abcdefg/my-bot' is not authorized to perform `ses:SendEmail' on resource `arn:aws:ses:eu-west-1:123456789012:identity/my_address@gmail.com'
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 44, in lambda_handler
    Message = message   )
  File "/var/runtime/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 626, in _make_api_call
    raise error_class(parsed_response, operation_name)

我已将以下权限写入 json 策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "logs:*"
            ],
            "Effect": "Allow",
            "Resource": "*"
        },
        {
            "Action": [
                "ses:SendEmail"
            ],
            "Effect": "Allow",
            "Resource": "arn:aws:ses:eu-west-1:123456789012:identity/*"
        }
    ]
}

在我计划为特定目的修改之前,我在 lambda 句柄中使用的代码的基础不是我自己的:

import json
import boto3

email_defaults = {
    "port" : 465,  # For SSL
    "smtp_server" : "smtp.gmail.com",
    "sender_email" : "my_sender_address@gmail.com", #replace as needed in testing
    "receiver_email" : "my_reciever_address@gmail.co.uk" #replace as needed in testing
    }

def lambda_handler(event, context):

    for i in event["Records"]:
        action = i["eventName"]
        ip = i["requestParameters"]["sourceIPAddress"]
        bucket_name = i["s3"]["bucket"]["name"]
        object = i["s3"]["object"]["key"]

    client = boto3.client("ses")

    subject = str(action) + 'Event from ' + bucket_name
    body = """
        <br>
        This email is to notify you regarding {} event.
        The object {} has been uploaded.
        Source IP: {}
    """.format(action, object, ip)

    message = {"Subject": {"Data": subject}, "Body": {"Html": {"Data": body}}}

    response = client.send_email(   Source = email_defaults["sender_email"], 
                                    Destination = {"ToAddresses": [email_defaults["receiver_email"]]}, 
                                    Message = message   )

    return "Hello world"

对于我可能忽略的任何事情的任何指导将不胜感激,但我目前很困惑。

【问题讨论】:

    标签: python amazon-web-services aws-lambda amazon-iam


    【解决方案1】:

    您必须添加允许向您的角色发送电子邮件的策略。

    在控制台中,转到您的 IAM 服务

    然后,在左侧选择角色:

    选择您的角色:

    然后添加内联策略

    然后查看并保存。并做了!您已准备好通过 lambda 发送电子邮件

    【讨论】:

    • 太棒了,看来我的权限不够高。我现在获得了更高的权限,并且电子邮件已发送,谢谢:)
    • 我们不能通过 serverless.yml 提供这个权限吗?
    • @bSr 当然!您可以从无服务器文件创建角色和策略,并通过 SAM 或无服务器进行部署
    • 您的政策非常广泛,请将其限制在您的应用程序范围内,例如 sendMail
    • @abhishukla 是的,它仅用于测试目的
    猜你喜欢
    • 1970-01-01
    • 2021-09-23
    • 2020-01-16
    • 2020-07-08
    • 1970-01-01
    • 2020-06-07
    • 2020-05-06
    • 2017-09-28
    • 1970-01-01
    相关资源
    最近更新 更多