0

I'm trying to set up a project environment to develop lambda functions both in javascript and typescript using VS code and the AWS toolkit. I migrated my (working) lambda code into the hello-world example and changed the template.yaml so that it's able to run locally. However, the SES.sendMail doesn't seem to work. I get a "Function 'awsToolkitSamLocalResource' timed out after 3 seconds" error

I installed all pre-requisites and I'm able to run a simple hello-world function locally through the toolkit. The problem seems to be the SES

Here is my lambda, I replaced the hardcoded email addresses if you don't mind

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

and my yaml file

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"

I expect it's some kind of IAM or SAM configuration error, in that the toolkit doesn't reach the actual SES

2 Answers2

0

I found the solution. Our office blocks all ports except 80 and 443. When I switched to my hotspot it worked like a charm.

0

Actually, this is due to the timeout configuration set on your template.yaml . You need to change the value from 3 to something higher because you are calling an external API and that makes it slow-ish.

Globals:
  Function:
    Timeout: 30

Further explanation: https://github.com/aws/aws-toolkit-vscode/issues/510

guevaraf
  • 71
  • 1
  • 3