<?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: ckmonish2000</title>
    <description>The latest articles on Forem by ckmonish2000 (@ckmonish2000).</description>
    <link>https://forem.com/ckmonish2000</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%2F793326%2F155bb7a6-ca2d-4de7-9ce8-a5c384cca9f7.jpg</url>
      <title>Forem: ckmonish2000</title>
      <link>https://forem.com/ckmonish2000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ckmonish2000"/>
    <language>en</language>
    <item>
      <title>Terraform 101</title>
      <dc:creator>ckmonish2000</dc:creator>
      <pubDate>Tue, 06 Dec 2022 14:47:20 +0000</pubDate>
      <link>https://forem.com/ckmonish2000/terraform-101-4jj1</link>
      <guid>https://forem.com/ckmonish2000/terraform-101-4jj1</guid>
      <description>&lt;p&gt;Infrastructure as code makes our lives easy by allowing us to deploy our servers by describing the configurations for our infrastructure as code.&lt;/p&gt;

&lt;p&gt;When you're setting up your servers you go through a 3-step process i.e&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Provision your servers: You create your EC2 instances, S3 buckets etc... &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configure your servers: You install docker or some other software that will help you during the application setup.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Application setup: You setup your app in production or test mode.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All the above steps are time consuming processes hence we can automate each of the above step by:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm88apxuwpmovuy940w01.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm88apxuwpmovuy940w01.png" alt="Types-of-iac-tools"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provisioning our infrastructure using tools like terraform or cloud formation.&lt;/li&gt;
&lt;li&gt;Managing Configurations using ansible or puppet&lt;/li&gt;
&lt;li&gt;Finally containerizing your application to make sure it runs the same everywhere with docker or vagrant&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this post we'll learn about basics of provisioning tool called terraform.&lt;/p&gt;

&lt;p&gt;Terraform is an infrastructure provisioning tool similar to AWS's cloud formation or the serverless framework which allow you to manage resources via code.&lt;/p&gt;

&lt;h1&gt;
  
  
  Install Terraform
&lt;/h1&gt;

&lt;p&gt;You can setup terraform by downloading a binary file and adding it to your system PATH from &lt;a href="https://developer.hashicorp.com/terraform/downloads" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Concepts
&lt;/h2&gt;

&lt;p&gt;Terraform has its own language called &lt;code&gt;HCL&lt;/code&gt; (Hashicorp configuration language) through which you have describe your infrastructure.&lt;/p&gt;

&lt;p&gt;you define all your configs in files that have a &lt;code&gt;.tf&lt;/code&gt; extension.&lt;/p&gt;

&lt;p&gt;Terraform supports various cloud providers like AWS, GCP and much more in order to manage infrastructure on these platforms terraform has plugins.&lt;/p&gt;

&lt;p&gt;plugins are of 3-types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Official&lt;/code&gt; - These are plugins created and maintained by Hashicorp eg: AWS, Azure, GCP and etc....&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Verified&lt;/code&gt; - These are plugins created and maintained by Hashicorp's partners like Linode, Digital Ocean and etc....&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Community&lt;/code&gt; - These are plugins built and maintained by the community.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;you can learn more about plugins from &lt;a href="https://registry.terraform.io/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic commands
&lt;/h3&gt;

&lt;p&gt;Once you define your config you can create your resources using 3 basic commands in a sequence:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;terraform init&lt;/code&gt;: This command looks at your config and downloads all the plugins that's required to create and manage your resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;terraform plan&lt;/code&gt;: Before actually creating your resources, you can review the changes that will be made to your infrastructure using this command.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;terraform apply&lt;/code&gt;: finally running the apply command will create your resources &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Creating your first resource with terraform
&lt;/h1&gt;

&lt;p&gt;The first resource we'll be creating will be a file.&lt;/p&gt;

&lt;p&gt;So, first create a file with a &lt;code&gt;.tf&lt;/code&gt; extension and add the code down below.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fey5wbwgxtqq9vx82qtwg.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fey5wbwgxtqq9vx82qtwg.png" alt="terraform local file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In terraform everything is defined as a block and the above image we are using something called a &lt;code&gt;resource&lt;/code&gt; block the resource block takes 2 arguments first is the &lt;code&gt;resource type&lt;/code&gt; and second is a logical name for your resource.&lt;/p&gt;

&lt;p&gt;In the resource type argument the keyword &lt;code&gt;before the _&lt;/code&gt; indicates the provider we are using in this case it's an official provider from hashicorp called local and the keyword &lt;code&gt;after the _&lt;/code&gt; indicates the resource provided by the provider.&lt;/p&gt;

&lt;p&gt;another example can be &lt;code&gt;aws_s3_bucket&lt;/code&gt; where &lt;code&gt;aws&lt;/code&gt; is the provider and &lt;code&gt;s3_bucket&lt;/code&gt; symbolizes the S3 feature provided by aws.&lt;/p&gt;

&lt;p&gt;Each block expects you to enter a few key value pairs to function,&lt;br&gt;
Here the resource block's key value pair will change depending on the provider and resources being used.&lt;/p&gt;

&lt;p&gt;In the above image the &lt;code&gt;local_file&lt;/code&gt; provider expects us to pass the &lt;code&gt;filename&lt;/code&gt; key which will create a file in the provided path and the content key specifies what content will be added to the newly created file.&lt;/p&gt;

&lt;p&gt;No if you run terraform &lt;code&gt;init&lt;/code&gt;, &lt;code&gt;plan&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt; you'll see a new file created with the name &lt;code&gt;pet.txt&lt;/code&gt; with the content &lt;code&gt;hello pets&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Using variables
&lt;/h1&gt;

&lt;p&gt;Terraform allows us to define variables and use them in our config file.&lt;/p&gt;

&lt;p&gt;you can learn about the variable types supported by terraform &lt;a href="https://developer.hashicorp.com/terraform/language/values/variables" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you can create a variable by starting the block with the &lt;code&gt;variable&lt;/code&gt; keyword,&lt;/p&gt;

&lt;p&gt;Each keyword block expects you to enter a default value and if required you can enter a variable type.&lt;/p&gt;

&lt;p&gt;if you do not enter a default value terraform will require you to enter a default value when you run the apply command.&lt;/p&gt;

&lt;p&gt;you can also set a default value by passing the value while running the apply command using the &lt;code&gt;-var&lt;/code&gt; flag or you can initialize your variables using a &lt;code&gt;.tfvar&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;You can also export the value for variables as an environment variable.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gdhqoe2q9xshsg81e98.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gdhqoe2q9xshsg81e98.png" alt="simple terraform variable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you can use the variable in your config like this &lt;code&gt;var.test&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;here's a more complex variable for your reference&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9gfqv5tpmzrl2prdhvd.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9gfqv5tpmzrl2prdhvd.png" alt="complex terraform variable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;let's access the name of the dog and print it in the console.&lt;/p&gt;

&lt;p&gt;You can print values from terraform using the &lt;code&gt;output&lt;/code&gt; block and the output block expects you to provide it a value to print.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjv75c96qby1tdh454qc9.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjv75c96qby1tdh454qc9.png" alt="output terraform"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now when you run the &lt;code&gt;terraform apply&lt;/code&gt; command you will see the output in your console.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7sttewsy7il0g017g11u.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7sttewsy7il0g017g11u.png" alt="terraform console"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Resource Dependency
&lt;/h1&gt;

&lt;p&gt;There are times where we might want to supply the output of one resource as an input to another resource or we might want a resource B to be created after the creation of resource A we can achieve this with resource dependency.&lt;/p&gt;

&lt;p&gt;There are 2 types &lt;code&gt;implicit&lt;/code&gt; and &lt;code&gt;explicit&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Implicit Resource Dependency
&lt;/h2&gt;

&lt;p&gt;We can use the output of one resource to be the input of another by referencing the resource using the &lt;code&gt;resource type&lt;/code&gt; followed by the resource's &lt;code&gt;logical name&lt;/code&gt; and one of the property that's being returned by the resource.&lt;/p&gt;

&lt;p&gt;here in our case, we are using the random provider and resource pet that returns and id.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fetoj3bkn7bjqw8t8xurb.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fetoj3bkn7bjqw8t8xurb.png" alt="Implicit Resource Dependency "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now run apply the changes and see what happens.&lt;/p&gt;
&lt;h2&gt;
  
  
  Explicit Resource Dependency
&lt;/h2&gt;

&lt;p&gt;using references will make your resource implicitly dependent but if you want to explicitly make a resource dependent, we can use the &lt;code&gt;depends_on&lt;/code&gt; key in the &lt;code&gt;resource&lt;/code&gt; block like in the image below&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmpl0nlnhu043rder3wrm.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmpl0nlnhu043rder3wrm.png" alt="Explicit Resource Dependency"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now if you have a look at the console the resource &lt;code&gt;time&lt;/code&gt; is created first following the &lt;code&gt;petz&lt;/code&gt; resource.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3lbxvlhmsy2oui8ckhao.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3lbxvlhmsy2oui8ckhao.png" alt="res"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Terraform Lifecycle
&lt;/h2&gt;

&lt;p&gt;Generally, in terraform most of the time when you make a change the resource block the resource is completely destroyed and recreated.&lt;/p&gt;

&lt;p&gt;For example, say that you change the name of the file the file will first be deleted then a new copy will be created but there are times where you might want the old file to be deleted only when the new one is up and running you can achieve that using the lifecycle block.&lt;/p&gt;

&lt;p&gt;there are 3 types &lt;code&gt;create_before_destroy&lt;/code&gt;, &lt;code&gt;prevent_destroy&lt;/code&gt; and &lt;code&gt;ignore_changes&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;create_before_destroy&lt;/code&gt;: when set to true will first create the new resource and then delete the old one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "azurerm_resource_group" "example" {
  # ...

  lifecycle {
    create_before_destroy = true
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;prevent_destroy&lt;/code&gt;: when set to true will cause Terraform to reject with an error any plan that would destroy the infrastructure.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ignore_changes&lt;/code&gt;: Accepts a list of attributes it should ignore when changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_instance" "example" {
  lifecycle {
    ignore_changes = [
      tags,
    ]
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are the basic concepts of terraform which will help you provision and manage your resources.&lt;/p&gt;

&lt;p&gt;If you are looking for resources on how to use terraform with cloud providers stay tuned will soon publish how to setup your linode and digital ocean instances using terraform.&lt;/p&gt;

&lt;p&gt;Also checkout &lt;a href="https://spacelift.io/blog/terraform-tutorial" rel="noopener noreferrer"&gt;Getting Started With Terraform on AWS&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for the read.&lt;/p&gt;

</description>
      <category>iac</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Build a Todo list with AWS Lambda and DynamoDB</title>
      <dc:creator>ckmonish2000</dc:creator>
      <pubDate>Tue, 29 Nov 2022 09:40:12 +0000</pubDate>
      <link>https://forem.com/ckmonish2000/build-crud-api-with-aws-lambda-and-dynamodb-clj</link>
      <guid>https://forem.com/ckmonish2000/build-crud-api-with-aws-lambda-and-dynamodb-clj</guid>
      <description>&lt;p&gt;In this post we will be building simple CRUD API to manage tasks and in the upcoming posts we will use this project as a base to explore other AWS services.&lt;/p&gt;

&lt;p&gt;But in order to follow along with this post you need to have a basic understanding of &lt;a href="https://dev.to/ckmonish2000/intro-to-dynamodb-with-node-keb"&gt;DynamoDB&lt;/a&gt; and how to setup your &lt;a href="https://dev.to/ckmonish2000/intro-to-serverless-framework-iac-101-471i"&gt;lambda function&lt;/a&gt; and &lt;a href="https://dev.to/ckmonish2000/how-to-setup-dynamodb-with-serverless-framework-3c95"&gt;DynamoDB database&lt;/a&gt; using the serverless framework.&lt;/p&gt;

&lt;p&gt;So, let's begin&lt;/p&gt;

&lt;h2&gt;
  
  
  Project structure
&lt;/h2&gt;

&lt;p&gt;The code is structured using a service-oriented approach where we basically follow a 3-layer architecture.&lt;/p&gt;

&lt;p&gt;The main advantage of using the 3-layer-architecture is that it creates a level of abstraction and allows you to manage your code base more efficiently.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvhvzppwzm40mykfqwl8r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvhvzppwzm40mykfqwl8r.png" alt="service-oriented architecture" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, create a folder called &lt;code&gt;src&lt;/code&gt; and create 3 files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;functions.js&lt;/code&gt; - which basically is your controller&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;io.js&lt;/code&gt; - which will be your service layer&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;model.js&lt;/code&gt; - which will be your data access layer&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Tips
&lt;/h2&gt;

&lt;p&gt;Before we start just want to drop in few serverless commands that will speed up your deployment and debugging process.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;serverless deploy function --function functionName&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;serverless logs -f functionName -t&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first command allows you to only deploy a single function rather than your entire infrastructure.&lt;/p&gt;

&lt;p&gt;The second command allows you to view the logs for a given function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Database operations
&lt;/h2&gt;

&lt;p&gt;Now in the &lt;code&gt;model.js&lt;/code&gt; file we will define all the operations we will be performing on the todo table.&lt;/p&gt;

&lt;p&gt;In the code below I'm creating a class called Todo which will contain the respective methods &lt;code&gt;fetchAll&lt;/code&gt; &lt;code&gt;fetchById&lt;/code&gt; &lt;code&gt;put&lt;/code&gt; &lt;code&gt;deleteById&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can follow the &lt;a href="https://dev.to/ckmonish2000/intro-to-dynamodb-with-node-keb"&gt;DynamoDB 101&lt;/a&gt; post to understand how it works&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const aws = require('aws-sdk');

class Todo {
  client = new aws.DynamoDB.DocumentClient();
  TableName = "todoTable"

  async put(Item) {
    const params = {
      TableName: this.TableName,
      Item
    }
    const result = await this.client.put(params).promise()

    return true
  }


  async fetchById(key) {
    const params = {
      TableName: this.TableName,
      Key: {
        userId: key
      }
    }
    const result = await this.client.get(params).promise()
    return result.Item
  }

  async fetchAll(key) {
    const params = {
      TableName: this.TableName,
    }
    const result = await this.client.scan(params).promise()
    return result.Items
  }

  async deleteById(key) {
    const params = {
      TableName: this.TableName,
      Key: {
        userId: key
      }
    }
    const result = await this.client.delete(params).promise()
    return true
  }
}

module.exports = new Todo();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Service layer
&lt;/h2&gt;

&lt;p&gt;This layer will basically encapsulate your business logic since this is a basic todo app we'll just extract the body and rout parameter from the request and return it.&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 = {
  input: (event) =&amp;gt; JSON.parse(event.body),
  params: (event) =&amp;gt; event.pathParameters
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Controller
&lt;/h2&gt;

&lt;p&gt;This section of the project will contain all your function logic that we'll link in the &lt;code&gt;serverless.yml&lt;/code&gt; file in a sec.&lt;/p&gt;

&lt;p&gt;if you're not sure how to write your functions checkout the post on &lt;a href="https://dev.to/ckmonish2000/serverless-functions-for-dummies-2aae"&gt;serverless functions for dummies&lt;/a&gt;&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';
const io = require('./io')
const Todo = require('./model')

const getAllTasks = async (event) =&amp;gt; {
  const data = await Todo.fetchAll();
  return data
};


const taskById = async (event) =&amp;gt; {
  const { id } = io.params(event)
  const data = await Todo.fetchById(id);
  return data
}

const createTask = async (event) =&amp;gt; {
  const body = io.input(event);
  const data = await Todo.put(body)
  return JSON.stringify({ message: "created task" })
}

const updateTask = async (event) =&amp;gt; {
  const body = io.input(event);
  const data = await Todo.put(body)

  return JSON.stringify({ message: "updated task" })
}

const deleteTask = async (event) =&amp;gt; {
  const { id } = io.params(event)
  const data = await Todo.deleteById(id);
  return JSON.stringify({ message: "deleted task with id = " + id })
}


module.exports = {
  getAllTasks,
  taskById,
  createTask,
  updateTask,
  deleteTask
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Serverless config
&lt;/h2&gt;

&lt;p&gt;Try to setup the serverless configs on your own, the below code is just for your reference.&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';
const io = require('./io')
const Todo = require('./model')

const getAllTasks = async (event) =&amp;gt; {
  const data = await Todo.fetchAll();
  return data
};


const taskById = async (event) =&amp;gt; {
  const { id } = io.params(event)
  const data = await Todo.fetchById(id);
  return data
}

const createTask = async (event) =&amp;gt; {
  const body = io.input(event);
  const data = await Todo.put(body)
  return JSON.stringify({ message: "created task" })
}

const updateTask = async (event) =&amp;gt; {
  const body = io.input(event);
  const data = await Todo.put(body)

  return JSON.stringify({ message: "updated task" })
}

const deleteTask = async (event) =&amp;gt; {
  const { id } = io.params(event)
  const data = await Todo.deleteById(id);
  return JSON.stringify({ message: "deleted task with id = " + id })
}


module.exports = {
  getAllTasks,
  taskById,
  createTask,
  updateTask,
  deleteTask
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now run &lt;code&gt;serverless deploy&lt;/code&gt; and try out your apis using postman.&lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
    <item>
      <title>How to setup DynamoDB with serverless framework for dummies</title>
      <dc:creator>ckmonish2000</dc:creator>
      <pubDate>Thu, 24 Nov 2022 18:23:16 +0000</pubDate>
      <link>https://forem.com/ckmonish2000/how-to-setup-dynamodb-with-serverless-framework-3c95</link>
      <guid>https://forem.com/ckmonish2000/how-to-setup-dynamodb-with-serverless-framework-3c95</guid>
      <description>&lt;p&gt;In this post you will learn how to setup a DynamoDB table with infrastructure as code using the serverless framework which will act as a base for the future posts.&lt;/p&gt;

&lt;p&gt;let's begin&lt;/p&gt;

&lt;h2&gt;
  
  
  Project set up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;first checkout to your project directory and run the below command create a serverless project.
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;p&gt;now you should have a simple boilerplate with a serverless.yml and handler which allows us to setup everything from the API gateway to DynamoDB by ourselves.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Adding an API Gateway to your handler
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Under the handler property add another key called events which accepts an array of events you can learn more about events &lt;a href="https://www.serverless.com/framework/docs/providers/aws/guide/events"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;we are going to use the http api event to trigger the function so add httpApi as the first event and under httpApi add 2 more properties one called &lt;code&gt;method&lt;/code&gt; and another called &lt;code&gt;path&lt;/code&gt; where method is the request method Type and path specifies your endpoint.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;now if you run &lt;code&gt;sls deploy -v&lt;/code&gt; a lambda function with an api gateway must be created.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting up DynamoDB
&lt;/h2&gt;

&lt;p&gt;There is something called AWS cloudformation which is an IAC solution provided by AWS which allows you to setup and manage your AWS services, &lt;/p&gt;

&lt;p&gt;The serverless framework follows the cloudformation template to create and manage services and that's the reason you won't find a detailed docs for setting up AWS resources on the serverless's website.&lt;/p&gt;

&lt;p&gt;So, let's begin by creating out DynamoDB table:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At the end of your &lt;code&gt;serverless.yml&lt;/code&gt; file add the properties down below, this is where you will define all the AWS resources you are going to use in your serverless project.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resources:
  Resources:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now under &lt;code&gt;Resources&lt;/code&gt; define a new property give this property a name of your choice (This is like a service name for your resources)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resources:
  Resources:
    TodoTable:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now just remember the key value pair data below this property will change depending on the AWS resource you are using, but all your services will have one property in common called &lt;code&gt;Type&lt;/code&gt; where you will have to tell serverless which service from AWS you want to create here we are creating a &lt;code&gt;DynamoDB table&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resources:
  Resources:
    TodoTable:
      Type: AWS::DynamoDB::Table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;if you have followed &lt;a href="https://dev.to/ckmonish2000/intro-to-dynamodb-with-node-keb"&gt;my previous post&lt;/a&gt; you would know that we have to define the primary key for every table and that's we are doing under &lt;code&gt;AttributeDefinitions&lt;/code&gt; and &lt;code&gt;KeySchema&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;AttributeDefinitions&lt;/code&gt; is used to define the column by giving it a name and datatype here we are giving it a name of userId and type of string&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;KeySchema&lt;/code&gt; is used to tell which column we are going to use as a primary key and define the type as &lt;code&gt;hash&lt;/code&gt; or &lt;code&gt;range&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resources:
  Resources:
    TodoTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
          - AttributeName: userId
            AttributeType: S
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Finally, we are going to add las 2 properties called &lt;code&gt;TableName&lt;/code&gt; and &lt;code&gt;BillingMode&lt;/code&gt; where both are self-explanatory.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resources:
  Resources:
    TodoTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
          - AttributeName: userId
            AttributeType: S
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
        BillingMode: PAY_PER_REQUEST
        TableName: ${self:custom.TableName}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;now if you run the deploy script it will create you an DynamoDB table.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Adding IAM role
&lt;/h2&gt;

&lt;p&gt;If you remember from a &lt;a href="https://dev.to/ckmonish2000/lambda-function-with-dynamodb-node-36i6"&gt;previous post&lt;/a&gt; in the series your functions don't have permission to access DynamoDB, so we need to create a IAM role and assign it to your function, we also automate that task using the serverless framework.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We need to add all the IAM related properties under the provider block of your &lt;code&gt;serverless.yml&lt;/code&gt; file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the following line under the provider block, and under statements you are going to define all the permissions your IAM user is going to have.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;iam:
    role:
      statements:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add a new property under &lt;code&gt;statements&lt;/code&gt; called &lt;code&gt;Effects&lt;/code&gt; it's an array element you can have multiple effects and under effects define a new key called actions which accepts an array here you will define all the actions your IAM user can perform.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;iam:
    role:
      statements:
        - Effect: 'Allow'
          Action:        
            - dynamodb:Query
            - dynamodb:Scan
            - dynamodb:GetItem
            - dynamodb:PutItem
            - dynamodb:UpdateItem
            - dynamodb:DeleteItem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Finally, you have to specify which resource will your function have access to under the &lt;code&gt;Resource&lt;/code&gt; property &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here you have 2 options &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Directly copy paste you table ARN here&lt;/li&gt;
&lt;/ol&gt;

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

&lt;ol&gt;
&lt;li&gt;Or you can follow the method mentioned below where you will pass the Table Service name you have created under resources as the first element in the array and serverless framework will automatically fetch the ARN for your specified resource.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;provider:
  name: aws
  runtime: nodejs12.x
  region: us-east-1
  iam:
    role:
      statements:
        - Effect: 'Allow'
          Action:        
            - dynamodb:Query
            - dynamodb:Scan
            - dynamodb:GetItem
            - dynamodb:PutItem
            - dynamodb:UpdateItem
            - dynamodb:DeleteItem
          Resource:
            - Fn::GetAtt: [ TodoTable, Arn]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now if you run the deploy command, you'll see a new IAM user has been created and now your function will be able to access the DynamoDB table created.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stay tuned in the next post we will build our first CRUD app and use it's as a base to explore other AWS services.&lt;/p&gt;

</description>
      <category>node</category>
      <category>serverless</category>
      <category>aws</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Intro to Infrastructure as code (Serverless framework)</title>
      <dc:creator>ckmonish2000</dc:creator>
      <pubDate>Fri, 18 Nov 2022 14:16:05 +0000</pubDate>
      <link>https://forem.com/ckmonish2000/intro-to-serverless-framework-iac-101-471i</link>
      <guid>https://forem.com/ckmonish2000/intro-to-serverless-framework-iac-101-471i</guid>
      <description>&lt;p&gt;If you have followed the previous posts about creating lambda functions, you would have realized that it's a pain in the ass to configure the infrastructure manually using the AWS console, you always wonder if there's an easy way of setting up your infrastructure and honestly there is, and it's called infrastructure as code.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is infrastructure as code?
&lt;/h2&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjj61dv63oh5f2yrf1py3.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjj61dv63oh5f2yrf1py3.png" alt="IAC concept"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let me give you an example to understand infrastructure as code &lt;/p&gt;

&lt;p&gt;If you have used an ORM (like prisma, typeORM or DjangoORM) in your project to connect to your database, you know that it generally is a twostep process &lt;/p&gt;

&lt;p&gt;where you first describe how your tables are going to look and then you run a migration which converts your code into SQL tables.&lt;/p&gt;

&lt;p&gt;Similarly in infrastructure as code you describe your infrastructure in something called a YAML file (similar to JSON) and then you run a migration script which will setup your infrastructure according to the description provided.&lt;/p&gt;

&lt;p&gt;In this post we are going to use a framework called serverless which specifically is used for you guessed it serverless infrastructure setup on various platforms like AWS, Cloudflare and etc... &lt;/p&gt;

&lt;p&gt;But if you're interested in diving deep checkout Terraform or Ansible.&lt;/p&gt;

&lt;p&gt;So, let's start&lt;/p&gt;

&lt;h2&gt;
  
  
  Install AWS CLI
&lt;/h2&gt;

&lt;p&gt;Serverless framework uses the aws cli to perform migrations so follow the below steps to setup AWS-CLI on windows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First, download and install the &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html" rel="noopener noreferrer"&gt;AWS-CLI&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now check if the AWS-CLI was installed properly by running the following command in your terminal &lt;code&gt;aws&lt;/code&gt; and you should get a output like the image below.&lt;/p&gt;&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4em4yesr4983nin5pehq.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4em4yesr4983nin5pehq.png" alt="Aws-cli"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Add new IAM user
&lt;/h2&gt;

&lt;p&gt;This is the only manual step to start with, The serverless framework is going to use the aws cli to execute tasks on your aws account so you need to provision your aws-cli with the creds to access your aws account&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search for IAM in the search bar and select users.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft86hklsz0x5o9v4edmvc.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft86hklsz0x5o9v4edmvc.png" alt="Add Iam user"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select add users &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe91lwechasnz0w5n2obo.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe91lwechasnz0w5n2obo.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;give the user a name and provide programmatic access as we will be creating resources through code and hit next.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frl3fm01egeps2w0mkcq8.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frl3fm01egeps2w0mkcq8.png" alt="IAM user permission"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For the purpose of this post provide the user administrative access but do not do it in a live project and hit next and create the user.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fktxzzrir85067byoxcpt.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fktxzzrir85067byoxcpt.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now you must see access code and secret key which we will just use in a second and make sure you don't share this with anyone.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1alnhfssx7s4km5tmlh.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1alnhfssx7s4km5tmlh.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now in the terminal run the command below and it will ask for your access key enter that followed by the secret key and hit enter until the process exits.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws configure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setup serverless framework
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;First, we have to install the serverless framework globally by running
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;now that you have installed the serverless framework checkout to the location of your choice.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setup your lambda function with code
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;serverless&lt;/code&gt; in the terminal you'll see that it gives you a variety of options to choose from choose AWS - Node.js - HTTP API and hit enter and select a project name and hit enter.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7z3mjc5cwf5497acho7s.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7z3mjc5cwf5497acho7s.png" alt="project templates serverless"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;select no for all the following options and continue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;now cd into your serverless project directory and you should see the following:&lt;/p&gt;&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmqd7c2y329o0cy8w208.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmqd7c2y329o0cy8w208.png" alt="serverless framework folder dir"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are 2 main files which needs to be explained&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;handler.js&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;serverless.yml&lt;/code&gt; file.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;if you first open the handler.js file you'll notice that it's the code for your lambda function.&lt;/p&gt;

&lt;p&gt;and the serverless.yml file is where you describe your infrastructure let me explain the instructions line by line.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fddcvc3ypzynytnlk5tyv.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fddcvc3ypzynytnlk5tyv.png" alt="serverless iac code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;YAML file store key value pair data just like JSON.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;code&gt;service&lt;/code&gt; key we pass in the service name that will be displayed on your aws lambda console like in the image below.
(FYI the naming in the console has 3 properties service name, stage name and the function name all separated by "-".)&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmlptzj9pa611fui9jw5q.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmlptzj9pa611fui9jw5q.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;frameworkVersion&lt;/code&gt; specifies which version of serverless framework we are using.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;provider&lt;/code&gt; key is used to configure your cloud provider &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt; here specifies which cloud provider you're using.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;runtime&lt;/code&gt; specifies which programming language you're using.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;(&lt;strong&gt;FYI:&lt;/strong&gt; the provider block might look different for other cloud providers you look it up &lt;a href="https://www.serverless.com/framework/docs/providers" rel="noopener noreferrer"&gt;here&lt;/a&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Under the &lt;code&gt;functions&lt;/code&gt; block you are telling serverless framework where to find your functions and you're linking an API gateway to your function.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;handler&lt;/code&gt; tells you where to find your function&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;events&lt;/code&gt; accepts an array of values where you can specify how you want to trigger the function call, here we are using the &lt;code&gt;httpApi&lt;/code&gt; (API Gateway) to trigger the function which in turn asks you to specify the API path and method to invoke the function.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's deploy the function
&lt;/h2&gt;

&lt;p&gt;In order to deploy this function all, you have to do is run the command down below.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;now if you go to your lambda console you should see that your function 's deployed.&lt;/p&gt;

&lt;p&gt;Will share how to create a DynamoDB and S3 instance in the next post.&lt;/p&gt;

&lt;p&gt;Thanks for your time.&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>aws</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>DynamoDB 101</title>
      <dc:creator>ckmonish2000</dc:creator>
      <pubDate>Tue, 15 Nov 2022 17:04:03 +0000</pubDate>
      <link>https://forem.com/ckmonish2000/intro-to-dynamodb-with-node-keb</link>
      <guid>https://forem.com/ckmonish2000/intro-to-dynamodb-with-node-keb</guid>
      <description>&lt;p&gt;This post is written considering you're a fresher to DynamoDB and it only explains the basic concepts that's required to perform CRUD operations. &lt;/p&gt;

&lt;p&gt;The language used in this post in node.js but the underlying concepts are same no matter which language you choose.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is DynamoDB?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;DynamoDB is a fully managed No-SQL database as a service provided by AWS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DynamoDB is super-fast where you get responses in single digit seconds.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It's infinitely scaleable on demand &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The data values in DynamoDB are stored on an SSD which makes it fast, reliable and extremely secure.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How is the Data Stored?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The items in DynamoDB are stored in tables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each item in the table contains 2 things a key (which is again of 2 type) and a value which are in the document format.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A table can store infinite number of items, but each item must not exceed the size of 400kb.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your tables have 2 types of keys a primary key which is mandatory (Think of it like the primary key in SQL) and then we have a sort key which is optional (think of it as the foreign key in SQL).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you have a primary key alone then each value must be unique but if you're pairing it with a sort key the combination of your primary and sort key must be unique (this helps you filter values efficiently).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setup DynamoDB
&lt;/h2&gt;

&lt;p&gt;Please follow the &lt;a href="https://dev.to/ckmonish2000/lambda-function-with-dynamodb-node-36i6"&gt;previous post&lt;/a&gt; to setup IAM user with the required.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search for DynamoDB in your AWS console and click on create table.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;To follow along create a table name Todo with the Partition key called ID and hit create table&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Server Setup
&lt;/h2&gt;

&lt;p&gt;Now let's setup our node environment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;first create a package.json by running &lt;code&gt;yarn init -y&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;add &lt;code&gt;"type": "module"&lt;/code&gt; in package.json&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BRTYxkVN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8w664466emr4nosr7xs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BRTYxkVN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8w664466emr4nosr7xs.png" alt="module support npm" width="273" height="119"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;now install the dependencies
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add express dotenv aws-sdk nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;now add the scripts section in package.json
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "start": "nodemon index"
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;create 3 files in your project directory index.js,Database.js and .env.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In .env add the your IAM user secret, access key and your AWS account region.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VzvA1JXd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/37000uj9tvbodaig84wr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VzvA1JXd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/37000uj9tvbodaig84wr.png" alt="Image description" width="848" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now setup your express server &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DCn8zr91--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7w7l91lx4kfo92n75a3y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DCn8zr91--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7w7l91lx4kfo92n75a3y.png" alt="express.js server setup" width="880" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go into the Database.js file and let setup our DynamoDB client which allows us to interact with our tables.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;the &lt;code&gt;aws.config.update&lt;/code&gt; is used to authorize the aws-sdk to access your tables in DynamoDB.&lt;/p&gt;

&lt;p&gt;the &lt;code&gt;const client = new aws.DynamoDB.DocumentClient();&lt;/code&gt; is used to initialize the DynamoDB client.&lt;/p&gt;

&lt;p&gt;finally, the constant Table_Names is used to define the DynamoDB table name we just created.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;now create a variable called DB and export it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let DB = {};

export default DB;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CRUD Operations
&lt;/h2&gt;

&lt;p&gt;Before we start coding let's understand few methods provided by the DynamoDB client.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;put&lt;/code&gt; this method is used to create a new entry in the DB or edit and existing entry.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;scan&lt;/code&gt; this method is used to all the entries of a given table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;get&lt;/code&gt; this method is used to get an element by their partition key or a combination partition and sort key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;delete&lt;/code&gt; this method deletes a given element.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the above methods use a callback in order to convert it into a promise we chain a method called &lt;code&gt;promise&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In all the above methods accept an object as an argument where we have to pass in the TableName attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const params = {
      TableName: Table_Names,
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Add item to table&lt;/u&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;addItem: async (item) =&amp;gt; {
    const params = {
      TableName: Table_Names,
      Item: item,
    }
    try {
      return await client.put(params).promise()
    } catch (err) {
      return (err)
    }
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In the above code the item parameter can be any JSON object of your choice.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Get all items from the table&lt;/u&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getAllItems: async () =&amp;gt; {
    const params = {
      TableName: Table_Names,
    }

    try {
      return await client.scan(params).promise()
    } catch (err) {
      return (err)
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;Get item by id&lt;/u&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getMemberByID: async (id) =&amp;gt; {
    const params = {
      TableName: Table_Names,
      Key: {
        ID: id,
      }
    }
    try {
      return await client.get(params).promise()
    } catch (err) {
      return (err)
    }
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the above function the parameter id is a string which needs to be the ID of the item you want to fetch.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We use the Key attribute to match the Partition key ID with the parameter id (if you have a sort key that also goes here).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Delete item by id&lt;/u&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;deleteItem: async (id) =&amp;gt; {
    const params = {
      TableName: Table_Names,
      Key: {
        ID: id
      }
    }
    try {
      return await client.delete(params).promise()
    } catch (err) {
      return (err)
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;finally you database.js should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import aws from "aws-sdk";

aws.config.update({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: process.env.AWS_REGION
})

const client = new aws.DynamoDB.DocumentClient();
const Table_Names = "Todo"

const DB = {
  addItem: async (item) =&amp;gt; {
    const params = {
      TableName: Table_Names,
      Item: item,
    }
    try {
      return await client.put(params).promise()
    } catch (err) {
      return (err)
    }
  },
  getMemberByID: async (id) =&amp;gt; {
    const params = {
      TableName: Table_Names,
      Key: {
        ID: id,
      }
    }
    try {
      return await client.get(params).promise()
    } catch (err) {
      return (err)
    }
  },
  deleteItem: async (id) =&amp;gt; {
    const params = {
      TableName: Table_Names,
      Key: {
        ID: id
      }
    }
    try {
      return await client.delete(params).promise()
    } catch (err) {
      return (err)
    }
  },
  getAllItems: async () =&amp;gt; {
    const params = {
      TableName: Table_Names,
    }

    try {
      return await client.scan(params).promise()
    } catch (err) {
      return (err)
    }
  }
}


export default DB;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create routes
&lt;/h2&gt;

&lt;p&gt;go to your index.js file and create routes like down below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import express from "express";
import env from "dotenv";
import DB from "./Database.js"

env.config();
const app = express();

app.use(express.json())

app.get("/", async (req, res) =&amp;gt; {
  const data = await DB.getAllItems();
  res.json(data)
})

app.post("/", async (req, res) =&amp;gt; {
  const data = await DB.addItem(req.body)
  res.json(data)
})

app.get("/:id", async (req, res) =&amp;gt; {
  const data = await DB.getMemberByID(req.params.id)
  res.json(data)
})

app.delete("/:id", async (req, res) =&amp;gt; {
  const data = await DB.deleteItem(req.params.id)
  res.json(data)
})

app.listen(3000, () =&amp;gt; {
  console.log("listening on 3000")
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can test the apis using Postman and they should workfine.&lt;/p&gt;

&lt;p&gt;I will try to cover more topics related to DynamoDB while building projects with lambda and DynamoDB.&lt;/p&gt;

&lt;p&gt;Thanks for time.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>serverless</category>
      <category>node</category>
      <category>database</category>
    </item>
    <item>
      <title>Lambda functions with DynamoDB &amp; Node</title>
      <dc:creator>ckmonish2000</dc:creator>
      <pubDate>Sat, 12 Nov 2022 15:51:13 +0000</pubDate>
      <link>https://forem.com/ckmonish2000/lambda-function-with-dynamodb-node-36i6</link>
      <guid>https://forem.com/ckmonish2000/lambda-function-with-dynamodb-node-36i6</guid>
      <description>&lt;p&gt;In this post let's use the API we previously created and create and fetch data from a database provided by AWS called DynamoDB.&lt;/p&gt;

&lt;p&gt;DynamoDB is a fully managed proprietary NoSQL database service that supports key–value and document data structures.&lt;/p&gt;

&lt;p&gt;So, to being with create another API with request type POST called createStudent and you can use the body property to extract request body from the event parameter in the lambda function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a DynamoDB table
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Search for DynamoDB in the search bar and click on create table.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Now enter the table name and enter the name of your primary key column [Note: If you enter a name for the sortkey then the combination of primary and sortkey will have to be unique].&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Select default settings under table settings and hit the create table button.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Now you should see that the table is created, and you can feed in some data.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  fetching data from DynamoDB in our function
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Let's replace the code from the previous code like this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const aws = require("aws-sdk")
const dynamo = new aws.DynamoDB.DocumentClient()

exports.handler = async (event) =&amp;gt; {

    const studentID = event.pathParameters.studentID

    const data = await dynamo.get({
        TableName:"school",
        Key:{
            ID:studentID
        }
    }).promise()

    const response = {
        statusCode: 200,
        body: JSON.stringify(data),
    };
    return response;
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;let's review the code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;By default the aws-sdk is installed in the instance running our function so we can use the DynamoDB client from there.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the get method on the document client takes an object as it's parameter where you specify the tablename and the key value to filter the data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The get method requires a call back we are converting the call back into a promise by invoking the .promise() method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if you deploy the code and try calling the api you'll get a internal server error to resolve it we have provide your function permission to access dynamodb.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So go to the configurations section in lambda and click on edit under the execution role card.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Click on the view role name and it will take you the aws iam console.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D-iU-83p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6uzrv8mxr2nzla4npmws.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D-iU-83p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6uzrv8mxr2nzla4npmws.png" alt="view iam role" width="880" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; There you'll notice your iam user only has the permission for lambda now we can add a new permission by either attaching a new policy or creating an inline policy.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;under services choose DynamoDB&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;under actions choose what all can this user do with a table &lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;For the sake of the post under resources select all resources but do not do this a production level project and click on review policy and then hit the create button.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Now try calling the api and it'll work&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Creating students
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now before you continue to the code provide DynamoDB write access to this function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The below code allows us to create new entries in DynamoDB.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const aws = require("aws-sdk")
const dynamo = new aws.DynamoDB.DocumentClient()

exports.handler = async (event) =&amp;gt; {
    const body = JSON.parse(event.body)
    console.log(body)

    const data = await dynamo.put({
        TableName:"school",
        Item:{
            ...body,
            ID: Math.floor(Math.random()*10000000).toString()
        }
    }).promise()


    const response = {
        statusCode: 200,
        body: JSON.stringify("Added new student welcome to the X-MEN"),
    };
    return response;
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;So here instead of &lt;code&gt;get&lt;/code&gt; we are using the &lt;code&gt;put&lt;/code&gt; method and we are extracting the data from the body of the request using &lt;code&gt;event.body&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now let's call the API using postman&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Now if you go to the DynamoDB explore section you'll see that the new student has been added.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Thanks for your time.&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>beginners</category>
      <category>aws</category>
      <category>dynamodb</category>
    </item>
    <item>
      <title>Create your first API with AWS lambda and Node</title>
      <dc:creator>ckmonish2000</dc:creator>
      <pubDate>Sat, 12 Nov 2022 09:23:06 +0000</pubDate>
      <link>https://forem.com/ckmonish2000/create-your-first-api-with-aws-lambda-and-node-3hnp</link>
      <guid>https://forem.com/ckmonish2000/create-your-first-api-with-aws-lambda-and-node-3hnp</guid>
      <description>&lt;p&gt;In the previous post we saw how to create a serverless lambda function with netlify now let create a function directly on AWS.&lt;/p&gt;

&lt;p&gt;we are going to use 2 AWS services namely lambda serverless function service from AWS and API gateway a service which it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup AWS API Gateway
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Login to your AWS console and search for API in the search bar and select API gateway&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;You have various options like HTTP, Rest and sockets but for the purpose of this post select HTTP API.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;You will be redirected to a page to create your API, so give a name to your API endpoint and click next also click next on the route's registration page as we will configure the later.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Now name your stage and make sure the auto-deploy switch is active and hit next [FYI: a stage is just a logical reference to a lifecycle state of your API (for example, dev, prod, beta, or v2).]&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Now Review your changes and create the API&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Creating an API route
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c9QsDA4F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/feaq5eqa6ct6ea5m3w7l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c9QsDA4F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/feaq5eqa6ct6ea5m3w7l.png" alt="Image description" width="880" height="421"&gt;&lt;/a&gt;&lt;br&gt;
Now we have an API endpoint setup in gateway now we have to configure routes like get student by id.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click on routes in the sidebar&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Click on create&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Now choose the Request type from the dropdown and add the route name and hit create [NOTE: the /{studentID} in the route indicates a route parameter which we will extract in the function]&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Now you have created an api endpoint the next step is to create a lambda function and link it to the route we just created.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Creating an Lamda function from the console
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Now search for lambda in the search bar select it and click on create function.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Select author from scratch &lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Give your function a name while keeping the default config and hit create function.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JJ-z4cBz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2z46ig7fd98gk78swfy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JJ-z4cBz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2z46ig7fd98gk78swfy.png" alt="Image description" width="880" height="412"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now you should see a view like this where you can setup various triggers to run your function and we are going to use API gateway's HTTP endpoint to trigger the function.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;And if you come down you should be able to view the function code and edit it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here we are extracting the studentID from the route and returning the student if their ID is present in the array.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Hit Deploy and your function must be live.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Linking the endpoint to your lambda function
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Go Back to the routes section of the API gateway and click on attach integration&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Then click on create and attach integration &lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;From the drop down choose Lambda function as integration type&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Then in the Integration details section select the lambda function we just created and make sure the invoke permission switch is active and then hit the create button.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Now if you come down to configurations under lambda function you should see the endpoint to trigger the function.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Now if you call the URL from the browser or postman you'll see the response.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Thankyou, &lt;/p&gt;

&lt;p&gt;for following this post in the next post I'll try to cover using Dynamo DB and how you can setup your serverless infrastructure using code with a framework called serverless&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>javascript</category>
      <category>aws</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Serverless functions for dummies⚡</title>
      <dc:creator>ckmonish2000</dc:creator>
      <pubDate>Mon, 07 Nov 2022 15:45:26 +0000</pubDate>
      <link>https://forem.com/ckmonish2000/serverless-functions-for-dummies-2aae</link>
      <guid>https://forem.com/ckmonish2000/serverless-functions-for-dummies-2aae</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;If you're thinking that you'll be building applications without a server your wrong,&lt;/p&gt;

&lt;p&gt;serverless means rather than you managing your servers it would be handled by cloud providers &lt;/p&gt;

&lt;p&gt;now you must be thinking then what's the difference between services like Heroku (you don't know what it's because they canceled their free tire a while back), &lt;/p&gt;

&lt;p&gt;well with services like Heroku you can choose how you want to structure your app and also note that your app will keep running even if no one is using it.&lt;/p&gt;

&lt;p&gt;But with serverless you have to split your logic into individual functions and the code only starts running when you make a request.&lt;/p&gt;

&lt;p&gt;There are various options for serverless functions, but the most popular ones are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;AWS Lambda&lt;/li&gt;
&lt;li&gt;Cloudflare Workers&lt;/li&gt;
&lt;li&gt;Firebase Cloud Functions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this blog post we will be using AWS Lambda,&lt;/p&gt;

&lt;p&gt;With netlify functions under the hood it uses AWS lambda, but it removes the complexity of setting up your environment.&lt;/p&gt;

&lt;p&gt;FYI, I would suggest using netlify only for small projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;initialize package.json
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add the dependency to run netlify-functions
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add netlify-lambda
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add the following code under the scripts section of package.json
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "server": "netlify-lambda serve functions",
    "build": "netlify-lambda build functions"
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a file named &lt;code&gt;netlify.toml&lt;/code&gt; and add the following in it
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[build]
  functions = "lambda"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now create a folder in the root directory called functions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a JS file with a name of your choice (Note: Name of the file will be the API endpoint)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Your first function
&lt;/h2&gt;

&lt;p&gt;Let's create a simple function that greets you when you request for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.handler = async (event, context) =&amp;gt; {
  const { name } = JSON.parse(event.body)

  return {
    statusCode: 200,
    body: `hello ${name}`
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me explain the code above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We are exporting an async function that takes in 2 arguments events and context.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;FYI: [event gives you access to the headers, body and other properties of the request and the context parameter provides methods and properties that gives you info about the function invocation.]&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Then you return a object with status code and body of your choice.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Run your serverless app locally
&lt;/h2&gt;

&lt;p&gt;Run the following command in your terminal and it should start your application of port 9000&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and now you can use postman or your app to query the API you just built.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Fetch live data from an API
&lt;/h2&gt;

&lt;p&gt;Here's another example, which fetches users list from github and returns data only on a GET request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const axios = require('axios').default;

exports.handler = async (event, context) =&amp;gt; {
  let data = await axios.get("https://api.github.com/users")

  if (event.httpMethod === "GET") {
    if (data) {
      return {
        statusCode: 200,
        body: JSON.stringify(data.data)
      }
    } 
  } else {
    return {
      statusCode: 404
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can also deploy the function to netlify &lt;a href="https://docs.netlify.com/functions/deploy/"&gt;Here's a link to the docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hope this blog was helpful thanks for your time.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>serverless</category>
      <category>aws</category>
      <category>node</category>
    </item>
    <item>
      <title>Express GraphQL</title>
      <dc:creator>ckmonish2000</dc:creator>
      <pubDate>Wed, 20 Jul 2022 08:56:00 +0000</pubDate>
      <link>https://forem.com/ckmonish2000/express-graphql-n7b</link>
      <guid>https://forem.com/ckmonish2000/express-graphql-n7b</guid>
      <description>&lt;p&gt;So this post is not gonna tell you what GraphQL is and how it's better over restful API's rather this is a gist to act as a tool to revise the basic concepts on GQL like how to define a query or mutation or how to write a resolver using a GraphQL server called Express GraphQl.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependencies &amp;amp; initial setup
&lt;/h2&gt;

&lt;p&gt;First and foremost we have to install the dependencies for the project , I'm assuming you have your bare bones node project already setup.&lt;/p&gt;

&lt;p&gt;Next run one of the command down below based on which package manager you are using :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn add express express-graphql graphql&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i --save express express-graphql graphql&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;after that set up the server in your index.ts file&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Queries &amp;amp; Mutations
&lt;/h2&gt;

&lt;p&gt;Once you have set up your GraphQL end point you'll have to setup your GraphQL Schema which is basically a combination of your GraphQL mutations, queries and subscriptions.&lt;/p&gt;

&lt;p&gt;to set up a Schema you have to first import 2 things &lt;/p&gt;

&lt;p&gt;&lt;code&gt;const {GraphQLSchema, GraphQLObjectType} = require('graphql')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the GraphQLSchema object is used to create a new instance of the schema and while initializing the object it take a JSON value as a parameter, This parameter will further contain the details for the queries and mutation.&lt;/p&gt;

&lt;p&gt;Down below I'm creating a file called test.ts which will export our schema&lt;/p&gt;

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

&lt;p&gt;once you have exported you schema import it and pass it into the your index.ts file so that your graphql end point knows where to look for the queries and mutations.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.use("/graphql", graphqlHTTP({&lt;br&gt;
  schema: Schema,&lt;br&gt;
  graphiql: true,&lt;br&gt;
}))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So let's begin by creating a query in a file called queries.ts&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X1ayF4RZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5eyifhqhezh27nrkyg7z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X1ayF4RZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5eyifhqhezh27nrkyg7z.png" alt="creating queries with express GQL" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;here let's first take a look at the GraphQLObjectType which takes in 2 parameters:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;name: indicates the name for the given return type of the resolver.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;fields indicates the values that a mutation or query will return so the fields have 2 sub fields i.e the type of the field and a custom function called resolve.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In type you add the type of value a particular field is gonna return and in resolve function your basically gonna return the value of the given type.&lt;/p&gt;

&lt;p&gt;and if you notice you can also create custom types like user in the above example it's not very different from creating a query,&lt;br&gt;
The only key difference is that &lt;/p&gt;

&lt;p&gt;a. In a custom object type you do not add the resolve function&lt;/p&gt;

&lt;p&gt;b. you can use the custom object type as a return type for any other resolver.&lt;/p&gt;

&lt;p&gt;and that's all we need to know in order to define a query and to define a mutation you can pretty much use the same steps but just pass the mutations in the mutations part of the schema.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arguments
&lt;/h2&gt;

&lt;p&gt;Now when you are building a mutation your gonna need to accept some parameters for that you can use the args key when defining a given query or mutation and extract them from the resolve function like shown down below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uc9QS6Rc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c54pmxtcyk90y4ue1383.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uc9QS6Rc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c54pmxtcyk90y4ue1383.png" alt="adding arguments with express GQL" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all for this post follow me for more such quick guides and handbooks&lt;/p&gt;

</description>
      <category>node</category>
      <category>graphql</category>
      <category>typescript</category>
      <category>api</category>
    </item>
    <item>
      <title>Node For Dummies</title>
      <dc:creator>ckmonish2000</dc:creator>
      <pubDate>Wed, 15 Jun 2022 07:46:08 +0000</pubDate>
      <link>https://forem.com/ckmonish2000/node-for-dummies-1mbk</link>
      <guid>https://forem.com/ckmonish2000/node-for-dummies-1mbk</guid>
      <description>&lt;p&gt;There are a million ways you can learn Node JS but the one I Prefer is hand made notes that can assist me in all my future projects.&lt;/p&gt;

&lt;p&gt;If your something like me you would also like sicknotes and cheat sheets for your future references.&lt;/p&gt;

&lt;p&gt;So without further ado let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  WTF Is Node.js
&lt;/h2&gt;

&lt;p&gt;Once upon a time JavaScript was a language that could not do anything more than manipulate DOM elements, In other words JS was confined only to your browser.&lt;/p&gt;

&lt;p&gt;But today we can do just about anything with JS from building websites to training AI models and that's all thanks to Node.&lt;/p&gt;

&lt;p&gt;So how exactly did node revolutionize JS ??? &lt;/p&gt;

&lt;p&gt;Basically when you write a line of code you need what's called a compiler that translates human readable code into lower level format that your computer can understand.&lt;/p&gt;

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

&lt;p&gt;So browsers are shipped with something called a JS engine that acts as a compiler, &lt;/p&gt;

&lt;p&gt;Like shown in the image above the code from your app will go through the engine to perform the required operations but there are a lot of limitations to the JS engine shipped with browsers like we can not interact with the OS or create files with JS.&lt;/p&gt;

&lt;p&gt;To over come these limitation Ryan Dahl in 2009 introduce the world to Node.JS&lt;/p&gt;

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

&lt;p&gt;which is basically uses chromes JS engine called V8 and wraps it  with Libuv a library built with C which gives JS the ability to perform a lot of native operations we will be visiting over the course of this series so stay tuned for the next blog post.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>WTF is WEB 5.0 ☣️</title>
      <dc:creator>ckmonish2000</dc:creator>
      <pubDate>Wed, 15 Jun 2022 07:45:08 +0000</pubDate>
      <link>https://forem.com/ckmonish2000/wtf-is-web-50-e8i</link>
      <guid>https://forem.com/ckmonish2000/wtf-is-web-50-e8i</guid>
      <description>&lt;p&gt;Web 5 announced by Block is the middle ground between web 2.0 and web 3.0 i.e.&lt;/p&gt;

&lt;p&gt;✅ WEB 2.0 + WEB 3.0 = WEB 5.0&lt;/p&gt;

&lt;p&gt;💵 Web 5 has made bitcoin their central currency but unlike web 3,&lt;/p&gt;

&lt;p&gt;Web 5 is not built on the blockchain so Bitcoin will only act as a currency to purchase in app assets like subscriptions, mods and stuff like that.&lt;/p&gt;

&lt;p&gt;👨‍💻 Web 5 is open source and is aiming to build a decentralized network layer using Bitcoin's layer 2 payment protocol called lighting.&lt;/p&gt;

&lt;p&gt;Web 5 is divided in to 3 main components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Wallet 👛&lt;/li&gt;
&lt;li&gt;Node 🌐&lt;/li&gt;
&lt;li&gt;DWAS 📱&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Wallets and nodes are almost similar to web 3&lt;/p&gt;

&lt;p&gt;but the add on's for wallets is what intrigue me,&lt;/p&gt;

&lt;p&gt;so apart storing assets and identifying a user the wallet will also have the ability to store user settings and meta data,&lt;/p&gt;

&lt;p&gt;E.g. you are some one who loves dark mode so rather than going to each app and turning on dark mode, All you will have to do is enable dark mode on your wallet and that setting will be carried forward to all your apps (A global state management system of sorts).&lt;/p&gt;

&lt;p&gt;And as a web 5 developers you wont develop DAPP or PWA you will develop something called DWAS that's decentralized web apps&lt;/p&gt;

&lt;p&gt;where the app will be hosted on a node in the network rather than on a central server,&lt;/p&gt;

&lt;p&gt;You may ask how is this any different from web 3 it's simple in web 3 all the data is decentralized but&lt;/p&gt;

&lt;p&gt;In web 5 the organization can store data related to their platform on their own servers but as a users will be able to abstract your personal information and preferences.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>dapps</category>
      <category>solidity</category>
    </item>
  </channel>
</rss>
