I am new to AWS and trying to deploy a GraphQL API written in NodeJS as an AWS Lambda using Serverless. I have followed several tutorials that are close to what I am doing but cannot get the handler to work. The closest tutorial to what I am doing was using JavaScript, where I am using TypeScript. I followed closely but using TypeScript. I am getting errors that it cannot find the handler module (Error: Cannot find module 'lambda').
My handler file is named lambda.ts looks like this `
import {app} from './app';
import {serverless} from 'serverless-http';
export const handler = serverless(app);
`
My serverless.yml looks like this `
service: graphql-api
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs12.x
region: us-east-1
functions:
serverlessApi:
handler: lambda.handler
app.ts
import express from 'express';
import cors from 'cors';
import { graphqlHTTP } from 'express-graphql';
import { schema, resolvers } from './api-schema/';
import { loginMiddleware } from './login-middleware';
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(loginMiddleware);
app.use(
'/graphql',
graphqlHTTP({
schema: schema,
graphiql: true,
rootValue: resolvers,
})
);
export default app;
` The lambda.js, app.ts, and serverless.yml files are all in the same root directory.
I have tried to convert the code to JavaScript and get errors that it cannot find the 'app' module. (Cannot find module './app')
lambda.js and looks like this `
const app = require('./app');
const serverless = require('serverless-http');
module.exports.handler = serverless(app);
`
I have also tried to export 'app' as default or named getting the same result. I am out of ideas. Any help is much appreciated.