0

我正在尝试建立一个项目环境,以使用 VS 代码和 AWS 工具包在 javascript 和 typescript 中开发 lambda 函数。我将我的(工作)lambda 代码迁移到 hello-world 示例中并更改了 template.yaml 以便它能够在本地运行。但是, SES.sendMail 似乎不起作用。我收到“函数 'awsToolkitSamLocalResource' 在 3 秒后超时”错误

我安装了所有先决条件,并且能够通过工具包在本地运行一个简单的 hello-world 函数。问题似乎是 SES

这是我的 lambda,如果您不介意,我替换了硬编码的电子邮件地址

const AWS = require('aws-sdk');
const SES = new AWS.SES({ region: 'eu-west-1' });

exports.handler = async (params)  => {
    try {
        console.log(params);

        const {
          to,
          from,
          reply_to: replyTo,
          subject,
        } = params;
        const fromBase64 = Buffer.from(from).toString('base64');

        const htmlBody = `
          <!DOCTYPE html>
          <html>
            <head></head>
            <body><h1>Hello world!</h1></body>
          </html>
        `;

        const sesParams = {
          Destination: {
            ToAddresses: ['mycompany@mail.com'],
          },
          Message: {
            Body: {
              Html: {
                Charset: 'UTF-8',
                Data: htmlBody,
              },
            },
            Subject: {
              Charset: 'UTF-8',
              Data: subject,
            },
          },
          Source: 'mygmail@gmail.com',
        };

        console.log('GOING TO SES')

        const response = await SES.sendEmail(sesParams).promise();

        console.log('DONE')

        console.log(response);
    } catch(error) {
        console.log(error)
    }
};

和我的 yaml 文件

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sammy

  Sample SAM Template for sammy

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      Policies:
        - AmazonSESFullAccess
      CodeUri: src/hello-world/build
      Handler: sendMail.handler
      Runtime: nodejs10.x
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

provider:
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "ses:SendEmail"
      Resource:
        - "*"
      Condition:
        StringEquals:
          ses:FromAddress:
            - "mygmail@gmail.com"

我预计这是某种 IAM 或 SAM 配置错误,因为工具包没有达到实际的 SES

4

2 回答 2

0

我找到了解决方案。我们的办公室封锁了除 80 和 443 之外的所有端口。当我切换到我的热点时,它就像一个魅力。

于 2019-09-16T20:06:06.373 回答
0

实际上,这是由于在您的 template.yaml 上设置了超时配置。您需要将值从 3 更改为更高的值,因为您正在调用外部 API,这使得它变得缓慢。

Globals:
  Function:
    Timeout: 30

进一步解释:https ://github.com/aws/aws-toolkit-vscode/issues/510

于 2020-05-31T21:29:35.083 回答