Reputation: 21865
I'm new to AWS Serverless Lambda so go easy on me :)
We have an event that triggers every Sunday at 00:01 AM
:
service: serverlesslambda
provider:
name: aws
runtime: nodejs12.x
functions:
changeWeeklyStarterStatus:
handler: handler.changeWeeklyStarterStatus
schedule: cron(0 0 0 ? * SUN *)
Establishes connection to Mongo DB and needs to do some manipulations :
"use strict";
module.exports.changeWeeklyStarterStatus = async event => {
const MongoClient = require("mongodb").MongoClient;
const MONGODB_URI = process.env.MONGODB_URI; // or Atlas connection string
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const MongoClient = require("mongodb").MongoClient;
var documentsCount = "";
MongoClient.connect(MONGODB_URI, (err, client) => {
if (err) return console.log(err);
var db = client.db("mydatabase");
db.collection("mycollection").countDocuments(getCountCallback);
app.listen(3000, () => {
console.log("listening on 3000");
});
});
function getCountCallback(err, data) {
console.log(data);
documentsCount = data;
}
app.use(bodyParser.urlencoded({ extended: true }));
return {
statusCode: 200,
body: JSON.stringify(
{
message: `Counting ${documentsCount} documents`,
input: event
},
null,
2
)
};
};
How can I run the body of the Lambda function and debug it when needed ?
Upvotes: 0
Views: 476
Reputation: 9402
This article will walk you through how to use the Test feature to test your function: https://aws.amazon.com/blogs/compute/improved-testing-on-the-aws-lambda-console/
In addition to running at test, you can instrument your code with logging statements. Assuming your function is running with an IAM role that has the AWSLambdaBasicExecutionRole policy (or equivalent permissions), it will be able to write to CloudWatch logs. You can click on the Monitoring tab of the lambda and find a link to view the full details in CloudWatch and then use the log statements to debug things when you run your test. It is not as ideal as being able to attach a debugger would be, but it is the mechanism AWS provides.
Also check out https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-cloudwatch-metrics-logging.html#lambda-cloudwatch-logs for details on the logs.
Check out https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-logging.html for details on Node logging in Lambda.
Upvotes: 1
Reputation: 887
Go to your AWS Console, click Services -> Lambda. From there find the Lambda function which is run by your event, click on it and you will find a "Test" button in the top right corner.
You will have to provide a Test event, if you're not passing any parameters, you can create a dummy event like the default one. Then when you'll click on Test the Lambda function will be triggered.
Note that it you are modifying the database, the modifications will be live, so avoid doing this in a production environment (unless you're only reading the database)
Upvotes: 1
Reputation: 7770
Treat this as a normal function and test it as you would. basically handler function (in your case changeWeeklyStarterStatus) which takes event as parameter. You could extract it in a separate file and call the function manually to test it.
Not related to your question but you might want to have a look at
https://docs.atlas.mongodb.com/best-practices-connecting-to-aws-lambda/
Upvotes: 2