简体   繁体   English

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

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

My setup is the following: 我的设置如下:

  • Lambda provides an API endpoint to a function which sends out an email via AWS SES Lambda为通过AWS SES发送电子邮件的功能提供API端点
  • API endpoint is called from static JS file on a S3 bucket API端点是从S3存储桶上的静态JS文件调用的

This works: 这有效:

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...
};

I would like to remove the credentials from the function and rather let Lambda get them from somewhere else. 我想从函数中删除凭据,而是让Lambda从其他地方获取它们。

If I remove the credentials I get: 如果我删除凭据,我会得到:

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\'

I'm still trying to wrap my head around policies, roles and credentials. 我仍然试图围绕政策,角色和凭据。 I first thought Lambda might be able to get the credentials from S3 environment variables, but I don't have a clue how to set these or if that's the correct approach anyway. 我首先想到Lambda可能能够从S3环境变量中获取凭据,但我不知道如何设置这些或者无论如何这是正确的方法。

Would be great if someone could give me a hint how this might work. 如果有人能给我一个暗示这可能如何起作用的话会很棒。 Or if it's not possible. 或者如果不可能的话。

My main reason to remove the credentials form the Lambda function is that I want to add the function code to a git repo. 我从Lambda函数中删除凭据的主要原因是我想将函数代码添加到git仓库。 And I feel bad about adding those credentials to the code repo. 我对将这些凭据添加到代码仓库感到很难过。

When you created your lambda function, you created an IAM Role with sufficient permissions to execute the function itself, but not to perform actions on any other AWS services. 创建lambda函数时,您创建了一个具有足够权限的IAM角色来执行函数本身,但不对任何其他AWS服务执行操作。 From the documentation: 从文档:

Regardless of how your Lambda function is invoked, AWS Lambda always executes the function. 无论您如何调用Lambda函数,AWS Lambda始终执行该函数。 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. 在创建Lambda函数时,您可以指定AWS Lambda可以代表您执行Lambda函数的IAM角色。 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. 如果您的Lambda函数在执行期间访问其他AWS资源(例如,要在Amazon S3存储桶中创建对象,从DynamoDB表读取项目或将日志写入CloudWatch Logs),则需要授予执行角色权限对于要使用Lambda函数执行的特定操作。

Consequently, your new IAM role doesn't have permissions to perform SES send actions. 因此,您的新IAM角色无权执行SES发送操作。

From the web console or CLI, you can find this IAM role and update the existing inline policy (or attach a new one) to allow Send Email actions: 在Web控制台或CLI中,您可以找到此IAM角色并更新现有内联策略(或附加新策略)以允许发送电子邮件操作:

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

From my reading of your question, it seems that S3 is largely irrelevant to the execution role, if you're only using it for a static page hosted there with a link to the API endpoint. 从我对你的问题的解读看来,如果你只是将它用于那里托管的静态页面,并且有一个指向API端点的链接,那么S3似乎与执行角色无关。 If you needed to list/get s3 objects from the function itself, you would likewise need to include those permissions in your IAM role. 如果需要从函数本身列出/获取s3对象,则同样需要在IAM角色中包含这些权限。

Further Reading: 进一步阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM