3

Now I have my lambda just echoing the event object:

def lambda_handler(event, context):
    pprint(event)

I have been using the event object in the code to get the path like this event['path']

When I run sam local to start up the api gateway locally like this, when I hit the locally running api gateway the lambda dumps an event object like i would expect and I can read the path from it:

sam local start-api

But When I deploy the SAM template to aws and test it in the api gateway console "event" is empty.

Why is event empty on aws and not locally? Do I need to do something to pass the full event? This is my template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
    sam-app

    Sample SAM Template for sam-app

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
    Function:
        Timeout: 3

Resources:

    lambdafunction:
        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:
            CodeUri: hello_world/build/
            Handler: app.lambda_handler
            Runtime: python2.7

            Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
                Variables:
                    PARAM1: VALUE

            Events:
                # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
                testMethod:
                    Type: Api
                    Properties:
                        RestApiId: !Ref ApiGatewayApi
                        Path: /testMethod
                        Method: GET

    ApiGatewayApi:
        Type: AWS::Serverless::Api
        Properties:
            StageName: Prod
            DefinitionBody:
                swagger: "2.0"
                info:
                    version: "2018-09-12T06:21:35Z"
                    title: mytest
                schemes:
                - "https"
                paths:
                    /testMethod:
                        x-amazon-apigateway-any-method:
                            produces:
                            - "application/json"
                            responses:
                                '200':
                                    description: "200 response"
                                    schema:
                                        $ref: "#/definitions/Empty"
                            security:
                            - sigv4: []
                            x-amazon-apigateway-integration:
                                uri:
                                  Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${lambdafunction}/invocations
                                responses:
                                    default:
                                        statusCode: "200"
                                passthroughBehavior: "when_no_match"
                                httpMethod: "POST"
                                contentHandling: "CONVERT_TO_TEXT"
                                type: "aws"
                securityDefinitions:
                    sigv4:
                        type: "apiKey"
                        name: "Authorization"
                        in: "header"
                        x-amazon-apigateway-authtype: "awsSigv4"
                definitions:
                    Empty:
                        type: "object"
                        title: "Empty Schema"
red888
  • 27,709
  • 55
  • 204
  • 392
  • 1
    I would try actually hitting the API endpoint to test it, instead of using the API Gateway "test" method. I believe with the test method you have to define custom test event data for it to send, otherwise it won't send anything. – Mark B Sep 17 '18 at 01:54

0 Answers0