This article was published as a part of the Data Science Blogathon.
While deploying an application, developers often face challenges concerning purchasing, provisioning, and managing backend servers. AWS Lambda is a service that allows developers to run code without having to set up and manage such servers and hence is often classified under the bucket of ‘serverless architecture.’ ‘Serverless.’ However, this doesn’t mean that there are no servers involved. It means that developers wouldn’t have to think about those servers as AWS handles all of them.
Lambda functions are frequently utilized for event-driven programming. The objective of this article is to give a brief introduction of what a lambda function is, followed by a demonstration of how to create it on AWS.
In AWS Lambda, we write and deploy functions instead of applications. These functions enable users to run code in the cloud without requiring them to set up infrastructural systems. At the lowest level, these functions process events. An event can be of several types. To give an example, let us consider the real-time data transformation scenario. Sometimes, we require restructuring or amending the data before sending it to the final database for storage. For this scenario, a lambda function can be prompted whenever a new batch of streaming data comes in for carrying out these transformations before sending it to a database for storage.
To sum up, you just need to send your function’s code to AWS, and they prepare everything on the backend (without you having to worry) to run it. You pay for the time the function gets to run in a “pay-as-you-go model.”
Lambda should be your choice when looking for event-driven or trigger-based programming, while EC2 should be leveraged when you need a more customized solution to fit your needs. EC2 provides an actual OS to work with, such as Linux or Windows, while Lambda provides a function as a service.
Lambda is frequently utilized for data manipulation that needs to take place constantly. Consider the scenario of uploading customer data into the cloud every week, where you need to mask any sensitive data once it lands in an S3 bucket. This is a perfect case for leveraging a lambda function. The lambda function can loop through the bucket and mask the data as soon as the event of data reaching the S3 bucket happens. This event can be set as a trigger to call the lambda function.
A Lambda function needs a few things to get implemented.
A Handler: A handler is a method in our function code that processes events. It is the python function that gets implemented when our lambda function runs. An example of a handler function is the following:
def lambda_handler(event, context): # TODO implement return { 'statusCode': 200, 'body': json.dumps(event) }
As we can see, the handler function takes in the two arguments – event and context. An event is a data that’s passed to the function upon execution. A context is an object that implements methods. Its main role is to provide information about the invocation, function, and execution environment.
A runtime environment: The runtime will usually correlate directly with the language we have selected for writing the lambda function.
Trigger: For lambda functions to get implemented, an event must occur. If there is an occurrence of an event, then the lambda function gets prompted. Events could be anything happening to the resources present in our AWS account. The upload of a file to S3 and the addition of a record to DynamoDB are examples of events that can be prompted for executing lambda functions.
AWS Lambda natively supports Java, Go, PowerShell, and Node. Js, C#, Python, and Ruby code provides a Runtime API that allows you to use any other programming languages to author your functions.
No need to set up and oversee any servers. Since AWS Lambda sets up all the infrastructural requirements in the background, the user can simply pass the code and seamlessly deploy it.
Automatic Scaling. It automatically scales the instances to handle excessive load times and implements a proper logging and error handling system.
Affordability. AWS Lambda charges you only when the code is running. The user is charged for every 100 ms of code execution and the number of times the code is triggered.
Owing to the popularity of serverless architecture, this article hopes to give you hands-on help for creating a simple AWS Lambda function capable of fetching data from an external API.
Step 1. Creating a Lambda Function
Search for Lambda on AWS Management Console. Once you are on the AWS Lambda page, you can see an option of ‘Create Function’ that can be used for creating your Lambda function.
In the next window, we can choose ‘Author from scratch’ for creating our code. We would then need to give a function name and choose the Runtime based on the programming language we want to use.
Concerning permissions, Lambda, by default, creates an execution role with permissions to upload logs to Amazon CloudWatch Logs. If a Lambda function requires permissions to call other AWS APIs, you’ll need to grant enough IAM permissions for other AWS services.
Post these selections; we can click on ‘Create Function’ at the bottom-right for our lambda function to get created.
Step 2. Coding our Function
We will create the function here to fetch weather from an external API (OpenWeatherMap API).
We can write the following in ‘Code Source’ (replacing the default code) :
from datetime import datetime def lambda_handler(event, context): conn = http.client.HTTPSConnection("community-open-weather-map.p.rapidapi.com") headers = { 'X-RapidAPI-Key': "your_own_api_key_here", 'X-RapidAPI-Host': "community-open-weather-map.p.rapidapi.com" } conn.request("GET", "/weather?q=London%2Cuk&lat=0&lon=0&callback=test&id=2172797&lang=null&units=imperial&mode=xml", headers=headers) res = conn.getresponse() data = res.read() # Return the desired weather information for today from the dictionary object return {"statusCode": 200,"body": data}
Step 3. Deploying & Testing the Function
Once we have written the code, we can click on ‘Deploy’ to deploy it and then further click on ‘Test’ to check the response we get.
As we can see, we get the desired response (weather of London) as output.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.