DEV Community

Cover image for Basics of AWS Lambda
Amir Azhar
Amir Azhar

Posted on

Basics of AWS Lambda

YOU HAVE BEEN WARNED!

You see, I'm no expert at Amazon Web Services (AWS) or Lambda functions. As a matter of fact, I only just started learning about them 2 weeks ago and have since played around with it during my internship. So please don't come at me if this post isn't as informative as you might have expected. Maybe look at this post as a sort of learning diary by yours truly! With that said, let's get started!

Table of Contents

  1. What is Lambda
  2. Why Lambda
  3. How Lambda
  4. Closing Remarks

What is Lambda

Like many of you, I've seen the words AWS and Lambda before on the internet but never knew what they meant or what they represented. But now, I'm no longer a lost sheep. So here is some background of AWS and Lambda.

AWS

Amazon Web Services or AWS in short, is a subsidiary of Amazon (thank you Papa Bezos) that provides on-demand cloud computing platform and APIs for companies and individuals. To clarify the technical jargon cloud computing, it is simply an access to required services such as servers, databases, networking without having to care about managing the actual computer resource. I know, sounds fancy, but that's just the tip of the iceberg of what AWS can offer. Once again, I'm no AWS sensei so we'll only be focusing on one of the services AWS offers - Lambda.

Lambda

As defined on the AWS website,

AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers.

So, the word serverless, is a tad bit misleading. It doesn't mean that there aren't any servers. It just means that we don't have to worry about whether our servers have enough resources for our application. All that will be taken care of by AWS. With that said, Lambdas are simply functions that run on these servers that are provisioned by AWS.

Why Lambda

All this is cool and all but why should YOU use AWS and Lambda? As mentioned earlier, AWS provides a myriad of services. If you're creating your next great startup that requires storage, computing power, databases, analytics and so on. AWS has you covered by being able to offer you all these services under one roof. It makes your development process much more efficient and scalable. So, if you have a resource hungry function that needs integration with a load of different services, why not use Lambda as well.

How Lambda

I know the section title is grammatically incorrect but I just wanted it to fit nicely with the rest of the section titles. Anyways, in this section I'll go through with you a simple step by step process on how I created my first Lambda using the AWS Console. If you haven't already created an AWS account, please do so because I'll be skipping that part and going straight into meat of the matter.

Step 1: Navigating to the Lambda Service

Navigating to the Lambda ServiceNavigating around the AWS console is as simple as searching for the required service on the search bar located at the top of the screen. Once there, you can go ahead and click on 'Create Function'.

Step 2: Creating the function

Blueprint Here, we have 4 options we can choose from but as beginners, we can choose to just focus on the first 2 options.

  1. Author from scratch means to write the function from scratch
  2. Use a blueprint means that we can choose from a wide selection of templates to base our function off of.

In our case, we will be choosing the latter, and using a Node.js hello-world blueprint. Then, we can go ahead and continue configuring.

Step 3: Configuration

Basic InfoAt this stage, you can set the name of your function and also set the execution role. What's an execution role? As mentioned earlier, Lambdas have the power to access other services. By default, its access is not authorized. For starters, selecting the 'Create new role with basic Lambda permissions' will allow us to use the CloudWatch Logs service, a service that helps us collect and store the logs of our Lambda. This should suffice for now but the role can be further modified in the future to allow access to other services whenever you need them.

CodeLastly, the code from the blueprint is also shown right before you finalise the process by clicking 'Create function'.

Step 4: Modifying the Code

Modifying the Code Now that we have created the Lambda, there seems to be a lot to unpack - code properties, runtime settings, layers, throttle bla bla bla. But today, we'll just focus on deploying and running the function.

Before I give a rundown of the code, let me perform some modifications to it so I don't seem like such an amateur.

exports.handler = async (event, context) => {
    var article = event.article
    var followers = event.followers

    // Thank you to my followers
    followers.forEach(follower => console.log(`Hello ${follower}! Thank you for following and reading my posts!`));

    // A shoutout to an interesting article
    console.log(`Here's an article I read recently! ${article.title} by ${article.author}`) 

    return 'All Done!'
};
Enter fullscreen mode Exit fullscreen mode

Let me now give a quick breakdown of the template syntax. handler in the index.js (the entry point) is simply the function that will be called whenever the Lambda is being invoked. This function takes in 2 parameters - event and context - with the former being the more important one.

event can be seen as the input to the function. This parameter can be in the form of a string, array or more commonly an object.

context parameter is an object that provides methods and properties that provide information about the invocation, function, and execution environment. For this example, this parameter is not as important.

Now, let's just straight into testing it so we can see what it does! Don't forget to click on 'Deploy' whenever you make any changes to the code.

Step 5: Testing

Testing Under the testing tab is where can configure our input. Our input is an object that consists 2 properties - article and followers.

article is a nested object that has the author and title properties, corresponding to an insightful article that I recently read.

followers is an array that contains my first 10 followers since I joined Dev.to 3 weeks ago.

With our test input settled, we can go ahead and click 'Test'.

Step 6: Results

Results Given the simplicity of our function, the execution was completed in mere milliseconds. Let's now focus our attention to the Log Output.

START RequestId: c9da68e2-f02a-49bd-ab0c-294fe5f1aeda Version: $LATEST
2021-12-16T15:50:49.461Z    c9da68e2-f02a-49bd-ab0c-294fe5f1aeda    INFO    Hello @leeraiyan! Thank you for following and reading my posts!
2021-12-16T15:50:49.461Z    c9da68e2-f02a-49bd-ab0c-294fe5f1aeda    INFO    Hello @tarantool! Thank you for following and reading my posts!
2021-12-16T15:50:49.461Z    c9da68e2-f02a-49bd-ab0c-294fe5f1aeda    INFO    Hello @ravitejamannam! Thank you for following and reading my posts!
2021-12-16T15:50:49.461Z    c9da68e2-f02a-49bd-ab0c-294fe5f1aeda    INFO    Hello @martinkr! Thank you for following and reading my posts!
2021-12-16T15:50:49.461Z    c9da68e2-f02a-49bd-ab0c-294fe5f1aeda    INFO    Hello @xingtler! Thank you for following and reading my posts!
2021-12-16T15:50:49.461Z    c9da68e2-f02a-49bd-ab0c-294fe5f1aeda    INFO    Hello @chawn! Thank you for following and reading my posts!
2021-12-16T15:50:49.461Z    c9da68e2-f02a-49bd-ab0c-294fe5f1aeda    INFO    Hello @coder5010! Thank you for following and reading my posts!
2021-12-16T15:50:49.461Z    c9da68e2-f02a-49bd-ab0c-294fe5f1aeda    INFO    Hello @sabiyatabassum! Thank you for following and reading my posts!
2021-12-16T15:50:49.461Z    c9da68e2-f02a-49bd-ab0c-294fe5f1aeda    INFO    Hello @chesecheif! Thank you for following and reading my posts!
2021-12-16T15:50:49.461Z    c9da68e2-f02a-49bd-ab0c-294fe5f1aeda    INFO    Hello @smpofana! Thank you for following and reading my posts!
2021-12-16T15:50:49.461Z    c9da68e2-f02a-49bd-ab0c-294fe5f1aeda    INFO    Here's an article I read recently! 25 Website Performance Metrics to Watch in 2022 by anthonynsimon
END RequestId: c9da68e2-f02a-49bd-ab0c-294fe5f1aeda
REPORT RequestId: c9da68e2-f02a-49bd-ab0c-294fe5f1aeda  Duration: 19.03 ms  Billed Duration: 20 ms  Memory Size: 128 MB     Max Memory Used: 55 MB  Init Duration: 143.67 ms    
Enter fullscreen mode Exit fullscreen mode

As expected, the output included all the logs that were intended, thanking my first 10 followers and giving a shoutout to anthonysimon's article, 25 Website Performance Metrics to Watch in 2022. In addition to that, we can see how long each step took, the total execution duration and how much memory was used.

And that's it! We've created our first Lambda function using the AWS console! I know I know, it isn't super impressive but we all start somewhere!

Step ???: Additional Information

Now, you might have been afraid when you saw 'Billed Duration' in the summary after invoking your Lambda function. If you're a broke university kid like me or you're just stingy with your money, fret not! AWS makes sure that you only pay for what you use, charged based on the number of function calls and the time it takes to execute it. Per month, users have 1 million free requests and 400,000 GB-seconds free (up to 3.2 million seconds of compute time) to play around with. Thereafter, Amazon charges users as little as $0.0000002 per request and $0.00001667 for every GB-second used, depending on your region.

If you're just playing around and not trying to build the next unicorn company, then you won't have to worry about paying for now!

Closing Remarks

There you have it! I hope this article gives a rough overview on how to create a Lambda on AWS and maybe even give you a glimpse on how you can use it in your next project. This tutorial handles all the intricacies of making a Lambda using the web console. However, if you want to feel more like a hackerman, you can rest easy knowing that this entire process of creating, deploying and updating a Lambda can be done in a local environment like VSCode. I'll delve deeper into this topic in the future when we explore the Serverless Framework!

Top comments (0)