Last Update: Jun 23, 2023 | Published: Jun 06, 2022
In this guide, I’ll explain how AWS Lambda lets you run your code in a cloud-based ‘serverless’ infrastructure, and how you can create an AWS Lambda function to perform any type of computing task.
There are four different ways to create an AWS Lambda function, but I’ll be using the web-based AWS Management Console to do it. I’ll also show you how to invoke your first function and how to monitor it using AWS CloudWatch metrics.
AWS Lambda is a service used for deploying applications in the Amazon Web Services cloud. And it doesn’t require you to manage any infrastructure!
Azure Lambda was one of the first Function-as-a-Service (FaaS) platforms. It preceded competing services like Microsoft’s Azure Functions.
Lambda’s serverless architecture helps to reduce costs and complexity when creating event-driven applications that must be able to scale easily. For example, there are no charges when your functions aren’t running.
AWS Lambda functions run code that can be written in various languages such as Node.js, Python, Ruby, Java, Go, and more. Code can be triggered by various events in the AWS cloud, such as:
Thanks to solid integrations with other AWS products, Lambda offers many possibilities and advantages for AWS customers. Here are some examples of what’s possible to do with Lambda functions:
You can integrate your functions with various monitoring and security tools in AWS such as Amazon CloudWatch, AWS CloudTrail, AWS Inspector, or a simple notification service.
Pricing for AWS Lambda depends on the amount of memory you allocate to functions and the time they take to execute. You can allocate any amount of memory between 128 MB and 10,240 MB. Moreover, pricing can vary depending on your location and the type of processor (x86 or ARM) you choose.
With the AWS Lambda Free tier, you get one million free requests per month, as well as 400,000 GB/seconds of compute time. You can use the AWS Pricing Calculator to estimate your Lambda and architecture costs.
First, you must have an AWS account to create a function, and the account must have full permissions for accessing AWS Lambda. If you don’t have an AWS account yet, you create one on the AWS website.
Now, let’s quickly go through the four different ways of creating, invoking, and managing your Lambda functions in the AWS cloud.
Note: To work with the AWS CLI, you will need to create a new role: the IAM role. Similar to an IAM user, an IAM role is an AWS identity providing access to specific permissions.
Now that you have a good understanding of the different ways to create an AWS Lambda function, let’s get into the practicals of creating a basic one using the AWS Management Console:
The code we’re using imports two packages: The first one (os) is used by our function to interact with the operating system. And the second one (json) allows us to work with JSON files.
The Lambda_handler function uses the template (Hello-World). It returns an output with a status code 200 and (Hello from Lambda) in the body.
Import os import json def lambda_handler(event, context): json_region = os.environ[‘AWS_REGION’] return { “statusCode”: 200, “headers”: { “Content-Type”: “application/json” }, “body”: json.dumps({ “Region “: json_region }) }
Now, you will need to configure test events to run the code with specific values you provide for your function. To configure test events, provide the parameters below and save them:
Your functions don’t do anything until you invoke them. Let’s perform the steps below to invoke our function:
Now that we have successfully created and invoked our function, let’s learn how to monitor it using AWS CloudWatch metrics.
In this tutorial, you learned what AWS Lambda is, how to create a function using the AWS Management Console, and how to invoke and monitor it.
As microservices replace monolith application architectures, AWS Lambda provides a cost-effective way to run code and create microservices in the cloud. How do you plan to use Lambda functions?