Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Dev Server for AWS Lambdas

Is there a dev server that runs AWS Lambdas locally? My requirements would be

  • nodejs server, no ruby or go or anything need to install other than node and npm packages
  • Creates a server that I can query via wget / curl or an API testing tool to send various events to
  • I should be able to specify a js file that the server uses as lambda and the server should restart / update when I change that file
like image 347
Lukas Avatar asked Nov 25 '25 09:11

Lukas


1 Answers

Here's a solution that does not require serverless or claudiajs.

I usually just write my own little express script for this purpose. I always just use Lambda Proxy integration so it's simpler.

Something like this...

const bodyParser = require('body-parser')
const express = require('express')

// Two different Lambda handlers
const { api } = require('../src/api')
const { login } = ('../src/login')

const app = express()

app.use(bodyParser.json())

// route and their handlers
app.post('/login', lambdaProxyWrapper(login))
app.all('/*', lambdaProxyWrapper(api))


app.listen(8200, () => console.info('Server running on port 8200...'))


function lambdaProxyWrapper(handler) {
  return (req, res) => {
    // Here we convert the request into a Lambda event
    const event = {
      httpMethod: req.method,
      queryStringParameters: req.query,
      pathParameters: {
        proxy: req.params[0],
      },
      body: JSON.stringify(req.body),
    }

    return handler(event, null, (err, response) => {
      res.status(response.statusCode)
      res.set(response.headers)

      return res.json(JSON.parse(response.body))
    })
  }
}

Then, run it with nodemon so it watches the files and reloads as necessary.

nodemon --watch '{src,scripts}/**/*.js' scripts/server.js
like image 160
Noel Llevares Avatar answered Nov 27 '25 21:11

Noel Llevares



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!