Build your first CRUD API with AWS ☁️
Tags: awscommunity, api, aws, cloud
Published: 2023-03-25 13:32:06




“ The purpose of this tutoriel is to build a very simple CRUD (Create , read, update, delete) API. To acheive this we will be guided through a few steps . From the DynamoDB console to the AWS Lambda console , we’ll learn how to create a DynamoDB table and Lambda function. After that, we’ll use the API Gateway interface to configure an HTTP API , and then we’ll use Postman to test your API ! ”

![](https://cdn-images-1.medium.com/max/2000/0*9hg9l5gf8dRbcHTO.png)

**1.Create A DynamoDB Table**

**Amazon DynamoDB **is a fully managed NoSQL database service with seamless scalability and quick and predictable performance. You don’t have to worry about hardware provisioning , setup and configuration, replication, software patching, or cluster scalability with DynamoDB because if offloads the administrative requirements of running and managing a distributed database.

To **create** a DynamoDB table :

* Open **the DynamoDB console** at [**https://console.aws.amazon.com/dynamodb/](https://console.aws.amazon.com/dynamodb/)**

* Select **Create** Table

![](https://cdn-images-1.medium.com/max/2140/0*p0famO5aSXFN9URU.png)

For Table name, enter **crud-http-table** , for Primary key , enter **id** and choose **create**

![](https://cdn-images-1.medium.com/max/2048/0*z0MVsdHu-xwl_Ojy.png)

Our DynamoDB table becomes available after a few seconds .

**2. Create A Lambda Function**

AWS Lambda is a compute service that allows you to run programs without the need for server provisioning or management. Lambda automatically scales from a few requests per day to thousands per second , running your code only when it’s needed.

We create Lambda function for the backend of your API. this lambda function creates, reads, updates, and deletes (CRUD) items from DynamoDB. The function uses events from API Gateway to determine how to interact with DynamoDB.

To **create** a Lambda function

* Open **The Lambda console** at [**https://console.aws.amazon.com/lambda](https://console.aws.amazon.com/lambda)**

* Select **Create function**

![](https://cdn-images-1.medium.com/max/2288/0*kFC9GRbmBUB7X_5w.png)

* Fill the function name , I will use **“ crud-http-function ”** for this example , Runtime **Node.js 14.x** , Under Permissions choose **Change default execution role** and select **Create a new role from AWS policy templates**. For Role name , enter **“ crud-http-role” **and choose **Simple microservice permissions** form Policy templates , this policy grants the Lambda function permission to interact with DynamoDB.

![](https://cdn-images-1.medium.com/max/2800/0*O4xDlzpvA2Ptil_H.png)

![](https://cdn-images-1.medium.com/max/2800/0*nXmRg-ZBfMoYC_dR.png)

* Scroll down to the console’s Code source editor ,open **index.js** and replace its contents withe the following code.

![](https://cdn-images-1.medium.com/max/2800/0*Gi75ch9H_UCOJ64e.png)

[https://github.com/omaralaoui1/CRUD_API_NODEJS/blob/main/index.js](https://github.com/omaralaoui1/CRUD_API_NODEJS/blob/main/index.js)

* Choose **Deploy** to update your function

**3. Create an HTPP API**

Amazon API Gateway is a fully managed service that makes publishing, maintaining, monitoring, securing, and operating APIs at any size simple for developers.

You can create RESTful APIs with API Gateway using either HTTP APIs or REST APIs. API Gateway, along with AWS Lambda, is the app-facing component of the AWS serverless infrastructure.

To **create** an **HTTP API**

* Open **the API Gateway console** at [**https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)**

* Select **Create API** and choose **build** for **HTTP API**

![](https://cdn-images-1.medium.com/max/2800/0*7_xwEqF4TuLSEX68.png)

* Fill **the API name** , I will use **‘ crud-http-api ’** for name , for **configure routes** choose **Next** to skip route creation . we’ll create routes later .

**4. Create routes**

![](https://cdn-images-1.medium.com/max/2000/0*UhfKrayy1kqvXwJD.png)

Routes are a way to send incoming API requests to backend resources. Routes consist of two parts: an HTTP method and a resource path, for example, **GET /items**.

For this example API, we create four routes:

* **GET** /items/{id}

* **GET** /items

* **PUT** /items

* **DELETE** /items/{id}

To **create** routes:

* Open **the API Gateway console** at [**https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)**

* Select **your API ( crud-http-api )**

* On the left panel choose **Routes** , and select **Create**

![](https://cdn-images-1.medium.com/max/2800/0*FlZlLI5WmZamGbNV.png)

* For **Method**, choose **GET**

* For the path, enter /itemsand choose **create **. Repeat the same thing for **GET** /items/{id}, **DELETE** /items/{id}, and **PUT** /items.

![](https://cdn-images-1.medium.com/max/2800/0*NNeGa-lRpI_svcCo.png)

![](https://cdn-images-1.medium.com/max/2800/0*qecD82UjlwEJp_Nz.png)

**5. Create an integration**

To connect a route to backend resources, you construct an **integration**. These integrations will be attached to a route at a later time. You establish a single Lambda integration for this sample API, which you use for all routes.

To **create** an integration:

* Open **the API Gateway console** at [**https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)**

* Select **your API ( crud-http-api )**

* On the left panel choose **Integrations** , and select **Manage integrations** and then choose **Create**

![](https://cdn-images-1.medium.com/max/2800/0*LIZRcyhRFh9U5brw.png)

* For **Integration** Type, choose **Lambda Function** and enter **“ crud-http-function ”**

![](https://cdn-images-1.medium.com/max/2800/0*LH_sQJiSyxpdFTL-.png)

* Choose **Create**

**6. Attach your integration to routes**

All routes in this example API use the same AWS Lambda integration.

To attach integrations to routes:

* Open **the API Gateway console** at [**https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)**

* Select **your API ( crud-http-api )**

* On the left panel choose **Integrations** , and select a **route**

* Under Choose an existing integration , choose **“ crud-http-function”**

![](https://cdn-images-1.medium.com/max/2800/0*6fNf1cFsU1XrrNSw.png)

* Choose **attach integration** , Repeat the same thing for all routes .

![](https://cdn-images-1.medium.com/max/2000/0*_xOYykdKDZCSzE_E.png)

All routes show that an AWS Lambda integration is attached.

**7. Test your API**

To make sure that our API is working , we use Postman , an API Platform for building and using APIs.

To get the URL to invoke your API:

* Open **the API Gateway console** at [**https://console.aws.amazon.com/apigateway/](https://console.aws.amazon.com/apigateway)**

* Choose **your API** , and Note **your API’s invoke URL**

![](https://cdn-images-1.medium.com/max/2800/0*fgVHYD5P7aQtdUin.png)

* **Copy your API’s invoke URL .**

To create an item with PUT Method :

* Open Postman and add your API’s URL

* Choose **PUT Method** to send your data to **Dynamodb Table** through **API Gateway** and **Lambda Function**.

* Add a request body with **item’s ID**, **first_name** , **last_name** and **age** .

![](https://cdn-images-1.medium.com/max/2000/0*EAulMm0PeDqca1k-.png)

![](https://cdn-images-1.medium.com/max/2000/0*Eqpm8xbxvdjcHIk0.png)

* **To get all items :**

![](https://cdn-images-1.medium.com/max/2000/0*K1X18mhkAbmZ_upa.png)

* **To delete an item:**

![](https://cdn-images-1.medium.com/max/2000/0*jdjKxzStlIooLC-j.png)

## Next Steps ….
>  *You can automate the creation of AWS resources by using **AWS CloudFormation **.*
>  If you enjoyed this article, share it with your friends and colleagues! *😃s*

![](https://cdn-images-1.medium.com/max/2000/0*iG93YytYynGtGUYn.jpg)