Introduction to AWS Lambda & Serverless Applications

Article Mohaimen Khalid

Introduction to AWS Lambda

AWS Lambda is a service that performs serverless computing, which involves computing without any server. AWS Lambda offers a completely different example for you to design/run your apps. It’s a solution to reduce this wastage of resources.

S3 bucket, updating Amazon dynamo dB tables, HTTP request from Amazon API gateway, etc. the services of AWS Lambda. If you want to work with AWS Lambda, Just store your code in AWS Lambda service.

Worry about maintenance, monitoring, security AWS Lambda service for you because AWS Lambda service takes care about your worry matter - maintenance of server, code monitoring, logs, and security.

NodeJS, Python, Java, Ruby, C# and Go support the AWS Lambda service.

What is AWS Lambda?

From official documentation AWS Lambda is -

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. AWS Lambda executes your code only when needed and scales automatically, from a

few requests per day to thousands per second. All you need to do is supply your code in one of the languages that AWS Lambda supports.

What is serverless?

Serverless computing is a method of providing backend services on an as-used basis. A serverless provider allows users to write and deploy code without the hassle of worrying about the underlying infrastructure.

Confusion about the term “SERVERLESS”

- The term ‘serverless’ is somewhat misleading, as there are still servers providing these backend services

- But all of the server space and infrastructure concerns are handled by the vendor.

- Serverless means that the developers can do their work without having to worry about servers at all.

1605617775_1


How AWS Lambda Works?

AWS Lambda follows some steps.

Step 1 − Uploads your code in Aws lambda with supported languages.

Step 2 − These are few AWS services on which AWS lambda can be triggered.

Step 3 − Aws Lambda uploads your code and triggers some event services Amazon S3, Dynamo dB, mobile app, Amazon SNS, Amazon Kinesis, etc.

Step 4 − Executes AWS Lambda code when triggered by AWS services like - sending email, shore data in S3 bucket, hosting websites, push notification hit on http request, etc.


Scenario of serverless service

1605617776_2

1: Static Website Hosting

Create a bucket in Amazon S3 and configure it for static website hosting. The static HTML, JS, and CSS will be served directly to user browsers from Amazon S3.

1605617776_3

2: User Management

Allow visitors to register as a new user, by providing and validating their email address. Amazon Cognito will be used to manage the User.

1605617776_4

3: Serverless Service Backend

Create a service backend using AWS Lambda and Amazon Dynamo DB to handle requests from

your front-end static website content.

1605617776_5

Advantages of using AWS Lambda

  1. Minimized cost
  2. Faster Development
  3. Easier Management
  4. Consolidate Functionality
  5. Multi-Language Support
  6. Billing based on Usage
  7. AWS Lambda is free getting to AWS free tier

Disadvantages of using AWS Lambda

  1. No Control over Environment
  2. Not good for small size project
  3. More Complex Call Patterns
  4. AWS Lambda does not allow you to install additional software if you need to add.
  5. After executing your software and if your software needs more time to execute & needs more memory, it shows timeout and the code will not be fully executed.

AWS Triggering

  1. Push notifications
  2. GET/POST calls to API Gateway
  3. S3 object etc.

AWS Lambda function code can be written in the following languages –

  1. NodeJS
  2. Java
  3. Python
  4. C#
  5. Go

AWS Lambda – Function in Python

In this chapter, we will create a simple AWS Lambda function with Python and understand how it works. Before start to writing Lambda code we have to create a new function, so follow the steps below & show the attached screenshot -

Step 1: You have to login to the AWS console and then go to AWS Lambda services. Then show this screen and click to create function button -

1605617776_6

Step 2: After clicking the create function button you will show this screen. Then give the name of the function. We write our function name HelloWorld. Then select runtime Python.

1605617776_7

This is our first code in AWS Lambda. This code returns the message Hello from Lambda using Python and looks as shown here –

1605617777_8

Step 3: Now, save the changes and test the code to see the output. You should see the following output and logs when you test it in the AWS console.

1605617777_9


Handler Details for Python

Note that the handler has to be the name of the file followed by the name of the function. In the above case, our file name is helloWorld.py and the name of the function is my_handler; so the handler will be HelloWorld.my_handler.Now, let us understand the details of the Lambda handler function using the following sample code –

def my_handler(event, context):

   return "aws lambda in python using zip file"

In the above code, the function name my_handler is having 2 prams - event and context.


Context Object in Python

Context object gives details like the name of Lambda function, time in milliseconds, request id, cloud watch name, timeout etc.The methods and attributes available on context object shown in below table - 

Method Name: get_remaining_time_in_millis()

Details:  This method gives the remaining time millisecond until the lambda function ends the function.

Attribute & Description

Attribute Name: function_name
Details:  It returns AWS Lambda function name
Attribute Name: invoked_function_arn
Details:  This will return ARN details.
Attribute Name: memory_limit_in_mb
Details:  This shows the memory limit added while creating lambda function
Attribute Name: aws_request_id
Details:  This gives the aws request id.
Attribute Name: og_group_name
Details:  This will give the name of the cloudwatch group name
Attribute Name: log_stream_name
Details: This will give the name of the cloudwatch log stream name where the logs are written.
Attribute Name: client_context
Details: This will detail the client application when used with aws mobile sdk. The details given are as follows −


Disadvantages of using AWS Lambda

  1. client_context.client.installation_id
  2. client_context.client.app_title
  3. client_context.client.app_version_name
  4. client_context.client.app_version_code
  5. client_context.client.app_package_name
  6. client_context.env - it has dict of environment details from the AWS Mobile SDK


Error Handling in Python for Lambda function

In this section, we show how to handle errors in Python. Let's start - 

def error_handler(event, context):

   raise Exception('Error Occurred!')

1605617777_100


Conclusion

Serverless application development is a very trendy topic. However, for more traditional web applications, more often than not, you simply need something that can respond in a “reasonable” amount of time. Amazon Web Services (AWS) is Amazon’s cloud web hosting platform that offers flexible, reliable, scalable, easy-to-use and cost-effective solutions. NodeJS, Python, Java, Ruby, C# and Go support the AWS Lambda service.