-2

I have just started learning AWS and nodejs . I am trying to understand how this lambda in the serverless app gets the required information while execution .Let's consider the below piece of code

use strict';

var AWS = require("aws-sdk");

var lambda = new AWS.Lambda({
    apiVersion: '2015-03-31',
    endpoint: 'https://lambda.' + process.env.DYNAMODB_REGION + '.amazonaws.com',
    logger: console
});

So the first statement it creates a variable AWS . In any normal application these dependencies will be available in node modules and when we refer to it , we will easily access it . But here in the case of lambda function which has been created as a serverless app how it gets the dependency.

My second question is when we refer to process.env.DYNAMODB_REGION what would be the value for process.env ?

My third question is it possible to create a common logger file, get it imported inside the lambda and use it to log the details ?

Please help me to understand how lambda function get all these details .

ani
  • 446
  • 1
  • 9
  • 31
  • Where is this piece of code used? This is not the code for your lambda, function is it? – tianz Apr 30 '22 at 18:16
  • No . I have just started learning for my assignment referring some examples . I ended up in the below post https://stackoverflow.com/questions/35754766/nodejs-invoke-an-aws-lambda-function-from-within-another-lambda-function and am trying to understand the example provided in the answer – ani Apr 30 '22 at 18:29
  • I couldn't find details relevant to these things . So posted here to get some guidance . – ani Apr 30 '22 at 18:31
  • 1
    The AWS SDK is provided in the Lambda runtime environment by default. Other npm dependencies would need to be included in the Lambda deployment zip file along with your code. – Mark B Apr 30 '22 at 19:02
  • @ani That question was asking about invoking a Lambda function within another Lambda function. Is this actually something you will be doing? – tianz Apr 30 '22 at 19:51

1 Answers1

1
  1. the aws-sdk dependency is provided by the Lambda runtime, so you don't have to download and package it yourself. Any other dependency that's not provided by the runtime, you will have to package them in the zip file which you upload when creating your Lambda function.

  2. DYNAMODB_REGION is not a standard environment variable that's set by the runtime; you'd have to provide its value yourself when you create your lambda. For the list of environment variables that are set by the Lambda runtime, as well as how to set your own environment variables, see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html.

  3. Not exactly sure what you are asking, but for logging, the easiest way is to use AWS CloudWatch. For more information you can see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-logging.html.

The code you posted is accessing a lambda function within another lambda function. It is not exactly how you'd create your lambda function for your serverless app.

tianz
  • 2,155
  • 2
  • 19
  • 28
  • Thank you so much for the reference. This is what I needed . I missed to read about lambda runtime . Regarding 3 question , let's say in our normal application we can have errorHandler.js which has the function to handle errors seperately and we can export it and use it wherever required. Incase of serverless app can we have such type of util functions or Reusable functions . – ani May 01 '22 at 06:20
  • And also cant we invoke a lambda from another lambda incase of serverless app ?? – ani May 01 '22 at 06:21
  • “Incase of serverless app can we have such type of util functions or Reusable functions .” Sure. It's the same way as your normal application. You just have to put it in the package which you upload for your Lambda function. – tianz May 01 '22 at 21:04
  • 1
    @ani "And also cant we invoke a lambda from another lambda incase of serverless app" you can, but depends what you want to achieve, it's not always the best solution. If you don't need the second lamdba to run synchronously, a better solution would be chaining them using a queue, like SNS or SQS. – tianz May 01 '22 at 21:06