0

For study purposes, I was trying to deploy a nodejs application to lambda, and I found most people did so using the serverless framework.

If we had to do without any such frameworks, or third party tools, how would we do it? I linked my project from s3 after creating the lambda function, and wrote the handler as such:

import cors from 'cors';
import helmet from 'helmet';
import express from 'express';
import bodyParser from 'body-parser';

import routes from './src/routes/routes';
// @ts-ignore
const port = 80;
const app = express();

app.use(helmet());
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use('/api', routes);


exports.handler = async (_event:any) => {
    app.listen(port, () => console.log(`Server started at port: ${port}`));

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };

    return response;
};

export default app;  

and I definitely got a permission denied error. I was trying to make the server run with it exposing the port of the public url endpoint given for the function. Is it possible to do that?

mathmaniage
  • 299
  • 1
  • 14
  • Follow the AWS documentation for node.js deployment to AWS Lambda [here](https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html). – jarmod May 23 '22 at 23:15
  • @jarmod , I went through the documentation, but, not much about integrating things with something like express, this itself listens on a port – mathmaniage May 26 '22 at 17:48
  • Lambda functions [cannot accept](https://stackoverflow.com/questions/49019617/can-aws-lambdas-receive-inbound-tcp-connections) inbound connections. They're triggered on events, such as an API request via API Gateway, or an object being uploaded to S3. – jarmod May 26 '22 at 18:13
  • Express + Lambda won't work unfortunately, API Gateway + Lambda is the fully managed go-to solution. If you really really want to use express, elastic beanstalk might be what your after: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html – Scaccoman Jun 08 '22 at 13:44

0 Answers0