These worker functions are RabbitMQ consumers that process incoming requests and reply with responses.
I’m looking for guidance on how to keep these worker functions always active after deploying the Lambda function. What configurations or practices can help achieve this?
// app.js
import e from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import 'dotenv/config.js';
import mongoose from 'mongoose';
import SwaggerRouter from './swagger.js';
import logger from './logger.js';
import StartUpRouter from './routes/user_founder.js'
import VCRouter from './routes/user_vc.js';
import SuperAdminRouter from './routes/superadmin.js';
import { handleVCListRequest, startTokenValidationWorker, startUserValidationWorker, startVCDataWorker } from './workers/messageQueueWorker.js';
import { startWorker } from './services/rabbitMQService.js';
import { processMessage } from './workers/emailWorker.js';
const app = e();
app.use(cors());
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(SwaggerRouter);
// ...other routes and middleware...
mongoose.connect(process.env.MONGO_URL, { useUnifiedTopology: true, useNewUrlParser: true })
.then(() => {
app.listen(process.env.PORT || 4001);
logger.info({listening: `Server is Processing on ${process.env.PORT}.`});
logger.info({success: "Connected to AuthenticationService Database."});
console.log({listening: `Server is Processing on ${process.env.PORT}.`});
console.log({success: "Connected to Database."});
startWorker(processMessage).catch((error) => {
console.error('Error while sending email:', error.message);
});
startTokenValidationWorker().catch((error) => {
logger.error('Error starting token validation worker:', error.message);
console.error('Error starting token validation worker:', error.message);
});
// ...other worker functions...
handleVCListRequest().catch((error) => {
logger.error('Error starting vc list worker:', error.message)
console.error('Error starting vc list worker:', error.message);
});
})
.catch((err) => {
logger.error({error: err.message});
console.error({error: err});
});
export default app;
// lambda.js
import ServerlessHttp from "serverless-http";
import app from "./app.js";
export const handler = ServerlessHttp(app);
2
Answers
AWS Lambda is not the right service here, as a serverless compute service.
While you can keep a Lambda function warm, the nature and purpose of serverless architectures is to not keep services running 24/7.
RabbitMQ consumers, that should always be listening for messages, should be running on a service like ECS, EC2 etc. that use servers & can be available 24/7.
A single AWS Lambda invocation (instance) cannot run more than 15 minutes. The way you have currently designed your system, simply will not work in an AWS Lambda environment.
If you were running RabbitMQ through Amazon MQ, you could have Amazon automatically invoke your Lambda function when there are messages in your Amazon MQ RabbitMQ instance. In that setup, your Lambda function would not connect to RabbitMQ, instead it would be invoked with the messages to process inside the
event
object.If you are not using Amazon MQ to run your RabbitMQ server, then AWS Lambda is just not going to work, and instead you need to use a service like EC2 or ECS that runs your event processing service all the time.