DEV Community

OMAR EL AALLAOUI for AWS Community Builders

Posted on

Build your first CRUD API with AWS ☁️

“ 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 ! ”

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 :

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

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

  • 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.

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

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

  • 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

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:

  • 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.

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:

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

  • 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**

  • 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”

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

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:

  • 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 .

  • To get all items :

  • To delete an item:

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

Top comments (0)