将 AWS SES 与从 S3 调用的 Lambda 函数 (nodejs) 结合使用

Using AWS SES with a Lambda function (nodejs) called from S3

我的设置如下:

这个有效:

var exec = require('child_process').exec;

var aws = require('aws-sdk');
var ses = new aws.SES({
  "accessKeyId": "MY_ACCESS_KEY",
  "secretAccessKey": "MY_SECRET_ACCESS_KEY",
  "region": "A_REGION"
});

var ses = new aws.SES();

exports.handler = function(event, context) {
    ...code to send email...
};

我想从函数中删除凭据,而是让 Lambda 从其他地方获取它们。

如果我删除我得到的凭据:

User `arn:aws:sts::1234567890:assumed-role/lambda_basic_execution/awslambda_1234567890\' is not authorized to perform `ses:SendEmail\' on resource `arn:aws:ses:us-region-123:1234567890:identity/my.identity@domain.com\'

我仍在努力思考政策、角色和证书。 我首先想到 Lambda 可能能够从 S3 环境变量中获取凭据,但我不知道如何设置这些凭据,也不知道这是否是正确的方法。

如果有人能给我提示这是如何工作的,那就太好了。或者如果这是不可能的。

我从 Lambda 函数中删除凭据的主要原因是我想将函数代码添加到 git 存储库。我对将这些凭据添加到代码存储库感到遗憾。

当您创建 lambda 函数时,您创建了一个 IAM 角色,该角色具有足够的权限来执行该函数本身,但不能对任何其他 AWS 服务执行操作。来自文档:

Regardless of how your Lambda function is invoked, AWS Lambda always executes the function. At the time of creating a Lambda function, you specify an IAM role that AWS Lambda can assume to execute your Lambda function on your behalf. This role is also referred to as the execution role. If your Lambda function accesses other AWS resources during execution (for example, to create an object in an Amazon S3 bucket, to read an item from a DynamoDB table, or to write logs to CloudWatch Logs), you need to grant the execution role permissions for the specific actions that you want to perform using your Lambda function.

因此,您的新 IAM 角色无权执行 SES 发送操作。

从 Web 控制台或 CLI,您可以找到此 IAM 角色和 update the existing inline policy (or attach a new one) 以允许发送电子邮件操作:

{
    "Version": "2012-10-17",
    "Statement": [
    {
       "Effect": "Allow",
       "Action": ["ses:SendEmail", "ses:SendRawEmail"],
       "Resource":"*"
     }
    ]
 }

从我对你的问题的阅读来看,S3 似乎与执行角色在很大程度上无关,如果你只是将它用于那里托管的静态页面,并且 link 到 API 端点。如果您需要从函数本身 list/get s3 对象,您同样需要在您的 IAM 角色中包含这些权限。

进一步阅读: