Run a Serverless "Hola, Canela!🐾" Lambda function
Tags: aws, serverless, tutorial, beginners
Published: 2020-01-30 15:39:46

---
title: Run a Serverless "Hola, Canela!🐾" Lambda function
published: true
description: A Lambda function consists of code you provide, associated dependencies, and configuration. 
tags: aws, serverless, tutorial, beginners
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/zw1xl0nmu55cx19v5zp3.png
---

<em>[🐾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.]</em>


<h3>Have you ever wondered about words like <a href="https://aws.amazon.com/lambda/">Serverless and Nonrelational Databases</a>?</h3>

<p>Well cheer up, my friends... it's time to learn and practice!</p> 

Today, we'll learn the basics of running code on AWS Lambda without  managing servers. First, we'll create a <b>'Hello Canela'</b> Lambda function using the AWS Lambda console. Second, we'll manually invoke our Lambda function. Lastly, we'll review our output metrics. 

<h3>But first, can you tell me what Serverless really means?</h3>

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.


<h3>WAIT... while you're at it, what is a “Lambda function”?</h3>

The code you run on AWS Lambda is called a “<b>Lambda function</b>.” 

<h3>Oh! Ok, but how do “Lambda functions” work?</h3>

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 “<b>stateless</b>,” 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.


<h5>🛑<mark>Did You Know?</mark> 🛑  Everything done in this tutorial is free tier eligible.</h5>




<h2>Step 1: Enter the Lambda Console</h2>

Go to the AWS Management Console. Find <b>Lambda</b> under <b>Compute</b> and click to open the AWS Lambda Console.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qc33cnkqdsveb3hlhbvl.png)


Or, you can choose to search for it by name in the search box.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zl88zt0i6lntp3ibwu5j.png)


<h2>Step 2: Select a Lambda Blueprint</h2>

Blueprints have sample code for doing minimal processing. Blueprints process events from event sources such as Amazon S3, DynamoDB, etc.

<b>a.</b> Once inside the AWS Lambda console, click <b>Create a Function</b>.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cf4569zqy9rfxh3eowik.png)


⚠️<b>Note<b/>⚠️
<em>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.</em>


<b>b.</b>  Select <b>Use a blueprint</b>.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jea6ews1e02a0jdbmmqr.png)


<b>c.</b>  Notice how in the <b>Filter box</b>, you can search by <em>runtime</em>, <em>keyword</em>, and <em>Blueprint name</em>. 


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8ujdur11o3d0lkqu4du4.png)


We're going to search by <em>keyword</em> and input <b>hello-world-python</b> and select the latest 3.7 <b>hello-world-python</b> blueprint.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/45oyvsfum8cwxzpje0od.png)


<b>d.</b>  Click <b>Configure</b>.


<h2>Step 3: Configure and Create Your Lambda Function</h2>

<b>a.</b>  Now we have to fill in some 'Basic Information' about your new Lambda function.

<b>Basic Information:</b>
<ul>
    <li><em>Function Name</em>: Name your Lambda function <b>hello-world-python</b>. </li>
    <li><em>Execution Role</em>: Create an IAM role <em>(referred as the execution role)</em> with necessary permissions for AWS Lambda to invoke your Lambda function on your behalf. Select <b>Create new role from AWS Policy templates</b>. </li>
    <li><em>Role Name</em>: Name it <b>lambda_basic_execution</b> </li>
</ul>



![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dh38jhr4u2rz53gfr1z4.png)


<b>Lambda Function Code:</b>
In this section, you can see the sample Python code we're using in this function template.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6804vvunyayk6loffuo2.png)


<b>b.</b>  Go to the bottom of the page and select <b>Create Function</b>.

<b>c.</b>  <em>Runtime</em>: Leave the default <b>Python 3.7</b> as the runtime.

<b>d.</b>  <em>Handler</em>: 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 <b>lambda_function.lambda_handler</b>.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bv66s69tfub79nd4egew.png) 


<b>e.</b>  Scroll down to configure your memory, timeout, and VPC settings.  For today's post, we can leave the default Lambda function configuration values. <em>(Notice the default value in Network is to select no VPC.)</em>


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/48kulcqs67czzn4bkve4.png)


<h2>Step 4: Invoke Lambda Function and Verify Results</h2>

Now it's time to go to our console and test the <b>hello-world-python</b> Lambda function.


<b>a.</b> Select <b>Configure test events</b> from the drop-down menu called <em>"Select a test event..."</em>.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eppcmzskium0a7n4s7zh.png)



<b>b.</b>  The <b>Configure test event</b> editor pops up to enter an event to test your function.  

<ul>
  <li><em>Event Template</em>: choose <b>Hello World</b> </li>
  <li><em>Event Name</em>: I went with <b>HelloCanelaEvent</b>. </li>
  <li><em>JSON values</em>: You can change the values in the sample JSON, but don’t change the event structure. For this post, replace <b>value1</b> with <b>'Hola, Canela!'</b>.</li>
  <li>Select <b>Create</b>. </li> 
</ul>



![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hn5ncxn3o2zq4ra32o0q.png)



<b>c.</b>  Select <b>Test</b>.



![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cky2292jcquc1lcegbk9.png)



<b>d.</b>  Upon successful execution, check out the console results:

<ul>
  <li><b>Execution results</b>: verifies that the execution succeeded </li>
  <li><b>Summary</b>: shows key information reported in Logs </li>
  <li><b>Log output</b>: shows logs generated by the Lambda function execution.  </li>
</ul>


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2bg6ems5lk2wfrsu1rlq.png)



<h2>Step 5: Monitor Your Metrics</h2>
<p>AWS Lambda automatically monitors Lambda functions and reports metrics through <a href="https://aws.amazon.com/cloudwatch/">Amazon CloudWatch</a>. 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.</p>

<b>a.</b>  Invoke the Lambda function a few more times by repeatedly clicking the <b>Test</b> button.  <em>(This will generate the metrics that can be viewed in the next step.)</em>
</br>

<b>b.</b>  Select <b>Monitoring</b> to view the results.


<b>c.</b>  Scroll down to view the metrics for your Lambda function.  


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0gqpaiojk3bgj7gsldkg.png)



The Monitoring tab shows all kinds of Amazon CloudWatch metrics: <em>Invocation count</em>, <em>Invocation duration</em>, <em>Invocation errors</em>, <em>Throttled invocations</em>, <em>Iterator age</em>, etc. 
</br>
If you feel like it, you can also click the <b>View logs in CloudWatch</b> button to go check out your logs there too!



![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/iseklx25mvgiupdbwdb5.png)



<p>Once you click into the logs your Lambda function generated, you can see the values of <b>"Hola Canela!"</b> printed out.</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pqs266qw4ibrtscn3342.png)



</br>
⚠️<b>Note<b/>⚠️
<em>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). </em>


<h2>Step 6: Delete the Lambda Function</h2>
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.  

<b>a.</b>  Select the <b>Actions</b> button and click <b>Delete Function</b>.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/am4trwtk9jbk4osb254u.png)


<b>b.</b>  You will be asked to confirm your termination - select <b>Delete</b>.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d8irw3fkcw1pd3cv6jbo.png)


<h1>Wohooooo!</h1>
🥳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🐾

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xsgopq6g61nldzwbscdh.jpeg)