Going Serverless with AWS Lambda Functions — 1

Ankit Prakash Gupta
Geek Culture
Published in
6 min readJan 6, 2019

Few years back, traditional physical servers were replaced by Cloud Servers and now, serverless is the new cool. In serverless execution, the servers are there but we don’t need to manage those servers anymore. All that is taken care by Service Provider. Along with this, no cost is levied as there is no instance of server running until unless the trigger is fired, which ends up as soon as the execution ends. AWS Lambda Functions, facilitating the serverless execution by providing compute resources on need without provisioning or managing servers, can be triggered in different ways: an HTTP request, a new document upload to S3, a scheduled Job, an AWS Kinesis data stream, or a notification from AWS Simple Notification Service (SNS).

AWS Lambda Functions might appear as the ubiquitous solution, but there are a few limitations in working with them. A single lambda function comes with a limited storage of 512 MBs, limited size of 50 MBs in the zipped form for deployment package and is short-lived maximum for 15 minutes. All the above mentioned demerits can be managed in one way or another. Along with this, there is one more significant issue with AWS Lambda Functions, cold start. It takes some time for the Lambda function to handle a first request, because Lambda has to start a new instance of the function. The cold start can be a real problem for your function, if at any time you expect a fast response and there are no active instances of your function.

Here, I will brief you through the simple and complex Lambda Function Creation along with creating and configuring AWS Lambda Layers using AWS CLI to be used with AWS Lambda Functions, which lets us surpass the limitation of having a deployment package of 50 MBs maximum, by allowing us to have a maximum of 5 layers having different libraries and having a total unzipped size of libraries upto 250 MBs.

Here, We will go through the steps of creating a simple Lambda Function triggered using an API :

  1. First, Login into AWS Account.
AWS Login Screen

2. Go to the Lambda Dashboard.

AWS Service Dashboard

3. On the Lambda Dashboard, click on Create Function option.

AWS Lambda Dashboard

4. On the Create Function Screen, type in the name of the Lambda Function, choose the Runtime Environment and choose Create a Custom Role in the Role selection Area which will direct you to the Role Creation Screen.

Create Lambda Function Screen

Lambda Roles : Each Lambda function has an IAM role (execution role) associated with it. You specify the IAM role when you create your Lambda function. Permissions you grant to this role determine what AWS Lambda can do when it assumes the role.

Role Creation Screen

5. For now, we will select the lambda_basic_execution Role which will grant permissions only for the Amazon CloudWatch Logs actions to write logs. We will use this policy as our Lambda function is a basic function and will not access any other AWS resources except writing logs.

Post this, click on Allow which will take you back on the Create Function Screen and an existing lambda_basic_execution role will be selected. After this, click on Create Function

6. We will be at Lambda Configuration Screen, using which we can change all the settings of the Lambda Function. Mainly, Add/Remove Trigger, change the lambda Function, change its role, memory allocation, set environment variables, set the timeout period, select Virtual Private Cloud (VPC) as well as set error handling mechanism.

Lambda Configuration Screen

We should always make changes to the required settings before activating the Lambda Function. For now, the main settings are :

  1. Trigger
  2. Lambda Function’s Code and lambda handler function.
  3. Timeout
  4. Allocated Memory
  5. Role and Permissions

7. Now, we will set the Trigger (Trigger is actually the event when the lambda function should be called and executed) for our function. For now, we will use an API hit on the AWS API Gateway as the triggering event and will choose API Gateway on the Add Triggers Panel on the Configuration Tab.

API Trigger Configuration

We choose to create a new API and in the security tab we choose to create an open API with API Key.

Type in the name of the API and click on Add button.

After this, we will click on the first_lambda button over Layers button and edit the code and use the following mentioned code for our first Lambda Function which will use passed name in the returned expression.

Inline Code Editor

We can use the inline editor to write the code or can also upload a zipped file having the code inside it. In both cases, we will have to set the Handler as the <file_name>.<function_name>. In the above case we have set it to be lambda_function.lambda_handler as the file name is lambda_function and function’s name is lambda_handler.

Also, note that event and context two variables are passed to lambda_handler function which provides us information about the triggering event.

Post this, click on Save button to save the changes in the Lambda Function.

8. We have successfully created our First Lambda Function triggered using API. Post which, on clicking on the highlighted API Gateway Button, we will see the API endpoint and API Key to be used.

Successful Creation of Lambda

We could try the lambda Function using Postman and hitting a request on the API endpoint along with API Key in the header with the header key as ‘x-api-key’.

API Hit using Postman
AWS Lambda Monitoring Screen

9. We can also check the invocations and errors occured in the lambda executions using the Monitoring Tab of the Lambda Dashboard.
We can also check the logs of Lambda Executions using Cloud Watch, since we provided permission to create CloudWatch Log Streams.

Not all lambda functions will have such small code and might need multiple modules to carry out the functionality feasibly. One such use case can be downloading images using AWS Lambda Functions. If, we are developing using python, we will need PIL and requests module along with their dependencies.

In the next article, we will cover creating custom layers, downloading libraries or modules for AWS Lambda instances using Docker and will create a lambda triggered using AWS S3’s Object Creation which will use a custom created layer.

I hope you find this article informative and easy to learn, if you have any queries feel free to reach me at info.ankitp@gmail.com

EDIT : Going Serverless with AWS Lambda Functions — 2 has been published.

--

--