AWS Lambda function is returning response from a different Lambda function

I’m new to AWS and attempting to build an API for a basic course scheduling app. I am currently able to get the API running locally and able to invoke two functions. Function 1 is executing properly, but function 2 seems to be executing the code from function 1. Here is how I have my SAM app structured:

- sam-app
| - events
| - tests
| - src
|   - api
|     - course
|       - AddCourse Lambda
|   app.js (Index Lambda, the default hello world sample function, mostly just using to check that API is up)

The Index Lambda at app.js does a GET / and returns status code 200 and body with message "Hello World!" so long as the API is reachable.

The AddCourse Lambda is supposed to do the following via POST /courses:

try {
        console.log("Adding a new item...");
        await docClient.put(params).promise();
        response = {
            'statusCode': 200,
            'headers': {
                'Content-Type': "application/json"
            },
            'body': JSON.stringify({
                message: 'Successfully created item!'
            })
        }
    } catch (err) {
        console.error(err);
        response = {
            'statusCode': 400,
            'headers': {
                'Content-Type': "application/json"
            },
            'body': JSON.stringify(err)
        }
    }

Instead, it is returning status code 200 and body with message "Hello World!".

My template.yml seems to have the correct routes specified too:

Resources:
  Index:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src
      Handler: app.handler
      Runtime: nodejs14.x
      Policies: AmazonDynamoDBReadOnlyAccess
      PackageType: Image
      Events:
        GetEvent:
          Type: Api
          Properties:
            Path: /
            Method: get
    Metadata:
      DockerTag: nodejs14.x-v1
      DockerContext: ./src
      Dockerfile: Dockerfile
  AddCourse:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/api/course/Course-POST-CreateNewCourse
      Handler: index.lambdaHandler
      Runtime: nodejs14.x
      Policies: AmazonDynamoDBFullAccess
      PackageType: Image
      Events:
        GetEvent:
          Type: Api
          Properties:
            Path: /courses
            Method: post
    Metadata:
      DockerTag: nodejs14.x-v1
      DockerContext: ./src
      Dockerfile: Dockerfile

What could possibly be going on here? Is there something inherently wrong with how I structured my app?

Source: Docker Questions

LEAVE A COMMENT