19

So I'm trying to convert an existing spring boot application to an AWS lambda and using SAM.

I'm trying to use aws-sam-cli to try my lambda locally, however with my SAM setup I am getting: Template does not have any APIs connected to Lambda functions

When I do: sam local start-api

My template.yml:

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: foo
Resources:
  MailFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: bar.LambdaHandler::handleRequest
      Runtime: java8
      CodeUri: target/foo-bar-1.0.jar
      Timeout: 300
      MemorySize: 1024
      Events:
        Timer:
          Type: Schedule
          Properties:
            Schedule: rate(1 day)

Any idea what I'm doing wrong? It looks correct as far as I can tell from https://blog.couchbase.com/aws-serverless-lambda-scheduled-events-tweets-couchbase/ + https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html

Kristof Plennings
  • 469
  • 1
  • 6
  • 17

6 Answers6

20

You didn't add any API Gateway event to your function. And start-api spawn a local API Gateway.

You need to add at least one Api event to your Events section.

Events:
  [...]
  Api:
    Type: Api
    Properties:
      Path: /myresource
      Method: get

If you just have a Schedule event, try to use generate-event to create such an event.

sam local generate-event schedule ...

and invoke function e.g. sam local invoke function-name -e event_file.json (see)

disco crazy
  • 31,313
  • 12
  • 80
  • 83
  • Ah, any way to set the lambda up so it just gets triggered periodically (once/day) without it being started by invoking that path? – Kristof Plennings Jun 04 '18 at 12:22
  • @KristofPlennings sure using your Schedule event is correct to do such things. with `sam local` you need to use `generate-event` then.. see my updated answer. – disco crazy Jun 04 '18 at 12:23
  • Thank you. I tried it and it returns a sample payload in the trend of: { "source": "aws.events" ... But my actual handleRequest doesn't seem to be executed. (handleRequest should create a dir to test it's doing something, but it's not being created). Thank you so much already. And noticed I should have checked: https://docs.aws.amazon.com/lambda/latest/dg/test-sam-cli.html better – Kristof Plennings Jun 04 '18 at 12:38
  • you need to invoke it `sam local invoke function-name -e event_file.json` – disco crazy Jun 04 '18 at 12:53
13

For Googlers:

  • Check whether you have an Event with Type: Api
  • ALSO check whether you have run sam build (very important)
  • Use the --debug flag so you will know what is going on

As of 2020/7/13, Type: HttpApi does not work with sam local start-api. See issue.

dz902
  • 4,782
  • 38
  • 41
3

This error message also displays if you are trying to test a websocket API locally. Unfortunately, local testing of websockets is not currently supported - see https://github.com/awslabs/aws-sam-cli/issues/896.

Evan Pon
  • 1,496
  • 1
  • 13
  • 22
1

I ran into this error too even when I did have an Api event defined in my SAM template. The problem was that I had a previous template in my .aws-sam/build/ directory which didn't have the Api event defined (from a previous run of sam build). Cleaning out the build directory fixed it.

leeor
  • 17,041
  • 6
  • 34
  • 60
1

I am getting this error, but I have function that is working with the HttpApi, it appears that current version of sam does not support HttpApi.

CLI Version

SAM CLI, version 0.52.0

Example Function

FeedsFunction:
  Type: AWS::Serverless::Function
  Properties:
    CodeUri:
    Description: "Function that handles feeds"
    Events:
      Handler:
        Type: HttpApi
        Properties:
          ApiId: !Ref FeedsApi
          Path: /
          Method: get
    Handler: api
    MemorySize: 1024
    Runtime: go1.x
    Timeout: 5
    Tracing: Active

There is currently an open issue on GitHub for adding support: https://github.com/awslabs/aws-sam-cli/issues/1641

Jason M.
  • 563
  • 1
  • 5
  • 10
0

I got this error when I had a whitespace error in my AWS::Serverless::Function definition, specifically Environment needed to be a child of Properties but was on the same level. Correcting the whitespace made this error disappear. Nodejs 10.15.

cyrf
  • 5,127
  • 6
  • 25
  • 42