Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SAM - Template does not have any APIs connected to Lambda functions

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

like image 483
Kristof Plennings Avatar asked Jun 04 '18 12:06

Kristof Plennings


People also ask

What is the name of Sam template property that defines the point in a lambda functions code where execution begins?

The template. yaml file is a SAM template describing the infrastructure for the application, and the app. js file contains the application code.

Which resources can be specified in the AWS serverless application model AWS Sam template?

Note that a serverless application is more than just a Lambda function—it can include additional resources such as APIs, databases, and event source mappings. You can use AWS SAM to define your serverless applications.

Which section of the AWS serverless application model template is required and identifies an AWS CloudFormation template file as an AWS Sam template file?

The declaration Transform: AWS::Serverless-2016-10-31 is required for AWS SAM template files. This declaration identifies an AWS CloudFormation template file as an AWS SAM template file. For more information about transforms, see Transform in the AWS CloudFormation User Guide.


2 Answers

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.

like image 32
dz902 Avatar answered Sep 16 '22 15:09

dz902


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)

like image 60
H6. Avatar answered Sep 19 '22 15:09

H6.