<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Pasquale Coretti</title>
    <description>The latest articles on Forem by Pasquale Coretti (@alevoid).</description>
    <link>https://forem.com/alevoid</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F242487%2F6c65f4cc-d7d8-49dd-94a1-8f8e75efded2.jpeg</url>
      <title>Forem: Pasquale Coretti</title>
      <link>https://forem.com/alevoid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alevoid"/>
    <language>en</language>
    <item>
      <title>Lambda - From Knowing Nothing to Creating Your First API With it in Less Than 10 Minutes</title>
      <dc:creator>Pasquale Coretti</dc:creator>
      <pubDate>Fri, 04 Oct 2019 08:28:10 +0000</pubDate>
      <link>https://forem.com/netguru/lambda-from-knowing-nothing-to-creating-your-first-api-with-it-in-less-than-10-minutes-4mhj</link>
      <guid>https://forem.com/netguru/lambda-from-knowing-nothing-to-creating-your-first-api-with-it-in-less-than-10-minutes-4mhj</guid>
      <description>&lt;p&gt;&lt;strong&gt;Serverless&lt;/strong&gt;  topic is becoming more and more  &lt;em&gt;hot&lt;/em&gt;  these days. The idea behind this architecture style is that a large part of the headaches related to the server’s operational responsibilities can be delegated to a 3d-party provider, so that developers can focus entirely on writing code aligned with the business goal the application serves.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhs-fs%2Fhubfs%2Flambda-img.png%3Fwidth%3D507%26name%3Dlambda-img.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhs-fs%2Fhubfs%2Flambda-img.png%3Fwidth%3D507%26name%3Dlambda-img.png" alt="lambda-img"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this post, I’m going to illustrate how ridiculously easy is to go from don’t-know-even-where-to-put-my-hands, to create a simple API with  &lt;strong&gt;AWS Lambda&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no charge when your code is not running. With Lambda, you can run code for virtually any type of application or backend service - all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What I’m  &lt;em&gt;not&lt;/em&gt;  going to do here,  &lt;em&gt;instead&lt;/em&gt;, is illustrating how to do it properly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why..?
&lt;/h3&gt;

&lt;p&gt;The fact is that, from one side, there is tons of staff that I’m going to knowingly omit (like how to handle routing properly or how to configure serverless webpack and so forth) and from another, what could look like a smart solution for me, could be a really bad one for some other, depending on the project needs. So, I come up with the idea of keeping it as generic as possible and try to write an  &lt;strong&gt;introduction&lt;/strong&gt;  to the subject. And the topic is so vast that it’s probably best to chunk it anyway.&lt;/p&gt;

&lt;p&gt;Specifically, what I would like to do is to go through the process of deploying a lambda using the &lt;strong&gt;&lt;a href="https://serverless.com/" rel="noopener noreferrer"&gt;Serverless Framework&lt;/a&gt;&lt;/strong&gt; that, as its own documentation says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;helps you build serverless apps with radically less overhead and cost. It provides a powerful, unified experience to develop, deploy, test, secure and monitor your serverless applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Serverless Framework is so easy to use that would be equally easy to miss some of the things it does for us under the hood. That is why in this post I'm also going to focus on the AWS console side of the process.&lt;/p&gt;

&lt;p&gt;Now, before getting to the juicy part, there’s a couple of things we want to take care of..&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Create an AWS Account:
&lt;/h3&gt;

&lt;p&gt;First thing to do is to create an &lt;a href="https://aws.amazon.com/" rel="noopener noreferrer"&gt;AWS Account&lt;/a&gt;, if you don't have one already.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Create AWS Access Keys:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Go to Services -&amp;gt; IAM:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2FAWS%2520console%2520-%2520Services-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2FAWS%2520console%2520-%2520Services-1.png" alt="AWS console - Services-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Inside your IAM page, Go to  &lt;strong&gt;Users&lt;/strong&gt;  -&amp;gt;  &lt;strong&gt;Add Users&lt;/strong&gt;;&lt;/li&gt;
&lt;li&gt;  Type the new user  &lt;em&gt;name&lt;/em&gt;  and, since we'&lt;em&gt;re&lt;/em&gt; going to need &lt;em&gt;an access key ID&lt;/em&gt;  and a  &lt;em&gt;secret access key&lt;/em&gt;, we can give the new user a  &lt;strong&gt;Programmatic access&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Next: Permissions&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;  Inside the Set Permissions page, we grant the user administrator access that provides full access to AWS resources and services:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2FAWS%2520IAM%2520-%2520permissions.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2FAWS%2520IAM%2520-%2520permissions.png" alt="AWS IAM - permissions"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Add Tags: optional - we can ignore this part for now (unless you want to add user's information like email, job title, etc);&lt;/li&gt;
&lt;li&gt;  Review and Create User;&lt;/li&gt;
&lt;li&gt;  Save Credentials. You can download the .csv file or copy directly the access key and the secret:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2Fcredentials.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2Fcredentials.png" alt="credentials"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  We can open up the terminal and type:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;serverless config credentials --provider aws --key {Access_key_ID} --secret {Secret_access_key}&lt;/p&gt;

&lt;p&gt;And we should receive a message to inform us that our AWS access keys were successfully stored. This step is going to be necessary when we will want to deploy our Lambdas to AWS.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Start our first serverless project:
&lt;/h3&gt;

&lt;p&gt;So let’s create a new directory and let’s say we want to call it:  &lt;em&gt;my-app-serveless&lt;/em&gt;. You got a better name, go ahed and use it (just make sure to keep it while you’re going through the steps below).&lt;/p&gt;

&lt;p&gt;We'll want also to globally add the serverless framework itself. It’s on  &lt;a href="https://www.npmjs.com/package/serverless" rel="noopener noreferrer"&gt;npm&lt;/a&gt;, so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm add serverless -g
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  And now we want to create the actual project, the relative configuration yml, our first function etc.
&lt;/h3&gt;

&lt;p&gt;We don’t need to do it from scratch, we let  &lt;em&gt;Serverless&lt;/em&gt;  do its magic for us.  &lt;em&gt;How?&lt;/em&gt;  Well, for creating a new project we use the command  &lt;em&gt;&lt;strong&gt;create&lt;/strong&gt;&lt;/em&gt;  followed by one of the following options:&lt;/p&gt;

&lt;p&gt;a. the template name we want to use;&lt;/p&gt;

&lt;p&gt;b. the template url;&lt;/p&gt;

&lt;p&gt;c. the local path to your template.&lt;/p&gt;

&lt;p&gt;I’m going to pass in a template name, which is composed of the  &lt;em&gt;provider name&lt;/em&gt;  and the  &lt;em&gt;runtime environment&lt;/em&gt;  I would like to use. They are respectively  &lt;strong&gt;AWS&lt;/strong&gt;  and  &lt;strong&gt;Nodejs&lt;/strong&gt;  separated by a dash. So it’s going to be  &lt;em&gt;aws-nodejs; * you can check all the supported providers and template list on serverless documentation.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, if we're inside the directory where the project is going to be created we can just launch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls create -t aws-nodejs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Otherwise we can use the  &lt;em&gt;path&lt;/em&gt;  flag  &lt;em&gt;&lt;strong&gt;-p&lt;/strong&gt;&lt;/em&gt; /  &lt;em&gt;&lt;strong&gt;-path&lt;/strong&gt;&lt;/em&gt;  to specify it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait a sec...  &lt;strong&gt;sls&lt;/strong&gt;? What is that? And what is happening here?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;sls&lt;/strong&gt;  is short for  &lt;em&gt;serverless&lt;/em&gt;. What we’re basically doing here is pretty simple: we are asking the framework to create for us a new serverless project inside the directory, using an already existing template named aws-nodejs, which also means that we’re letting serverless know that we want to deploy our code to AWS cloud provider. If we wanted, for instance, to deploy to Azure, we would have to use the appropriate template for that.&lt;/p&gt;

&lt;p&gt;Now, looking at our directory, we've probably already noticed that serverless has done a bunch of staff under the hood. We got a  &lt;strong&gt;.serverless&lt;/strong&gt;  dir, a configuration  &lt;strong&gt;serverless.yml&lt;/strong&gt;  and a  &lt;em&gt;hello&lt;/em&gt;  handler (inside the  &lt;strong&gt;handler.js&lt;/strong&gt;  file) already set up.  &lt;em&gt;How cool is that?&lt;/em&gt; Basically we could already deploy it. But we won’t, not for now.&lt;/p&gt;

&lt;p&gt;First, we need to take a look at the  &lt;strong&gt;serverless.yml&lt;/strong&gt;  file. We can ignore for now all the comments and focus on the not-commented part.&lt;/p&gt;

&lt;p&gt;This is how it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;service: my-new-cool-serverless-project  

provider:  
  name: aws  
  runtime: nodejs10.x  

functions:  
  hello:  
    handler: handler.hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;service:  This is the name space for the group of lambdas you’re going to be creating. You can totally rename with what you believe is most suitable (by default it takes the name of the directory the service has been created in).&lt;/p&gt;

&lt;p&gt;provider  -&amp;gt;  name  and  runtime: AWS and nodejs - no surprise there,  &lt;em&gt;am I right?&lt;/em&gt; At the end, that’s what  we’ve asked passing as the template 'aws-nodejs'.  There is a couple of things we could add here like the region (if not specified, defaults to  &lt;strong&gt;US East (N. Virginia)&lt;/strong&gt;) and the stage (that defaults to  &lt;strong&gt;dev&lt;/strong&gt;). Since I like to have everything clear in my .yml, I’m gonna go ahed and add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;region: eu-central-1
stage: dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;functions:  that is followed by our lambdas. Right now we got just one function:  &lt;strong&gt;hello&lt;/strong&gt;  that can be renamed however we wish. Now, if you look at its handler, it points at the  &lt;em&gt;handler.js&lt;/em&gt;  file that  &lt;em&gt;sls&lt;/em&gt;  created and the exported function hello.&lt;/p&gt;

&lt;p&gt;You can think of the handler as the name of the actual function that is going to be deployed on AWS platform. We can rename it as we wish inside our  &lt;em&gt;yml&lt;/em&gt;  file, as long as we keep the handler path relative to the actual function's source. So, if we were to move the handler to a source directory  &lt;em&gt;/src&lt;/em&gt;  we would have to rewrite it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;functions:hello: // the name of the lambda functionhandler: src/handler.hello // where the function source code is and the name of the function in the code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty straight, right? Ok, let’s take a look at the handler function itself. At time of writing it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use strict";module.exports.hello = async event =&amp;gt; {  return {    statusCode: 200,    body: JSON.stringify(      {        message: "Go Serverless v1.0! Your function executed successfully!",        input: event      },      null,      2    )  };  // Use this code if you don't use the http event with the LAMBDA-PROXY integration  // return { message: 'Go Serverless v1.0! Your function executed successfully!', event };};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, this function takes in a parameter  &lt;strong&gt;event&lt;/strong&gt;  and returns an object with status code and stringified body. About the  &lt;strong&gt;Event:&lt;/strong&gt;  if you go and look for it in the serverless documentation for  &lt;a href="https://serverless.com/framework/docs/providers/aws/guide/intro/" rel="noopener noreferrer"&gt;AWS&lt;/a&gt;  provider, you’ll see that it’s described as “&lt;em&gt;anything that triggers an AWS Lambda Function to execute”&lt;/em&gt;. It actually makes sense since we know that lambdas are  &lt;strong&gt;event-triggered&lt;/strong&gt;. You can take a look at all the kind of events our lambda can subscribe to in AWS, and we’re going to do it right after our deployment. And probably, the event everybody is looking forward to subscribe  their  lambda to is the  &lt;em&gt;&lt;strong&gt;AWS API Gateway HTTP endpoint request&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;* FYI, our hello function actually also accepts context and a callback as parameters, but I won’t touch them here as they’re not relevant to what we’re trying to accomplish;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, even though we haven't written a single line of code yet, we can already move on testing our function - and the good news is that we don’t need to deploy it first. We can test it locally to check that everything’s fine.&lt;/p&gt;

&lt;p&gt;We’re going to use the command  &lt;em&gt;invoke local&lt;/em&gt;  followed by the name of the function. So if you haven’t renamed it, the command will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls invoke local -f hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point you should see on your terminal exactly what our lambda is returning.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why are we testing it?
&lt;/h3&gt;

&lt;p&gt;Well, the  &lt;em&gt;invoke local&lt;/em&gt;  command is a great tool to check that our lambda is running. We just need to be aware that it won’t always work out of the box, like it does in this situation. In some cases we will need to pass the actual event the lambda is going to respond to. We're going to check all the kind of events lambdas can subscribe to a bit ahed in this post. For now, I can anticipate that we can copy the specific event type, whatever it is, from AWS, pasting inside a JSON or YAML file and pass it to our lambda using the  &lt;em&gt;&lt;strong&gt;-p&lt;/strong&gt;&lt;/em&gt;  flag.&lt;/p&gt;

&lt;p&gt;Now that we know our lambda is running, we are actually ready to  &lt;strong&gt;deploy&lt;/strong&gt;  it.&lt;/p&gt;

&lt;h3&gt;
  
  
  How?
&lt;/h3&gt;

&lt;p&gt;We can check the possibilities we have by typing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls deploy —help
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we've got different options, from deploying the entire serverless service to deploy single functions; in this case, we want to deploy the service, so we’ll just type in our terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;* serverless won't be able to deploy the service if you haven't set up credentials already - in case: go back to Create AWS Access Keys section.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The process is going to be pretty fast since we’ve got just one function to deploy, but it’s going to take more time once the project grows.&lt;/p&gt;

&lt;p&gt;If we wanted to check the deployment status, how everything's going, if anything's failed, there are two different ways to do it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Going into your  &lt;em&gt;AWS console&lt;/em&gt;  -&amp;gt;  &lt;em&gt;Services&lt;/em&gt;  -&amp;gt;  &lt;em&gt;Cloudformation&lt;/em&gt;: here you'll find your stack by the name you gave it in your service (in the  &lt;em&gt;yml&lt;/em&gt;  file). Clicking on it, you’ll be able to check all the updates. (Small piece of advice, be sure to be on the correct region - specifically, the region you passed to your yml configuration file and region in your AWS console needs to match - you can change it from the navigation bar on top, near your login name);&lt;/li&gt;
&lt;li&gt; Running the deploy in verbose mode:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls deploy -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the deployment's completed, we can finally check our Lambda. From your AWS console, just go back to  &lt;em&gt;Services&lt;/em&gt;  -&amp;gt;  &lt;em&gt;Lambda&lt;/em&gt;  and you should be able to see your function under the functions list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2FServices%2520Lambda.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2FServices%2520Lambda.png" alt="Services Lambda"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2FLambda%2520functions-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2FLambda%2520functions-1.png" alt="Lambda functions-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The name of the lambda is the result of the service name, the staging environment, and the function name. We can click on it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2FScreen%2520Shot%25202019-09-01%2520at%252013.07.14-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2FScreen%2520Shot%25202019-09-01%2520at%252013.07.14-1.png" alt="Screen Shot 2019-09-01 at 13.07.14-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inside our lambda we see 2 tabs:  &lt;em&gt;Configuration&lt;/em&gt;  and  &lt;em&gt;Monitoring&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Their name seem pretty intuitive. The first one is, indeed, for  &lt;strong&gt;Configuration&lt;/strong&gt;. You can check here the function's code, runtime, handler name, and all the stuff you’ve set up inside your serverless configuration file.&lt;/p&gt;

&lt;p&gt;Here you could also add  &lt;strong&gt;Environment variables,&lt;/strong&gt;  modify basic settings like memory allocation for the specific lambda (defaults to  &lt;em&gt;1024&lt;/em&gt;  - and it can go up till 3008MB - and remember that although increasing it can positively impact your performance, it’s also going to affect costs) and timeout.&lt;/p&gt;

&lt;p&gt;On this regard, since memory size and function execution time play (along with the cost per function's invocation) a key role in the cost calculation, a good way to minimize them is to write as quick and as low RAM consuming functions as possible. It is also a good practice to set the RAM quota and timeout to a low number on your testing AWS instance (to minimize the risk of deploying a function that e.g. falls into an endless loop and increases the resource usage and the subsequent costs that we need to pay).&lt;/p&gt;

&lt;p&gt;You have the possibility to change settings here or in your  &lt;em&gt;yml&lt;/em&gt;  file. Whatever the decision is, just remember to keep it consistent; all the configurations should be kept in one place only.&lt;/p&gt;

&lt;p&gt;Inside  &lt;strong&gt;Monitoring&lt;/strong&gt; tab, instead, we can check the CloudWatch Metrics, relative to the function’s performance - so whenever we want to check how our lambdas are doing, this is the place we want to be in.&lt;/p&gt;

&lt;p&gt;Looking up at the top right corner of the page, there’s a  &lt;strong&gt;Test&lt;/strong&gt;  button. Yup, I said before we can test our lambda locally using the command  &lt;em&gt;invoke local&lt;/em&gt;  and I also said that sometimes we will need to pass the event our lambda has subscribed to in order for the test to work,  &lt;em&gt;remember&lt;/em&gt;? Well, clicking on the Test button will open up a series of possibilities for us:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; To create a new test specifying the Event we want to pass to our function, and test it;&lt;/li&gt;
&lt;li&gt; To copy the event we want to pass to our lambda when we test it locally;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you can see, there’s really a lot of events you can use, from Amazon CloudWatch to Alexa Smarthouse - Control. Clicking on any of this event will prompt the source code, that you can copy/past in json/yml file.&lt;/p&gt;

&lt;p&gt;If we choose now to test our lambda we would be free to pass any event we like, give it a name, and create our first not-really-useful test.  &lt;em&gt;Done?&lt;/em&gt;  Alright, now we watch the test succeed.  &lt;em&gt;Niice!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Another interesting thing is that if you go to the  &lt;strong&gt;S3&lt;/strong&gt;  service on your AWS console, you will also see the bucket name of your service (as: your service  &lt;em&gt;name&lt;/em&gt;,  &lt;em&gt;stage&lt;/em&gt;  and  &lt;em&gt;serverlessdeploymentbucket&lt;/em&gt;  followed by some  &lt;em&gt;key&lt;/em&gt;). At the end of the day, that's what  serverless deployment actually is: packing the service/function to a .zip and uploading it to the provider specified in the &lt;em&gt;yml&lt;/em&gt; file (AWS in our case).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2Fs3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2Fs3.png" alt="s3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's right, here's another thing Serverless Framework is doing for us (instead of having to manually zip and upload the service by ourselves).&lt;/p&gt;

&lt;p&gt;If, at any point, we need to remove it from s3, it's going to be better not to do it directly from here. The best way to do it it is using:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sls remove&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;that is going to remove the bucket and stack, allowing us to avoid out-of-sync kind of situation in which we would end up having to remove and re-deploy everything.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is it useful to know our lambdas are stored here?
&lt;/h3&gt;

&lt;p&gt;In case anything goes wrong with them, we can always come here, download them, and check if there is any error in the source file.&lt;/p&gt;

&lt;p&gt;Ok, I believe now you have a pretty wide idea of where you lambda-related stuff is on the AWS console and it’s time for us to finally start building our API.&lt;/p&gt;

&lt;p&gt;There are, again, different ways of doing it. Theoretically we could use Amazon API Gateway which is, as the name suggest, a gateway for the api, which would be responsible for routing our http events to our lambda functions. We could, but we don’t have to. There’s a simpler way. How does it sound using our configuration file for achieving the same result?&lt;/p&gt;

&lt;p&gt;We’re going to subscribe to the event called Amazon API Gateway Proxy (you can check it in Lambda -&amp;gt; function_name -&amp;gt; test).&lt;/p&gt;

&lt;p&gt;Using the proxy implementation of API Gateway (used by default by serverless), is going to give our lambda the power to decide how the response should look like. Without it, we would need  to go inside the Amazon API Gateway and configure there what the response should look like - and that’s probably not what we want to get into.&lt;/p&gt;

&lt;p&gt;To set up the gateway locally, we have to go back to our  &lt;em&gt;yml&lt;/em&gt;  file. And this time we’re going to hook our lambda to an http event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;functions:    hello:      handler: handler.hello      events:         - http:           path: /hello           method: GET
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or we can use the shortcut:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;functions:  hello:    handler: handler.hello    events:      - http: GET /hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, whenever someone does a get request to&lt;/p&gt;

&lt;p&gt;&lt;em&gt;wherever_your_gateway_is_hosted&lt;/em&gt;&lt;strong&gt;/hello&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;it’s going to execute  &lt;strong&gt;handler.hello&lt;/strong&gt;  for  &lt;strong&gt;hello&lt;/strong&gt;  function, it’s going to pass the event that AWS gateway formatted as the first argument, it’s going to wait for a response or an error and respond back to whoever made that request.&lt;/p&gt;

&lt;p&gt;Now about running this function, we won’t invoke it locally as we would not be able to check if it’s working with an api. What we need instead is a way to run an offline server that can execute our lambda..&lt;/p&gt;

&lt;p&gt;We basically need to imitate the api gateway server on our machine. Since our functions are serverless and they’re going to be executed in a different context, the api gateway on our machine is going to execute our lambdas for us just like it would on AWS.&lt;/p&gt;

&lt;p&gt;Let’s do it. We need to add  &lt;strong&gt;serverless-offline&lt;/strong&gt;  plugin into our dependencies and add it in our configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add serverless-offline// and inside our serverless.yml let's add: plugins:  - serverless-offline
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last thing we want to take care of before seeing the results of our work is modify what we're returning from our lambda:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports.hello = async event =&amp;gt; {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: "Hey there, Welcome!",
        input: event
      },
      null,
      2
    )
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to stringify the body - that's not something is going to be done for us.&lt;/p&gt;

&lt;p&gt;And now we can just run:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sls offline&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;and go check if it works on: localhost:{PORT}/api. If you didn’t set up a port it defaults to 3000. You can set it up via  &lt;em&gt;-P&lt;/em&gt;  flag.&lt;/p&gt;

&lt;p&gt;If everything works correctly you should see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2Fapi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.netguru.com%2Fhubfs%2Fapi.png" alt="api"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your message and event are printed on the page.&lt;/p&gt;

&lt;p&gt;If you want to add a parameter  &lt;em&gt;name&lt;/em&gt; to greet, you'd have to add it in the configuration file, so the event would become:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;- http: GET /hello/{name}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And we can take the parameter from our  &lt;em&gt;event.pathParameters.name&lt;/em&gt;, so that our lambda would become:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports.handler = async event =&amp;gt; {
  const {
    pathParameters: { name }
  } = event;

  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: `Hey there ${name}, Welcome!`,
        input: event
      },
      null,
      2
    )
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, following this logic you could add a new handler to take care of another end point, and so on... I am going to leave up to you the kind of approach you will decide to experiment with.&lt;/p&gt;

&lt;p&gt;Those that I have been playing with so far are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; using the api gateway to proxy to one lambda and that one function to proxy to  &lt;strong&gt;express&lt;/strong&gt;  and having express to define the routes;&lt;/li&gt;
&lt;li&gt; using the same handler for all the lambdas and handle routing based on the functions' names (that is available in  &lt;strong&gt;context&lt;/strong&gt;  passed to the handler);&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Long story short...
&lt;/h3&gt;

&lt;p&gt;We talked about the Serverless framework -  &lt;strong&gt;how to set up a project&lt;/strong&gt;,  &lt;strong&gt;what it does under the hood&lt;/strong&gt;,  &lt;strong&gt;how to modify the  &lt;em&gt;serverless.yml&lt;/em&gt;  based on your need.&lt;/strong&gt;  We went more into details on lambdas and  &lt;strong&gt;how to test them&lt;/strong&gt;,  &lt;strong&gt;how to work&lt;/strong&gt;  &lt;strong&gt;/ where to find what you may need while working with them&lt;/strong&gt;  inside the AWS console, completing it with the creation of a simple &lt;strong&gt;working API&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And yet, there are parts I've not even had time to touch, like lambdas's  &lt;em&gt;cold starts&lt;/em&gt;  and how they can affect costs, how to build lambda functions with  &lt;a href="https://github.com/serverless-heaven/serverless-webpack" rel="noopener noreferrer"&gt;Webpack&lt;/a&gt;  or how to setup a serverless application with a GraphQL endpoint. But, I am going to try to keep writing on this topic and sharing details or info that I find useful for a general deeper understanding! :)&lt;/p&gt;

</description>
      <category>aws</category>
      <category>serverless</category>
      <category>lambda</category>
    </item>
  </channel>
</rss>
