[🐾Editorial Note:🐾 Canela is my service dog. She's basically my other half, so she's featured in my blog posts. And if you stick till the end of each post, you get to see a new pic of her in each post.]
Have you ever wondered about words like Serverless and Nonrelational Databases?
Well cheer up, my friends... it's time to learn and practice!
Today, we'll learn the basics of running code on AWS Lambda without managing servers. First, we'll create a 'Hello Canela' Lambda function using the AWS Lambda console. Second, we'll manually invoke our Lambda function. Lastly, we'll review our output metrics.
But first, can you tell me what Serverless really means?
There are a few differences between traditional cloud computing and serverless.
For one, customers don't pay for underutilized resources. And instead of spinning up a server in AWS for example, you’re spinning up code execution time. The serverless computing service takes your functions as input, performs logic, returns your output, and then shuts down. You are only billed for the resources used during the execution of those functions.
WAIT... while you're at it, what is a “Lambda function”?
The code you run on AWS Lambda is called a “Lambda function.”
Oh! Ok, but how do “Lambda functions” work?
After you create your Lambda function, it's always ready to run if triggered. (ex: Think of running a formula in an EXCEL spreadsheet.)
Lambda functions are “stateless,” with no resemblance to underlying infrastructure. This allows Lambda to launch as many copies of functions as needed, in order to scale to the rate of incoming events.
After you upload your code to AWS Lambda, you can connect your function with other AWS resources such as an S3 bucket, an Amazon DynamoDB table, an Amazon Kinesis stream, etc. When the AWS resource changes, Lambda will execute your function and manage the compute resources as needed in order to keep up with incoming requests.
🛑Did You Know? 🛑 Everything done in this tutorial is free tier eligible.
Step 1: Enter the Lambda Console
Go to the AWS Management Console. Find Lambda under Compute and click to open the AWS Lambda Console.
Or, you can choose to search for it by name in the search box.
Step 2: Select a Lambda Blueprint
Blueprints have sample code for doing minimal processing. Blueprints process events from event sources such as Amazon S3, DynamoDB, etc.
a. Once inside the AWS Lambda console, click Create a Function.
The console shows this page only if you do not have any Lambda functions created yet. If you have created functions already, you will be taken to the Lambda > Functions page. On the list page, you'd choose 'Create a function' to go to the Create function page.
b. Select Use a blueprint.
c. Notice how in the Filter box, you can search by runtime, keyword, and Blueprint name.
We're going to search by keyword and input hello-world-python and select the latest 3.7 hello-world-python blueprint.
d. Click Configure.
Step 3: Configure and Create Your Lambda Function
a. Now we have to fill in some 'Basic Information' about your new Lambda function.
- Function Name: Name your Lambda function hello-world-python.
- Execution Role: Create an IAM role (referred as the execution role) with necessary permissions for AWS Lambda to invoke your Lambda function on your behalf. Select Create new role from AWS Policy templates.
- Role Name: Name it lambda_basic_execution
Lambda Function Code:
In this section, you can see the sample Python code we're using in this function template.
b. Go to the bottom of the page and select Create Function.
c. Runtime: Leave the default Python 3.7 as the runtime.
d. Handler: You can specify a handler (function in your code) where AWS Lambda can begin executing your code. Here we see Lambda identifies this from the code sample and it's already pre-populated with lambda_function.lambda_handler.
e. Scroll down to configure your memory, timeout, and VPC settings. For today's post, we can leave the default Lambda function configuration values. (Notice the default value in Network is to select no VPC.)
Step 4: Invoke Lambda Function and Verify Results
Now it's time to go to our console and test the hello-world-python Lambda function.
a. Select Configure test events from the drop-down menu called "Select a test event...".
b. The Configure test event editor pops up to enter an event to test your function.
- Event Template: choose Hello World
- Event Name: I went with HelloCanelaEvent.
- JSON values: You can change the values in the sample JSON, but don’t change the event structure. For this post, replace value1 with 'Hola, Canela!'.
- Select Create.
c. Select Test.
d. Upon successful execution, check out the console results:
- Execution results: verifies that the execution succeeded
- Summary: shows key information reported in Logs
- Log output: shows logs generated by the Lambda function execution.
Step 5: Monitor Your Metrics
AWS Lambda automatically monitors Lambda functions and reports metrics through Amazon CloudWatch. To monitor your code as it executes, Lambda automatically tracks the number of requests, the latency (delay before a transfer of data begins) per request, and the number of requests resulting in error. Then it publishes the associated metrics via Amazon CloudWatch.
a. Invoke the Lambda function a few more times by repeatedly clicking the Test button. (This will generate the metrics that can be viewed in the next step.)
b. Select Monitoring to view the results.
c. Scroll down to view the metrics for your Lambda function.
The Monitoring tab shows all kinds of Amazon CloudWatch metrics: Invocation count, Invocation duration, Invocation errors, Throttled invocations, Iterator age, etc.
If you feel like it, you can also click the View logs in CloudWatch button to go check out your logs there too!
Once you click into the logs your Lambda function generated, you can see the values of "Hola Canela!" printed out.
With AWS Lambda, you pay for what you use. After you hit your AWS Lambda free tier limit, you are charged based on the number of requests for your functions (invocation count) and the time your code executes (invocation duration).
Step 6: Delete the Lambda Function
You won't get charged for keeping this Lambda function, but it's always a best practice to go back and delete resources in AWS you're no longer using.
a. Select the Actions button and click Delete Function.
b. You will be asked to confirm your termination - select Delete.
🥳You just created your 1st AWS Lambda function!🥳
This is your first step in learning how to run apps without needing to provision or manage servers. Lambda automatically scales your apps by running your code in response to each trigger, scaling precisely with the size of your workloads.
¡Gracias por tu tiempo!
~Alejandra💁🏻♀️ & Canela🐾
Top comments (2)
I’m following you just for the photos of Canela 😄 so keep them coming!
Great article by the way!
haha! love this.. thank you :)