Skip to content

Latest commit

 

History

History
131 lines (87 loc) · 4.87 KB

README.06.aws.md

File metadata and controls

131 lines (87 loc) · 4.87 KB

AWS Lambda

  • AWS Lambda lets you run code without provisioning or managing servers

  • We pay only for the compute time we consume.

AWS documentation on GitHub

Supported Node.js runtimes:

Create a lambda function

Using the AWS Management Console:

serverless create --template aws-nodejs --path my-service

The lambda function handler

At the time you create a Lambda function you specify a handler, a function in your code, that AWS Lambda can invoke when the service executes your code.

exports.myHandler = function(event, context, optionalCallback) {
  // event – To pass in event data to the handler.
  // context – To provide Our handler the runtime information of the Lambda function that is executing.
  // callback – We can use the optional callback to return information to the caller, otherwise return value is null.
}

for example:

exports.myHandler = function(event, context, callback) {
  // The console.log() statements log some of the incoming event data to CloudWatch Logs.
  console.log("value1 = " + event.key1);
  console.log("value2 = " + event.key2);
  callback(null, "some success message"); // or callback("some error type"); 
}

The following are example callbacks:

callback();     // Indicates success but no information returned to the caller.
callback(null); // Indicates success but no information returned to the caller.
callback(null, "success");  // Indicates success with information returned to the caller.
callback(error);    //  Indicates error with error information returned to the caller.

The Context Object

While a Lambda function is executing, it can interact with AWS Lambda to get useful runtime information such as:

  • How much time is remaining before AWS Lambda terminates your Lambda function (timeout is one of the Lambda function configuration properties).

  • The CloudWatch log group and log stream associated with the Lambda function that is executing.

  • The AWS request ID returned to the client that invoked the Lambda function. You can use the request ID for any follow up inquiry with AWS support.

  • If the Lambda function is invoked through AWS Mobile SDK, you can learn more about the mobile application calling the Lambda function.

console.log('Loading function');

exports.handler = function(event, context, callback) {
    //console.log('Received event:', JSON.stringify(event, null, 2));
    console.log('value1 =', event.key1);
    console.log('value2 =', event.key2);
    console.log('value3 =', event.key3);
    console.log('remaining time =', context.getRemainingTimeInMillis());
    console.log('functionName =', context.functionName);
    console.log('AWSrequestID =', context.awsRequestId);
    console.log('logGroupName =', context.logGroupName);
    console.log('logStreamName =', context.logStreamName);
    console.log('clientContext =', context.clientContext);
    if (typeof context.identity !== 'undefined') {
        console.log('Cognito
        identity ID =', context.identity.cognitoIdentityId);
    }    
    callback(null, event.key1); // Echo back the first key value
    // or
    // callback("some error type"); 
};

The handler code in this example logs some of the runtime information of the Lambda function to CloudWatch. If We invoke the function using the Lambda console, the console displays the logs in the Log output section. We can create a Lambda function using this code and test it using the console.

https://docs.aws.amazon.com/lambda/latest/dg/get-started-create-function.html

AWS Lambda logs on CloudWatch

AWS SAM Local, a CLI Tool to Test AWS Lambda Functions Locally

AWS Serverless Application Model (AWS SAM)

Prescribes rules for expressing Serverless applications on AWS.

How to create serverless applications using AWS SAM:

Detailed instructions on how to write AWS SAM templates and deploy them to AWS CloudFormation: