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

6 Answers 6

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Ah, any way to set the lambda up so it just gets triggered periodically (once/day) without it being started by invoking that path?
@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.
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: docs.aws.amazon.com/lambda/latest/dg/test-sam-cli.html better
you need to invoke it sam local invoke function-name -e event_file.json
14

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.

Comments

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.

1 Comment

any update here, is it supported now :-)? any workaround found?
2

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.

Comments

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.