5 Things To Master To Write Efficient AWS Lambda Serverless Functions

5 Things To Master To Write Efficient AWS Lambda Serverless Functions

🙌 Hello there! Welcome to The Serverless Spotlight!

In this week's edition we'll look at 5 Lambda features to master to write powerful and efficient Lambda functions to create serverless applications.


AWS Lambda allows you to run server-side code within seconds.

Instead of taking hours to create, provision and configuring servers, with Lambda you can do everything servers allow you to do but without the overhead.

With Lambda, you create a function, write your server code and deploy it.

Then you can easily invoke it as you would your server.

But to write efficient Lambda functions and benefit from their full potential, you need to understand a few key concepts.

Let’s look at these 5 key concepts to understand in AWS Lambda.

1. Creating Lambda Function URLs

Lambda functions have multiple uses cases. Quite often they can be used as triggers from event-driven applications. 

But sometimes we want to invoke a Lambda function directly.

In this case we have 2 options:

  • Create an API with AWS API Gateway and invoke functions through that API
  • Create Lambda function URLs to quickly invoke a function code directly.

Each method has its benefits and use cases. I wrote an article here explaining this.

To create a function URL, head over to your Lambda function.

Below, click on the Configuration tab, and then you will see Function URL in the left side menu.

Click on Create function URL. 

On this page you can specify the Auth type and then check the CORS checkbox. 

Save the function when that’s done.

Now back in the function’s page you will see a function URL which you can directly call on your frontend client.

2. Testing Lambda functions

Testing functions is one of the most useful features Lambda offers. 

Sometimes you do not yet have a frontend client to run tests and just need to test your function with some dummy data.

In the function page, click on the Test tab.

You can add the data you want and click on Invoke to make a test run.

A tab will open displaying the response and result of the function call.

3. Debugging Lambda functions with CloudWatch

Often times the code we write in Lambda hits errors and needs to be debugged.

With AWS CloudWatch, we can have a fine-grained view into every call made by all Lambda functions on your AWS account.

Let’s see how to set this up.

In the Lambda function page, click on the Monitor section.

Near the top you’ll see a button labeled “View CloudWatch Logs”. Click on that.

This will open up a new browser tab and bring you to the CloudWatch log page for that particular Lambda function.

Below you’ll see a list of log streams:

Each of these log streams represent a Lambda function invocation. Each time you invoke your Lambda function, a log stream is generated here.

Click on one to view details of the log event.

Here we can see my Lambda function hit a “Runtime.ImportModuleError” (on the second line).

Clicking on that will expand the event and show you more fine-grained details on the error.

4. Creating Lambda Triggers

Lambda triggers allow you to trigger your Lambda function based on events coming from another AWS service.

Some typical examples are:

  • AWS API Gateway
  • Amazon S3
  • Amazon SQS

For example, you can create an API Gateway and define a path that when called will trigger the Lambda function (Lambda function URL alternative).

As another example, you can set a trigger for S3, so that whenever a file is uploaded to S3, your Lambda function will be triggered. 

Then in the function code you can retrieve the data from the S3 bucket and file and do some processing on it, perhaps save a storage URL to DynamoDB, etc.

To create a Lambda trigger, simply click on the Trigger button on the Lambda function page:

Here you can select a trigger source. If you choose S3, you will be asked to specify the S3 bucket and some additional information.

5. Configuring functions memory and timeout

Lambda functions run with a default memory size and timeout.

By default all Lambda functions will be given 128MB of memory and 3 seconds timeout.

Memory servers to run more code and perform better. The higher the memory, the more processing power it has to run its code.

Higher memory is useful for processing larger datasets.

Timeout is the maximum amount of time a Lambda function will be run before being stopped by AWS.

If the function run exceeds the timeout limit, it will terminate and return a function timeout error.

For more complex workflows and larger datasets processing, you can extend this timeout to up to 15 minutes.

To configure memory and timeout, click on the Configuration tab in your function page.

Click on the Edit button at the top right.

Here you can change the allocated memory for this function.

You can provision between 128MB and 10240 MB (10GB) for a function.

You can also change the timeout time for up to 15 minutes.

Giving both of these higher values will make the function processing more powerful.

Summary

Understanding these five key concepts to write efficient Lambda functions:

  • Creating Lambda function URLs
  • testing functions
  • debugging with CloudWatch
  • setting up triggers
  • configuring memory and timeout

By mastering these important and essential Lambda features, you can enable powerful serverless workflows, writing server code lightning fast.


👋 My name is Uriel Bitton and I hope you learned something in this edition of The Serverless Spotlight

🔗 You can share the article with your network to help others learn as well.

📬 If you want to learn how to save money in the cloud you can subscribe to my brand new newsletter The Cloud Economist.

🙌 I hope to see you in next week's edition!

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics