<?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>DEV Community: Aniebiet Udoh</title>
    <description>The latest articles on DEV Community by Aniebiet Udoh (@revealtheweb).</description>
    <link>https://dev.to/revealtheweb</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%2F931251%2Fe78b9f32-dffd-4d72-9749-2acb10efee90.png</url>
      <title>DEV Community: Aniebiet Udoh</title>
      <link>https://dev.to/revealtheweb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/revealtheweb"/>
    <language>en</language>
    <item>
      <title>How to Build a Serverless API using AWS lambda, API gateway, DynamoDB and Node.js</title>
      <dc:creator>Aniebiet Udoh</dc:creator>
      <pubDate>Fri, 23 Sep 2022 14:39:46 +0000</pubDate>
      <link>https://dev.to/revealtheweb/how-to-build-a-serverless-api-using-aws-lambda-api-gateway-dynamodb-and-nodejs-30o8</link>
      <guid>https://dev.to/revealtheweb/how-to-build-a-serverless-api-using-aws-lambda-api-gateway-dynamodb-and-nodejs-30o8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Recently, I was tasked with building a serverless application for an NFT valuation service, this was not my first time working with the serverless framework, and I’ve always enjoyed the process of building serverless applications.&lt;/p&gt;

&lt;p&gt;In this tutorial, I show how to create a serverless API, hoping you’d enjoy the process as much as do.&lt;/p&gt;

&lt;p&gt;This tutorial walks through creating a serverless TODO application with the serverless framework, AWS Lambda, and DynamoDB for persistence.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Basic understanding of ES6 JavaScipt&lt;/li&gt;
&lt;li&gt;You have Node.js Installed&lt;/li&gt;
&lt;li&gt;An AWS account&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is Serverless?
&lt;/h3&gt;

&lt;p&gt;Serverless architecture is a pattern of providing backend services on demand without application developers needing to have knowledge or worrying about the underlying servers.&lt;/p&gt;

&lt;p&gt;💡 Yes, servers are still used, but this time, app developers don't need to worry about upgrading, patching, or maintaining them.&lt;/p&gt;

&lt;p&gt;To explain further, take the analogy of an automated teller machine (ATM) for example, an ATM machine may have a server running 24/7, and at any time, it's always ready to perform its task.&lt;/p&gt;

&lt;p&gt;But if you think again, at night when most people are asleep, the ATM machine servers are still running and still acquiring costs, even though it's not being used.&lt;/p&gt;

&lt;p&gt;Now if the machine ran on a serverless architecture, the servers would not need to be up and running always, it'll only become active if action is carried out, for example when a user wants to make withdrawals or transfers,  then the required function &lt;code&gt;withdraw&lt;/code&gt; or &lt;code&gt;transfer&lt;/code&gt; will run and that's it, then the server goes back to sleep.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the API
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1 - Creating an AWS account and Setting up AWS CLI
&lt;/h3&gt;

&lt;p&gt;We are going to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an account on AWS (Amazon Web Services)&lt;/li&gt;
&lt;li&gt;Create an IAM user&lt;/li&gt;
&lt;li&gt;Install and configure the AWS CLI (Command Line Interface)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a simple step - if it’s your first time using AWS, I recommend reading through the &lt;a href="https://aws.amazon.com/getting-started/"&gt;Amazon Getting Started&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create AWS Account
&lt;/h3&gt;

&lt;p&gt;Create an AWS account by going to &lt;a href="https://aws.amazon.com/"&gt;https://aws.amazon.com/&lt;/a&gt; and clicking on the Create an AWS  account button at the top right.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ABBTzU0m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vg0behlmri0i1a3rhy91.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ABBTzU0m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vg0behlmri0i1a3rhy91.PNG" alt="Sign up" width="879" height="68"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;💡 Please note, you’ll need a credit/debit card to create an account, you’ll not be charged if you follow this tutorial because the task we perform is still within the free tier of AWS.&lt;/p&gt;

&lt;p&gt;If you plan to experiment further, please ensure to check that you are still within the free tier to avoid being charged.&lt;/p&gt;

&lt;p&gt;Now that we are done creating an account, we will be interacting with AWS from the command line on our local computer via the AWS CLI&lt;/p&gt;

&lt;p&gt;To make that possible, we need a user credential which we’ll give to AWS CLI to enable it to interact with our AWS account, so we need to create an IAM user account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create AWS IAM User
&lt;/h3&gt;

&lt;p&gt;While logged in to your AWS account, enter “IAM” into the search bar at the top and click on the IAM result&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bSWmzq5e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ex5u2bbgwqohb45mdyi.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bSWmzq5e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ex5u2bbgwqohb45mdyi.PNG" alt="Iam" width="880" height="198"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the IAM screen, click on &lt;strong&gt;Users&lt;/strong&gt; and click &lt;strong&gt;Add users&lt;/strong&gt; in the resulting screen&lt;/p&gt;

&lt;p&gt;We enter any username we want to use, in my case, I entered “&lt;strong&gt;todoapp-dev&lt;/strong&gt;”, then click on the checkbox to mark “&lt;strong&gt;Access key - Programmatic access”.&lt;/strong&gt; This is important because we will be interacting with our AWS account locally on our computer using the AWS CLI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zbk5Vd5B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/si9ps2jl7s9czrovy37z.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zbk5Vd5B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/si9ps2jl7s9czrovy37z.PNG" alt="User" width="880" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click “&lt;strong&gt;Next: Permissions&lt;/strong&gt;” at the bottom of the screen to proceed to the next step.&lt;/p&gt;

&lt;p&gt;On the next screen, click “&lt;strong&gt;Attach existing policies directly&lt;/strong&gt;” and mark Check “&lt;strong&gt;AdministratorAccess&lt;/strong&gt;”&lt;br&gt;
&lt;em&gt;(Note: this is not recommended, you only want to grant access to the specific AWS services you’d be using)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After that, Click “&lt;strong&gt;Next: tags&lt;/strong&gt;” leave that as it is, click “&lt;strong&gt;Next: Review&lt;/strong&gt;” and finally click &lt;strong&gt;Create user&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We are then presented with a screen showing our credentials: &lt;strong&gt;Access key ID&lt;/strong&gt; and &lt;strong&gt;Secret access key&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These are the credentials we’ll use to interact with our account from the AWS CLI, don't lose them. &lt;/p&gt;

&lt;p&gt;You can copy them somewhere or just leave the screen as it is, we’ll come back to it.&lt;/p&gt;
&lt;h3&gt;
  
  
  Install and configure the AWS CLI (Command Line Interface)
&lt;/h3&gt;

&lt;p&gt;Now, we need to install the AWS CLI. To install, use the command below&lt;/p&gt;

&lt;p&gt;For Linux:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="s2"&gt;"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip"&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="s2"&gt;"awscliv2.zip"&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;unzip awscliv2.zip
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; ./aws/install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For windows and macOS you can use the installers below.&lt;br&gt;
Download the installer for your platform and run them.&lt;/p&gt;

&lt;p&gt;Windows: &lt;a href="https://awscli.amazonaws.com/AWSCLIV2.msi"&gt;https://awscli.amazonaws.com/AWSCLIV2.msi&lt;/a&gt;&lt;br&gt;
macOS: &lt;a href="https://awscli.amazonaws.com/AWSCLIV2.pkg"&gt;https://awscli.amazonaws.com/AWSCLIV2.pkg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To confirm that it's installed properly in the command line, issue the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;aws &lt;span class="nt"&gt;--version&lt;/span&gt;
aws-cli/2.7.0 Python/3.9.11 Linux/5.10.16.3-microsoft-standard-WSL2 exe/x86_64.ubuntu.20 prompt/off
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should see a result similar to the above. Well done.&lt;/p&gt;

&lt;p&gt;Next, we need to configure AWS CLI.&lt;/p&gt;

&lt;p&gt;To do so, in the command line enter the command below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;aws configure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will be asked for our &lt;strong&gt;AWS Access Key ID&lt;/strong&gt;, copy the key gotten from creating an IAM user and paste it there, and press enter, we’ll then be asked for a &lt;strong&gt;secret key&lt;/strong&gt;, do the same, but this time copy the hidden secret key.&lt;/p&gt;

&lt;p&gt;For the region and any other prompt, we just press enter leaving them at “None” till we are then back to normal CLI prompt.&lt;/p&gt;

&lt;p&gt;Congratulations, we have successfully configured our AWS CLI. We continue to the next step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2 - Install and Setup Serverless Framework
&lt;/h2&gt;

&lt;p&gt;We are going to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install serverless framework&lt;/li&gt;
&lt;li&gt;Create a serverless boilerplate application&lt;/li&gt;
&lt;li&gt;Deploy and test default bootstrapped API.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is Serverless Framework?
&lt;/h3&gt;

&lt;p&gt;The serverless framework is an open-source NPM package that makes deploying functions to various serverless cloud hosting providers (in our case AWS lambda) easy.&lt;/p&gt;

&lt;p&gt;With it, we can declare functions and the events that trigger those functions, and any other setups we need in a configuration file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install the Serverless Framework
&lt;/h3&gt;

&lt;p&gt;To install the serverless framework, ensure you have node.js installed, then in your command line, enter the command below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; serverless
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command above installed the serverless CLI package globally and we can check that it’s installed properly by issuing the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;serverless &lt;span class="nt"&gt;--version&lt;/span&gt;
Framework Core: 2.72.2
Plugin: 5.5.4
SDK: 4.3.2
Components: 3.18.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a serverless boilerplate application
&lt;/h3&gt;

&lt;p&gt;What that out of the way, to test that everything is wired up properly for us to start writing our functions, we issue the command below in our command line to bootstrap a demo serverless REST API&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;serverless
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We would be asked various questions as shown below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hgIa7qZF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21bkwu9s6h44tq10yxrt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hgIa7qZF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21bkwu9s6h44tq10yxrt.png" alt="Screen" width="470" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select the AWS - Node.js - HTTP API, It’ll as for an app name, and give the app a name, in my case, I used “&lt;strong&gt;serverless-todo-app&lt;/strong&gt;”. It’ll then set up the project.&lt;/p&gt;

&lt;p&gt;We’ll then be asked a few more questions.&lt;/p&gt;

&lt;p&gt;For login/register to the serverless dashboard, for the sake of this project, we select “no”.&lt;/p&gt;

&lt;p&gt;For deploying the project, select “no” for now too&lt;/p&gt;

&lt;p&gt;After that, we are good to go.&lt;/p&gt;

&lt;p&gt;CD into the project folder and open in vscode&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;serverless-todo-app
&lt;span class="nv"&gt;$ &lt;/span&gt;code &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now see the contents of the boilerplate application, specifically, the “&lt;strong&gt;handler.js&lt;/strong&gt;” and “&lt;strong&gt;serverless.yml&lt;/strong&gt;” files. We will look into those later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deploy and test default bootstrapped API
&lt;/h3&gt;

&lt;p&gt;Let's now deploy our demo app to see that it works.&lt;/p&gt;

&lt;p&gt;In the project directory, we issue the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;serverless deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After a few seconds, you will be presented with an &lt;strong&gt;endpoint&lt;/strong&gt; URL similar to the one below, among other information&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;https://53df424gq7.execute-api.us-east-1.amazonaws.com/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We simply go to that URL in our browser, you should see a result similar to the one below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VZdw_WRS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmz7hz3grvinymci988t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VZdw_WRS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmz7hz3grvinymci988t.png" alt="display" width="880" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you see it, it means that our function is working and everything is wired up properly.&lt;/p&gt;

&lt;p&gt;We can go back into AWS and in the top search box, we enter Lambda and click on the result, we should now see that a lambda function has been created for us automatically.&lt;/p&gt;

&lt;p&gt;That's as a result of the deployment we just did.&lt;/p&gt;

&lt;p&gt;We can now begin writing custom functions and persisting our data into a database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3 - Connecting DynamoDB and Creating Lambda Functions
&lt;/h2&gt;

&lt;p&gt;We are going to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provision our database&lt;/li&gt;
&lt;li&gt;Create the serverless functions for our todo API&lt;/li&gt;
&lt;li&gt;Deploy Test our functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We are creating a simple todo API and we need the data to be stored in a database for persistence.&lt;/p&gt;

&lt;p&gt;Here, leverage DynamoDB, a NoSQL database provided by AWS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Provisioning our Database
&lt;/h3&gt;

&lt;p&gt;With the serverless framework, we can define the AWS resources we want to use. We do that in the &lt;strong&gt;serverless.yml&lt;/strong&gt; file provided for us, and the serverless framework will provision that resource for us on AWS, so we don't need to login into the AWS web interface to provision the resource.&lt;/p&gt;

&lt;p&gt;Taking a look at the serverless.yml file inside our project folder, we have the following&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GTAOXfOz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b06gf65xyd4n2nmyzv4p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GTAOXfOz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b06gf65xyd4n2nmyzv4p.png" alt="yml" width="334" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will now add resources, in our case the database we want to use by adding a resources section to the file. &lt;em&gt;Take note of the indentation.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="nn"&gt;...&lt;/span&gt;
&lt;span class="na"&gt;resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;Resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;TodoAPI&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# We set the resource name: here we use TodoAPI as the name&lt;/span&gt;
      &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::DynamoDB::Table&lt;/span&gt; &lt;span class="c1"&gt;# Type of resource we are provisioning, in this case, a DynamoDB Table&lt;/span&gt;
      &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# The properties for the resource&lt;/span&gt;
        &lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;TodoApiTable&lt;/span&gt; &lt;span class="c1"&gt;# Here we set the table name to "TodoApiTable&lt;/span&gt;
        &lt;span class="na"&gt;BillingMode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;PAY_PER_REQUEST&lt;/span&gt; &lt;span class="c1"&gt;# The billing mode is set here&lt;/span&gt;
        &lt;span class="na"&gt;AttributeDefinitions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# Represents an attribute for describing the key schema for the table and indexes.&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;AttributeName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;id&lt;/span&gt; &lt;span class="c1"&gt;# An ID to Identify each record in a unique way&lt;/span&gt;
            &lt;span class="na"&gt;AttributeType&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;S&lt;/span&gt; &lt;span class="c1"&gt;# The attribute is of type String, hence the S&lt;/span&gt;
        &lt;span class="na"&gt;KeySchema&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# Specifies the attributes that make up the primary key for a table or an index&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;AttributeName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;id&lt;/span&gt; &lt;span class="c1"&gt;# The name of the key attribute&lt;/span&gt;
            &lt;span class="na"&gt;KeyType&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;HASH&lt;/span&gt; &lt;span class="c1"&gt;# The ID as the key&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above is what is known as AWS cloud formation.&lt;/p&gt;

&lt;p&gt;Cloud Formation is basically &lt;strong&gt;a method of provisioning AWS infrastructure using code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the code above, we are provisioning an AWS resource to be used by our API, in this case, the DynamoDB.&lt;/p&gt;

&lt;p&gt;We set the name and type of the database “&lt;strong&gt;TodoAPI&lt;/strong&gt;”, and give it several properties.&lt;/p&gt;

&lt;p&gt;We then went on to set a table name, the billing mode, and the attributes for the table.&lt;/p&gt;

&lt;p&gt;Don't worry about the BillingMode as “PAY_PER_REQUEST”. AWS gives 1 million free requests, and we are unlikely to exceed that in this tutorial.&lt;/p&gt;

&lt;p&gt;Finally, for the attributes, we specify that we need a primary key for the table and specify the type of the primary key.&lt;/p&gt;

&lt;p&gt;To test this we just need to save the file and re-deploy the application&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;serverless deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To see that this works, we login to our AWS account, and in the search box, we search for “&lt;strong&gt;DynamoDB&lt;/strong&gt;” click on it in the result, and click on tables on the left navigation section.&lt;/p&gt;

&lt;p&gt;We can see that a table has been created for us automatically bearing our table name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6FSRw6RH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzlfnobwmna613s60e14.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6FSRw6RH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzlfnobwmna613s60e14.png" alt="Table" width="455" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, with most of the setup work out of the way, we can begin to write the functions for our API.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create the Serverless Functions for our todo API
&lt;/h3&gt;

&lt;p&gt;What are serverless functions? they are basically just regular functions that we write, which performs, most of the time, a single task.&lt;/p&gt;

&lt;p&gt;We then deploy or send these functions to cloud providers which then store them and can run/call them through certain events or triggers.&lt;/p&gt;

&lt;p&gt;Let us change the structure of our boilerplate API a bit to make the project more modular.&lt;/p&gt;

&lt;p&gt;Currently, the folder structure looks as seen below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2aFWEbvk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7mdik58y0qg65rugyuw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2aFWEbvk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7mdik58y0qg65rugyuw.png" alt="folders" width="354" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We create a source folder “src” and inside the source folder, we create another folder “handlers”.&lt;/p&gt;

&lt;p&gt;Inside the project directory in your command line, enter the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;src &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;src
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;handlers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then move the handler.js file into the handlers folder and rename it to createTodo.js.&lt;/p&gt;

&lt;p&gt;The folder structure should now look like below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9PQg0llF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txhmacmv0pl6mr985zqf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9PQg0llF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txhmacmv0pl6mr985zqf.png" alt="create" width="358" height="210"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Writing the createTodo Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Currently, in our createTodo.js file, we have the following contents which basically contain the code that runs when we tested our bootstrapped API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Go Serverless v2.0! Your function executed successfully!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What the code above does is respond to a get request and return the request event details as JSON to the browser or client.&lt;/p&gt;

&lt;p&gt;Before we proceed, we need to install a few packages to enable us to build the createTodo function.&lt;/p&gt;

&lt;p&gt;We need two packages, one for generating a unique ID, and the other is AWS SDK for interacting with AWS from our function.&lt;/p&gt;

&lt;p&gt;To install these, issue the following command in your command line, in the project directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm init &lt;span class="c"&gt;# This generates a package.json file for us(Press enter to accept default values) &lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;aws-sdk uuid &lt;span class="c"&gt;# this installs the aws-sdk and uuid packages from npm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that done, we now replace the code in createTodo.js with the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AWS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-sdk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;v4&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;uuid&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createTodo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dynamodb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AWS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DynamoDB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DocumentClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Connect to AWS DynamoDB&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;v4&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createdAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newTodo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;createdAt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Inserts new data (todo) into the DynamoDB&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;dynamodb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;put&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TodoApiTable&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newTodo&lt;/span&gt;
  &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// Returns the newly added todo&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newTodo&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;createTodo&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we first require the needed libraries, then we create a function to actually do the todo creation.&lt;/p&gt;

&lt;p&gt;Inside the function, we first connect to dynamoDB by creating an instance of the connection using the aws-sdk.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dynamodb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AWS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DynamoDB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DocumentClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Connect to AWS DynamoDB&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then get the todo data from the event body. The &lt;strong&gt;event&lt;/strong&gt; basically contains all the information from the request.&lt;/p&gt;

&lt;p&gt;Next, we create an object containing the todo data coming from the event body and attach the genetated &lt;strong&gt;id&lt;/strong&gt; and &lt;strong&gt;createdAt&lt;/strong&gt; properties.&lt;/p&gt;

&lt;p&gt;Next, using the database connection handler “dynamodb” we add a new todo into the “TodoApiTable”&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;  
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;dynamodb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;put&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TodoApiTable&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newTodo&lt;/span&gt;
  &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we return a status code and the body and export the handler for creating todo.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Writing the getTodos Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We create another file “&lt;strong&gt;getTodos.js&lt;/strong&gt;” inside our handlers folder. The getTodos get all the previously saved todo from our database.&lt;/p&gt;

&lt;p&gt;See the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AWS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-sdk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getTodos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dynamodb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AWS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DynamoDB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DocumentClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;dynamodb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TodoApiTable&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Items&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;getTodos&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code is similar to the &lt;strong&gt;createTodo.js&lt;/strong&gt; code.&lt;/p&gt;

&lt;p&gt;We start by importing the required libraries, we then use the &lt;code&gt;AWS sdk&lt;/code&gt; client to get a connection to the database,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dynamodb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AWS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DynamoDB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DocumentClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The major difference from the code for &lt;code&gt;createTodo&lt;/code&gt; is that, in this case, we are searching the database for the saved todo, so we use the dynamoDB scan method, and give it a table name to search from.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;dynamodb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TodoApiTable&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally, we return the todos, with status a status code.&lt;/p&gt;

&lt;p&gt;Now, we need to add our exported handler functions and set permissions so our functions can access dynamoDB.&lt;/p&gt;

&lt;p&gt;We do that in the &lt;code&gt;serverless.yml&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Update it to look like seen below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;serverless-todo-app&lt;/span&gt;
&lt;span class="na"&gt;frameworkVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;||&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;3"&lt;/span&gt;

&lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;aws&lt;/span&gt;
  &lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nodejs12.x&lt;/span&gt;
  &lt;span class="na"&gt;lambdaHashingVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;20201221"&lt;/span&gt;
    &lt;span class="na"&gt;region&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;us-east-1"&lt;/span&gt; &lt;span class="c1"&gt;# Replace the region here with your own&lt;/span&gt;
&lt;span class="c1"&gt;# Set permission for our functions&lt;/span&gt;
  &lt;span class="na"&gt;iamRoleStatements&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;Effect&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Allow&lt;/span&gt;
    &lt;span class="na"&gt;Action&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;dynamodb:*&lt;/span&gt; &lt;span class="c1"&gt;# Perform all database actions (create/update/delete)&lt;/span&gt;
    &lt;span class="na"&gt;Resource&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;arn:aws:dynamodb:us-east-1:301048034781:table/TodoApiTable&lt;/span&gt; &lt;span class="c1"&gt;# Replace with your table unique identefier (ARN). It's found on AWS and check your table details&lt;/span&gt;

&lt;span class="c1"&gt;# Add function handlers&lt;/span&gt;
&lt;span class="na"&gt;functions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;createTodo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;src/handlers/createTodo.handler&lt;/span&gt;
    &lt;span class="na"&gt;events&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;httpApi&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/&lt;/span&gt;
          &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;post&lt;/span&gt;
  &lt;span class="na"&gt;fetchTodos&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;src/handlers/getTodos.handler&lt;/span&gt;
    &lt;span class="na"&gt;events&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;httpApi&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/todos&lt;/span&gt;
          &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;get&lt;/span&gt;

&lt;span class="na"&gt;resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;Resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;TodoAPI&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# We set the resource name: here we use TodoAPI as the name&lt;/span&gt;
      &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS::DynamoDB::Table&lt;/span&gt; &lt;span class="c1"&gt;# Type of resource we are provisioning, in this case, a DynamoDB Table&lt;/span&gt;
      &lt;span class="na"&gt;Properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# The properties for the resource&lt;/span&gt;
        &lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;TodoApiTable&lt;/span&gt; &lt;span class="c1"&gt;# Here we set the table name to "TodoApiTable&lt;/span&gt;
        &lt;span class="na"&gt;BillingMode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;PAY_PER_REQUEST&lt;/span&gt; &lt;span class="c1"&gt;# The billing mode is set here&lt;/span&gt;
        &lt;span class="na"&gt;AttributeDefinitions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# Represents an attribute for describing the key schema for the table and indexes.&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;AttributeName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;id&lt;/span&gt; &lt;span class="c1"&gt;# An ID to Identify each record in a unique way&lt;/span&gt;
            &lt;span class="na"&gt;AttributeType&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;S&lt;/span&gt; &lt;span class="c1"&gt;# The attribute is of type String, hence the S&lt;/span&gt;
        &lt;span class="na"&gt;KeySchema&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# Specifies the attributes that make up the primary key for a table or an index&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;AttributeName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;id&lt;/span&gt; &lt;span class="c1"&gt;# The name of the key attribute&lt;/span&gt;
            &lt;span class="na"&gt;KeyType&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;HASH&lt;/span&gt; &lt;span class="c1"&gt;# The ID as the key&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Deploying and Testing our Functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To deploy our functions it’s as easy as using the command we used previously for deployment.&lt;/p&gt;

&lt;p&gt;Having saved our code, we enter the command below into our CLI to deploy our API&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;serverless deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After a few seconds, we get a link to our endpoint, yours will look a little different.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: I omitted some characters in my endpoint below, cause it’s not safe showing you mine.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;endpoints&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="s"&gt;https://...zfq96rti.execute-api.us-east-1.amazonaws.com&lt;/span&gt;
    &lt;span class="s"&gt;https://...zfq96rti.execute-api.us-east-1.amazonaws.com/todos&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now test our endpoint using postman&lt;/p&gt;

&lt;p&gt;Testing createTodo:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9wuq0PqZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1w335w06kufdeyd5dr9s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9wuq0PqZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1w335w06kufdeyd5dr9s.png" alt="Creating todos" width="880" height="522"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Testing getTodos&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z7Kli_nG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkdazd7uwljp7im2vuiu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z7Kli_nG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkdazd7uwljp7im2vuiu.png" alt="Get todos" width="880" height="618"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;We have successfully created a serverless API with the serverless framework, used Lambda functions, and connected our API to dynamoBD to persist our data.&lt;/p&gt;

&lt;p&gt;If you made it to the end, congratulations.&lt;/p&gt;

&lt;p&gt;Share and drop your comments below.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>serverless</category>
      <category>tutorial</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
