首页 > 解决方案 > How to warmup a serverless lambda with child routes in nodejs

问题描述

I have created a lambda function in nodejs with child route
Here my structure:


Here index.js

const express = require('express');
const notes = require('./notes/notes.controller');
router.use('/notes', notes);
router.use('/otherRoutes', otherRoutes);
module.exports = router;

app.js

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

/*CORS*/
app.use((req, res, next) => {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader(
        "Access-Control-Allow-Headers",
        "Origin, X-Requested-With, Content-Type, Accept"
    );
    res.setHeader(
        "Access-Control-Allow-Methods",
        "GET, PUT, POST, PATCH, DELETE, OPTIONS"
    );
    next();
});
const helmet = require('helmet');
app.use(helmet());
require('./db');
const routes = require('./routes');
app.use('/', routes);

module.exports = app;

notes.controller.js

const express = require('express');
const notesController = express.Router();
const Note = require('./note');

notesController
    .post('/', async (req, res, next) => {
        const note = await Note.create(req.body);
        res.status(200).send(note)
    });

notesController
    .put('/:id', async (req, res, next) => {
        const note = await Note.findByIdAndUpdate(req.params.id,
            { $set: req.body },
            { $upsert: true, new: true });
        res.status(200).send(note)
    });

notesController
    .get('/', async (req, res, next) => {
        const notes = await Note.find();
        res.status(200).send(notes)
    });

notesController
    .get('/:id', async (req, res, next) => {
        const note = await Note.findById(req.params.id);
        res.status(200).send(note)
    });

notesController
    .delete('/:id', async (req, res, next) => {
        const note = await Note.deleteOne({ _id: req.params.id });
        res.status(200).send(note)
    });

module.exports = notesController;

I want to warmup my lambda function and I followed this tutorial : https://serverless.com/plugins/serverless-plugin-warmup/
But the structure is different because it is multiple lambda function. In my structure, it is one lambda function with multiple child routes.
Do you know how can I warmup my lambda function with child routes please ?

Thank you for your help.

标签: node.jsaws-lambdaserverlessaws-serverless

解决方案


自本周 re:Invent 以来,不再需要为 Lambda 函数设置复杂的升温策略,因为一个名为 Provisioned Concurrency 的新功能已启动,并由无服务器框架提供支持。只需在 serverless.yml 文件中添加一行,在您希望为其设置预置并发的函数下添加您想要多少个暖 Lambda 实例,然后一切就绪:

    provisionedConcurrency: 3

无服务器框架的参考文档:https ://serverless.com/framework/docs/providers/aws/guide/functions/

包含更多详细信息的博客文章:https ://serverless.com/blog/aws-lambda-provisioned-concurrency/


推荐阅读