5

Problem

I'm trying to run simple integration between Lambda and API Gateway with AWS SAM. I want to customize the input for lambda - apply some requestTemplates. But those seem to be ignored.

Steps to reproduce

Running with:

sam local start-api
curl -v -XPOST -H "Content-type: application/json" -d '{"jarmil":"prdel"}' http://localhost:3000/

The output is:

START RequestId: 43594e8a-c3af-4f47-9d85-6c605131f02a Version: $LATEST
Processing event {'httpMethod': 'POST', 'body': '{"jarmil":"prdel"}', 'resource': '/', 'requestContext': {'resourcePath': '/', 'httpMethod': 'POST', 'stage': 'prod', 'identity': {'sourceIp': '127.0.0.1:53210'}}, 'queryStringParameters': {}, 'headers': {'Accept': '*/*', 'Content-Length': '18', 'Content-Type': 'application/json', 'User-Agent': 'curl/7.43.0'}, 'pathParameters': None, 'stageVariables': None, 'path': '/'}
END RequestId: 43594e8a-c3af-4f47-9d85-6c605131f02a

It seems to work as if the default API was created. I get the event printed. But it seems my API definition is completely ignored. I always get back the original event without requestTemplates being applied. Symptoms:

  • Whatever I put in my swagger definition has no effect. Changing produces, putting there any malformed swagger
  • When I use invalid RestApiId (non-existing reference) - no change
  • When I use non-existing type instead of AWS::Serverless::Api - no effect

Environment

  • Sam version: 0.2.4
  • OS: X

Code

My template.yml:

AWSTemplateFormatVersion : '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: POC
Resources:
  ScheduleFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      Runtime: python3.6
      Handler: lambda_function2.lambda_handler
      Events:
        ApiRoot:
          Type: Api
          Properties:
            RestApiId: !Ref ScheduleApi
            Path: /
            Method: ANY
  ScheduleApi:
    Type: 'AWS::Serverless::Api'
    Properties:
      StageName: dev
      DefinitionUri: swagger.yml

My swagger.yml:

swagger: 2.0
info:
  title: "Scheduling API"
consumes:
  - application/json
produces:
  - application/json
paths:
  /:
    post:
      x-amazon-apigateway-integration:
        httpMethod: post
        type: aws
        requestTemplates:
          application/json: "#input x"

And the lambda_function2.py:

def lambda_handler(event, context):
    print("Processing event", event)
    return event

Further resources

This example from sam repo looks pretty much the same as what I want to achieve. Doesn't work either.

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118

0 Answers0