AWS Lambda Tutorial for Beginners

Introduction to AWS Lambda

AWS Lambda is a service that computes the code and works without managing any server. It takes cost if the code runs, but if the code is not running; they take no cost for it. AWS services contain the event, and when an event is a response, the code is executed like adding/removing files in S3 Bucket and updated the services like Amazon API gateways, Dynamo DB and so on. Any languages can be used in AWS Lambda code like node js, java, python, etc. AWS Lambda Working: - There are five stages which describe the working of AWS lambda- AWS Lambda Working
  1. AWS Lambda supports some languages like node js, java, and c # and so on. So upload the code of AWS lambda in any of the languages.
  1. AWS lambda triggers few AWS services.
  2. AWS lambda contains the code and event details on which the trigger has occurred just like the event for a mobile app, the event for Amazon S3, and the event for other services.
  3. Under some scenarios, the AWS lambda code executed when triggered the AWS services. Scenarios are: -
  • Files are uploaded by the user in the S3 bucket.
  • Get/Post URL hit.
  • Add/update/delete the data in dynamo DB tables.
  • Push the notification.
  • Collection of the data stream.
  • Website hosting.
  •  Sending the email.
  •  Mobile app.
5. If AWS lambda code is executed then AWS charges, and otherwise not.

Merits of AWS Lambda: -

  1. The Execution cost is reduced.
  2. The Code is flexible.
  3. Support Multi-languages.
  4. Code authoring and deploying is easy.

Demerits of AWS Lambda: -

  1. Multi-User running in the same host, so it causes the problem of security, performance, and robustness.
  2. Decide the memory and timeout after analyzing the code. If code takes more time for analyzing the code, it will automatically terminate, and the code will not be fully executed.
  3. Small projects are not suitable for AWS lambda.
The Lifecycle of Building the AWS Lambda: - There are 4 stages of lifecycle to build the AWS Lambda-
  1. Authoring- AWS Lambda code written in different languages such as Node js, Java, C#, Python and AWS lambda using different IDE for writing the code.
The table which shows the different tools and IDE that are used to write the code:

Languages

IDE for Lambda Code

Node js Visual Studio IDE AWS lambda console
Java Eclipse
C# .Net core Visual Studio
Python AWS lambda Console
 
  1. Deploying- There are two ways to deploy the code after when you decide which language you use to write the code-
  • Zip all the files.
  • The code is written directly in the AWS console.
  1. Testing- We can use sample event data to test the lambda function. There are the following methods which are used to test the lambda function:
  • Lambda function is tested using console.
  • Lambda function is tested using the AWS command line interface.
  • Lambda function tests locally in the AWS sam command line interface.
  1. Monitoring- To monitor the lambda function, we use Amazon Cloud Watch. The log messages are added in that language which we choose and those messages also seen in Amazon Cloud Watch.
Models for Writing code in Lambda Function: -
  1. Handler: - Lambda Function has started execution by using the calling function known as Handler. When we create the lambda function, we can identify the handler. There are three parameters passed to the handler function-
  • Event
  • Context
  • Callback function.
Example-
export.lambdahandler (event, context, callback); //syntax of Handler.
  1. Logging: - The Amazon Cloud Watch also displayed the logs which are added inside the lambda function. Every language has different log syntax according to languages. Example-log is the syntax of node js.
  2. Error Handling: - There is a function in AWS lambda known as the callback function.
This function is used to notify the lambda function that shows an error or success in the code. Example-
export.lambdahandler (event, context, callback) => {
var error= new error (“Error in the code”);
callback (error); //this notify the lambda function that an error in the code.
};

Trigger Event in AWS Lambda

In AWS Lambda, the event can trigger as follows:
  • Objects modification in S3 buckets.
  • Data is deleted, updated and inserted in Dynamo DB table.
  • Sent notification from Amazon SNS.
  • AWS Kinesis data stream has log entries.
  • CloudTrail has a history of the log.
  • Synchronization in events of client data in Amazon Cognito.
  • API Gateway is used to call get/post.

Uses of AWS Lambda

  1. Create the thumbnails of the images into S3.
  2. Easily update of data using dynamo DB.
  3. Send the notification and SMS using SNS.
  4. Cron job using Scheduled Events.
  5. Monitor the log on the account using CloudTrail.
  6. Capture real tracking data coming from website clicks, and social media feeds or log using kinesis.

AWS Lambda Function Configuration

When you create the lambda function, you have some configuration information with it. Settings to configure function:
  1. Open Lambda Console.
  2. Select a function.
  3. Configure the available options and then choose to save.
Functions Settings:
  1. Code- You can edit your function in a fix editor. If any language doesn't support that editor, then upload the deployment package.
  2. Runtime- Function is executed by using lambda runtime.
  3. Handler- Every language has its handler format. The function is started execution in runtime using handler.
  4. Environmental Variables- Lambda sets the key-value pair in the execution environment. You can extend the function configuration using the environmental variables outside the code.
  5. Tags- Tags are applied in all functions including all versions and aliasing. Use tags to filtering in the lambda console and to organize the functions into groups for report the cost.
  6. Memory-At the time the amount of memory is available to the function. You can choose the amount of memory with 64 MB increments between 128 MB- 3,008 MB.
  7. Timeout- Before stopping the function, the amount of time that lambda allows the function to run.
The function is configured with lambda API:-
  1. Create Alias
  2. Tag Resources.
  3. Update Function Configuration.
  4. Update Function Code.
  5. Add Permission.
  6. Put Function Configuration.
  7. Publish Version.
AWS Services:
  1. Alexa
  2. Amazon SNS.
  3. Amazon API Gateways.
  4. Amazon Cloud Watch logs.
  5. AWS CloudTrail.
  6. Kinesis
  7. Dynamo DB.
  8. Amazon S3.
  9. Amazon SES.
  10. Amazon SQS.

Deleting the lambda Function

We will remove the AWS lambda function from the AWS console. There are two ways to delete the lambda function.
  1. Using of AWS console.
  2. Using of AWS Command line interface (CLI).
Using of AWS Console- We can delete the lambda function using AWS console. Follow these steps: Step1: Login AWS Console and go to AWS service that you find the list of functions created. Step2: Select the radio button of that function which you want to delete. Step3: Then go to Action dropdown button and then select the delete option to delete the lambda delete. Step4: After selecting the delete option, read the message carefully and then click the delete button and then your lambda function is deleted permanently. Using of AWS CLI- To delete the lambda function using AWS CLI, follow these commands for deleting. Commands are-
delete-function
--function-name <value>
[--qualifier <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]
After this, you can check that the function is not available in the AWS lambda list.