close

Security threats keep coming! Learn how to protect your organization.

Home

Amazon Web Services

How to Create an AWS Lambda Function

Author avatar - Sagar

Sagar

|
AWS (Amazon Web Services)

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.

advertisment

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.

What is an AWS Lambda function?

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:

advertisment

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:

  • Execute a lot of database queries without exhausting database connections such as Amazon DynamoDB streams.
  • Mount an Amazon Elastic File System (EFS) in any AWS EC2 instance (virtual machine).
  • Deploy Docker images and Docker containers, such as Nginx or Apache containers, on Amazon Kubernetes.

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. 

Can you create an AWS Lambda function for free?

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.

advertisment

Four different ways to create AWS Lambda functions

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.

  • AWS Management Console – You can use the web-based AWS Management Console to manually create your functions. I’ll show you how to do that later.
  • AWS Command Line Interface (AWS CLI) – If you want to use commands instead of the AWS Management Console, then use the AWS CLI, which supports Windows, macOS, and Linux.

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.

  • AWS SDKs – If you are looking to directly create Lambda functions using language-specific APIs and manage them, then you should use AWS SDKs.
  • AWS CloudFormation – You can also use AWS CloudFormation, an Infrastructure-as-Code (IaC) service for creating templates that define your function.

How to create a Lambda function in the AWS Management Console

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:

  • Open your web browser, navigate to the AWS Management Console, and log in.
  • In the console, click on the search bar at the top, search for ‘Lambda’, and click on the Lambda menu item.
Accessing the Lambda service in the AWS Management Console
  • Next, click Create function on the right side of the screen, as shown below.
We create a function in the AWS Management Console
  • Now, select the parameters below and then click on Create function.
    • Select Author from scratch
    • Here, we’ll enter petri-lambda as the function name
    • Set the Architecture to X86_64.
We configure our Lambda function

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
        })
    }
We enter the code for our function
  • Click Test after entering the code.

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:

  • Set the Test event action to Create new event
  • We set the Event name to Petri-lambda
  • Set Event Sharing to Private
  • We named the template Hello-world
We configure test events for our code

How to invoke an AWS Lambda function

Your functions don’t do anything until you invoke them. Let’s perform the steps below to invoke our function:

  • Invoke the function by clicking Test and choosing (Saved event: Petri-lambda).
  • Now you will notice that the code has been executed successfully. In the Response field, you will see “Hello from Lambda,” so we know that the function was executed successfully.
We invoke our function

How to monitor AWS Lambda functions

Now that we have successfully created and invoked our function, let’s learn how to monitor it using AWS CloudWatch metrics.

  • Again, on the same Lambda function page, click on the Monitor tab.
  • You will notice that there are three tabs: Metrics, Logs, and Traces, which can be viewed in the Monitor console as shown below.
  • On the Metrics tab, you will see a graphical view of your function activity with details about Invocations, Duration, Error count, and success rate.
We monitor it using AWS CloudWatch metrics
  • To check the logs further, click on the Logs tab. You’ll see which API call was made, how long the API request took, get information about memory size, etc.
We can get more details on the Logs tab

Conclusion

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?

Article saved!

Access saved content from your profile page. View Saved