Cover image by Thomas Couillard
Hello Again đđ˝
Letâs get the meme out of the way: Serverless services still have a server. The naming is just used to imply that itâs a server that you no longer have to manage.
This means even though there is a server involved at some point, we donât have to mess with it. So instead of patching OS updates, setting up scaling rules, etc. we can focus on our business logic and deploy a function.
Thatâs right. A lambda is just a function that lives on AWS. You put your code in it, and some kind of event calls the function.
đď¸ Need to know terms
Serverless: A managed service that automatically scales based on your traffic where billing is based on usage.
Lambda: A type of serverless service that allows us to write code that executes in a function that is called by some type of event.
Trigger: An event that invokes a lambda
Itâs worth reiterating, that a Lambda is event-driven. That event can be anything from someone coming to your website, to a new user signing up, to some scheduled interval that runs every 5 minutes or so. Thereâs a lot of flexibility when it comes to what triggers the event.
đď¸ Creating our first Lambda
Using a step-by-step process, letâs go ahead and create our lambda function:
- Log into your AWS account, search for Lambda using the search bar at the top, and select the first option
- Click the âCreate functionâ button
- Starting from scratch, name your function pets-get-func
- Click the âCreate functionâ button
đ Thatâs it! Congratulations on creating your Lambda function!
đĄ Reviewing the Lambda Dashboard
With our function created, letâs take a moment to explore what weâre now presented with.
In the above screenshot, the top portion is known as the âFunction Overviewâ. Since Lambdaâs are event-driven, we can configure a trigger as well as the destination for the response.
Further down, we see the âCode Sourceâ panel. While itâs fine to write code in this built-in editor (as weâll soon do), for bundles of code larger than 50mb, we can also use the âUpload fromâ dropdown.
In a typical flow however, youâll most likely use the CLI or another tool to push your function code to AWS.
đŤ Running our lambda function
As you can see from the code editor, a lambda function can really be as simple as something that just returns some data. A couple of things to note:
Functions are stateless. So youâre not going to want to put any variables outside your function handler.
The event argument contains information relating to whatever triggered the function. Since many things can do just that, this argument is contextual.
Go ahead and update your function so that it looks like the screenshot below and click âDeployâ
Next up is to create a test event for our event variable:
- Click Test
- Name your event âexampleEventâ
- click Create at the bottom. Now that we created a test event, we can click the Test button one more time and our function will run.
đ Congratulations on running your lambda function!
While viewing the execution results, we can see the response body as well as meta information such as how long the function took to run. Execution time is something to be mindful of because it relates to how youâll be billed.
Speaking of billing, letâs wrap this up with a chat on pricing.
đ¸ Pricing
A trend with serverless services is that youâll typically pay for two things:
compute: How long it takes for something to run
requests: How many times your service is used
Using today's default lambda in us-east-2, the pricing is as follows:
Compute: $0.0000000021 per/ms
Requests: $0.20 per 1M requests
đ If those penny-fractions get you worried, fear not, AWS also provides a free amount of usage each month:
1M free requests per month and 400,000 GB-seconds of compute time per month
https://aws.amazon.com/lambda/pricing/
As we get along further in understanding some of the core AWS services, I do want to make sure your account is set up correctly to avoid any unexpected charges đ
So for those interested, I created a video you can checkout to make sure you're covered
Top comments (0)