使用 lambda 函数在不同区域触发 SES

Use lambda function to trigger SES on different region

我在 ap-southeast-1 上有一个 lambda 函数。 最初我在同一地区设置了一个沙盒电子邮件地址,我的代码有效。

现在我需要在 eu-west-1 上使用经过验证的 SES 电子邮件地址来触发通知。

我怎样才能做到这一点?

我的 serverless.yml 文件如下所示(省略不相关的部分):

custom: ${file(env.yml)}

provider:
  name: aws
  runtime: go1.x
  stage: ${opt:stage, 'dev'}
  region: ${self:custom.REGION, 'ap-southeast-1'}
  environment: ${file(env.yml)}
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query            
      Resource: "arn:aws:dynamodb:*:*:*"
    - Effect: Allow
      Action:
        - ses:SendEmail
        - ses:SendRawEmail
      Resource: "arn:aws:ses:${env:SES_REGION}:${env:ACCOUNT_ID}:identity/*"


functions:
  notify:
    handler: bin/notify
    events:
      - http:
          path: notify
          method: post
          cors: true
          authorizer:
            arn: "arn:aws:cognito-idp:${self:provider.region}:${self:custom.ACCOUNT_ID}:userpool/${self:custom.USER_POOL_ID}"  

    

env.yml 文件 (SES_REGION=eu-west-1):

AWS_PROFILE: ${env:AWS_PROFILE}
RELEASE_STAGE: ${env:RELEASE_STAGE}
REGION: ${env:REGION}
SES_REGION: ${env:SES_REGION}
ACCOUNT_ID: ${env:ACCOUNT_ID}
USER_POOL_ID: ${env:USER_POOL_ID}
MAIL_SENDER: ${env:MAIL_SENDER}

我遇到错误:

"error_message":"AccessDenied: User `arn:aws:sts::41310816xxxx:assumed-role/xxx-remarks-api-dev-ap-southeast-1-lambdaRole/xxx-remarks-api-dev-notify' is not authorized to perform `ses:SendEmail' on resource `arn:aws:ses:ap-southeast-1:41310816xxxx:identity/noreply@xxx'

我注意到错误提到 'ap-southeast-1' 上的 ses 资源。这是错误的原因吗?如果是这样,我该如何强制更新我的代码?

似乎您的 lambda 没有 运行 SES

所需的权限

第 1 步: 转到 IM 角色并编辑策略,然后打开 JSON 并添加以下代码

  {
   "Effect":"Allow",
   "Action":[
   "ses:SendEmail",
   "ses:SendRawEmail"
    ],
    "Resource":"*"
    }

第 2 步: 除了您的 Lambda 调用代码之外,SES 并非在所有地区都可用,因此请确保您的地区支持 SES。

        var aws = require('aws-sdk');
        var lambda = new aws.Lambda({
         region: 'eu-west-1' //change to your region
        });

这要感谢上面接受的答案。我需要在 ap-southeast-1.

中调用我的 lambda 函数时指定区域

serverless.yml:

provider:
  name: aws
  runtime: go1.x
  stage: ${opt:stage, 'dev'}
  region: ap-southeast-1
  environment: ${file(env.yml)}
  iamRoleStatements:
    - Effect: Allow
      Action:
        - ses:SendEmail
        - ses:SendRawEmail
      Resource: "arn:aws:ses:*:*:*"

在代码中 - 指定 ses 服务所在的区域,而不是使用默认区域:

    // Send notification
    func (n *Notification) Notify() error {
        // svc := ses.New(session.New())
        svc := ses.New(session.New(), aws.NewConfig().WithRegion("eu-west-1"))
        htmlFile := "notify.html"
        txtFile := "notify.txt"
        : bla bla
}