<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pavan S</title>
    <description>The latest articles on DEV Community by Pavan S (@pavan_s_7438006da5249fe71).</description>
    <link>https://dev.to/pavan_s_7438006da5249fe71</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%2F2263138%2F1569d876-772c-4ccd-969a-cb501ae117e5.jpg</url>
      <title>DEV Community: Pavan S</title>
      <link>https://dev.to/pavan_s_7438006da5249fe71</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pavan_s_7438006da5249fe71"/>
    <language>en</language>
    <item>
      <title>"Building a Serverless REST API with AWS Lambda, API Gateway, and DynamoDB Using Python"</title>
      <dc:creator>Pavan S</dc:creator>
      <pubDate>Sun, 27 Oct 2024 09:07:59 +0000</pubDate>
      <link>https://dev.to/pavan_s_7438006da5249fe71/building-a-serverless-rest-api-with-aws-lambda-api-gateway-and-dynamodb-using-python-35jb</link>
      <guid>https://dev.to/pavan_s_7438006da5249fe71/building-a-serverless-rest-api-with-aws-lambda-api-gateway-and-dynamodb-using-python-35jb</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In this project, I developed a serverless REST API using AWS Lambda, API Gateway, and DynamoDB. The API enables CRUD operations on employee records, demonstrating how to leverage AWS's serverless architecture to build scalable, cost-effective applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tech Stack
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AWS Lambda&lt;/strong&gt;: Executes CRUD functions without the need for server management.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Gateway&lt;/strong&gt;: Routes HTTP requests to specific Lambda functions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DynamoDB&lt;/strong&gt;: Stores employee records in a NoSQL database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Python (boto3)&lt;/strong&gt;: Handles the application logic and database operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step-by-Step Project Setup
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Set Up DynamoDB Table
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Navigate to DynamoDB&lt;/strong&gt; in the AWS Console and &lt;strong&gt;create a new table&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Table Name&lt;/strong&gt;: &lt;code&gt;employee_info&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Primary Key&lt;/strong&gt;: &lt;code&gt;employeeid&lt;/code&gt; (String).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Attributes&lt;/strong&gt;: Define fields such as &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;department&lt;/code&gt;, &lt;code&gt;position&lt;/code&gt;, and &lt;code&gt;salary&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;DynamoDB offers a highly scalable NoSQL solution perfect for handling unstructured employee data.&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%2F4rr1thnd9jdekigun6rz.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%2F4rr1thnd9jdekigun6rz.png" alt="DynamoDB " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Create an IAM Policy
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Sign in to the AWS Management Console&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Navigate to the &lt;strong&gt;IAM&lt;/strong&gt; console.&lt;/li&gt;
&lt;li&gt;In the left navigation pane, click on &lt;strong&gt;Policies&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click on the &lt;strong&gt;Create policy&lt;/strong&gt; button.&lt;/li&gt;
&lt;li&gt;ADD policy for Dynamodb&lt;/li&gt;
&lt;/ol&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%2F6ud5ctbf63upkn0xofc3.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%2F6ud5ctbf63upkn0xofc3.png" alt="IAM" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Write the Lambda Function Code
&lt;/h3&gt;

&lt;p&gt;Create a Lambda function in Python that will handle requests for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Retrieving all employees or a specific employee (&lt;code&gt;GET&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Adding a new employee (&lt;code&gt;POST&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Updating an employee's details (&lt;code&gt;PATCH&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Deleting an employee record (&lt;code&gt;DELETE&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&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%2Fbqtul8knt8f87pxxbq6y.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%2Fbqtul8knt8f87pxxbq6y.png" alt="lambda" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Create API Gateway Endpoints
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Set Up API Gateway
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;strong&gt;API Gateway&lt;/strong&gt; and create a REST API.&lt;/li&gt;
&lt;li&gt;Define Resources and Methods:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;/employee&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET&lt;/code&gt; - Retrieve employee data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST&lt;/code&gt; - Add new employee.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PATCH&lt;/code&gt; - Update existing employee data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DELETE&lt;/code&gt; - Remove an employee record.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;/employees&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET&lt;/code&gt; - Retrieve all employees.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;/Status&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET&lt;/code&gt; - Retrieve all employees Status.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&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%2F3ltwpia9my8betwre29j.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%2F3ltwpia9my8betwre29j.png" alt="API Gateway" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Connect Methods to Lambda
&lt;/h3&gt;

&lt;p&gt;For each HTTP method, connect the endpoint to the appropriate Lambda function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Deploy the API
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;strong&gt;Deployment Stage&lt;/strong&gt; in API Gateway.&lt;/li&gt;
&lt;li&gt;Deploy the API to make the endpoints accessible.&lt;/li&gt;
&lt;li&gt;Note the endpoint URL generated by API Gateway; this will serve as the base URL for the REST API.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Example Requests
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GET&lt;/strong&gt; &lt;code&gt;/employee?employeeid=123&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Retrieves data for employee with ID 123.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;POST&lt;/strong&gt; &lt;code&gt;/employee&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adds a new employee.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Body:&lt;/strong&gt;&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
json
  {
    "employeeid": "123",
    "name": "John Doe",
    "position": "Engineer",
    "salary": 60000
  }

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

&lt;/div&gt;

</description>
      <category>aws</category>
      <category>cloudskills</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
