Serverless Computing, Hands on with AWS-Lambda

Syed Saad Ahmed
5 min readApr 24, 2021

--

Introduction

In last few decades, we are continuously looking at the innovation happening in the area of Cloud Computing. After the launch of EC2 by AWS in 2006 more or less, Infrastructure as a code concept starts shining and it was the best thing happened at that time using which one can easily create, provision and orchestrate VMs using simple clicks via browser or via some API calls or using command line. The issue was that we still have to care about our infrastructure at the back of our head. Moving forward we got the concept of Serverless Computing. AWS introduces Lambda and it allows you to take your code and run it without any provisioning service, you don’t have to worry about the low level details regarding the computational power and such things, It also provides massive parallelization for your service to execute, So a developer just have to write the function and not to care about the handling of infrastructure related headache.

Introduction to AWS-Lambda

Introduced in the end of 2014 within the stack of AWS computing platform, Lambda is a serverless computing platform which can easily execute your function in response of any certain event and along with this, it also manages the computational resources required for the execution of that piece of code. It allows you to run your code without the provisioning or management of any machine or server. You pay only for the compute time you consume.

https://aws.amazon.com/lambda/
Basic Diagram for understanding AWS Lambda, Credits: AWS

Writing Your First Lambda Function with Python

Here let’s write a simple Python function with AWS Lambda in order to understand its working. Well I am a student so I am using AWS student account in order to practice it. Scrolling to Lambda and click on Create function. We’ll be creating a Lambda from scratch, so select the Author from scratch option.

Basic Lambda Function Landing Zone

Setting up an application from the baseline using the configuration shown in the picture. selecting Python as runtime, name of the application and other stuff as shown.

Lambda Function Configuration Page

After setting up the base line configuration, now move to setting up the test event. We will be Invoking our function via test event. First of all choosing a template that matches the service that triggers your function, or enter your event document in JSON. It is shown here as shown below.

Test Event Configuration

At the next step moving to the addition of code in the AWS Portal. Here we will be adding the code for four functions (Sum, Difference, Multiply, Divide). After adding the code, we will deploy it and now we are gonna test it and check the repsonse.

import jsonprint('Loading function')def lambda_handler(event, context):
key1=event['key1']
key2=event['key2']
sum=int(key1)+int(key2)
difference=int(key1)-int(key2)
multiply=int(key1)*int(key2)
division=int(key1)/int(key2)
return sum,difference,multiply,division

Moving forward to the response, here is the response for our simple python function.

Response structure for the Lambda function

Lambda Functions Automation with Serverless Application Model (SAM) & Github Actions

Let’s have a look at using the GitHub Actions to automate serverless application deployment on AWS. we can use github actions for that, Moreover there are hundreds other options present also. One of them is using the AWS own CI/CD system.

It’s quite simple to start with, We can start with creating a basic SAM application or we can also use our pre-made configuration and then integrate it with github action in order to have an end to end automated structure.

#Step 1- Build your application
cd sam-app
sam build

#Step 2- Deploy your application
sam deploy --guided
sam deploy --guided output

To build and test the application locally one can use the command;

sam build — use-container

Run functions locally and invoke them with the sam local invoke command.

sam local invoke HelloWorldFunction — event events/event.json
sam local start-api
curl http://localhost:3000/

How it will work

  • As soon as a push to GitHub is detected, Action workflow will be triggered.
  • The pipeline can be also triggered manually or recurrently.
  • It will execute all the steps as described in the Github actions workflow yml file as shown in the figure below.

Now we will be having a look at the configuration of Github workflow action,and having a look at it. Before running it via Github action one must put AWS secret credentials inside the github secrets. Here is the github action workflow.

Github Action Workflow Success

Seeing the Results

Here you are, completed with the deployment of an API using SAM (serverless application deployment) AWS-CLI. You can scroll to API Gateway in your AWS console and have a look by hitting the URL.

output from the URL inside API Gateway

More with Serverless using AWS Lambda

Exploring the deployment of a complete Python Flask application to AWS Lambda with some external python library like Zappa. It is a library to deploy serverless web apps on AWS Lambda. In the upcoming blog post, we will be building a simple Flask web app with Python and run the web app on AWS Lambda using Zappa in only a few steps.
Happy Coding!

--

--