Build a CRUD Serverless API from scratch using AWS Lambda (Python), API Gateway, and DynamoDB.
Tags: serverless, devops, aws, python
Published: 2023-08-22 00:28:07

**1. Introduction**

AWS Lambda is a serverless compute solution that allows you to run code without the need for server provisioning or management. You only pay for the compute time you use. This indicates that my own project, which will not receive a lot of traffic, is a great choice for AWS Lambda. This use case is ideal for applications with variable traffic or that can scale to zero.


**2. Prerequisites**
IAM Role/IAM User with the enough permissions.
AWS CLI installed
Python3
AWS CLI
GIT



**3. AWS Resources**

You'll need the AWS resources listed below.

AWS IAM Role
AWS Lambda
AWS API Gateway
AWS DynamoDB

**4. Architecture**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svurzp5emszoi2mxpgpl.png)


You can create the resources manually using the AWS Console, or you can leverage this using any IAC options like Terraform, AWS SAM/CloudFormation. In this Article you will use the AWS Console to things easier 

**5. Deployment Steps**

Step -1 - Create a role (name it as you wish and add permissions)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2zdzdm2laoqwq1hmxa35.png)

Step -2 - Create a lambda function (name it as you wish, chose the latest python version as runtime)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7j99tsjgpitrwpt7q194.png)


Python codes can be found here:

git clone https://github.com/Assassin010/CRUD-Serverless-API-Python-G.git

git clone https://Assassin10@bitbucket.org/assassin10/crud-serverless-api-python-b.git

git clone https://gitlab.com/Assassin010/CRUD-Serverless-API-Python-L.git

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ck93lctp6y4t1h5q7c67.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51iaos4tgi867k62nsto.png)


Step -3 - Build the REST API

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46iaduk82aeg173cydxs.png)


Create 3 resources 
health with path /health
product path /product
products path /products

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dwu0y2be6bkp36w3lfnz.png)


Step -4

Create Methods for each resources 

- On health resource, create "GET" method

- On product resource, create "GET", "POST", "PATCH" and 
"DELETE"

- On products resource, create "GET"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1qi0zhwvtn6c8uxfoaj8.png)


check all boxes as followed for all Methods on each resource and chose your created lambda (Step -2)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1fht28lhg9hkw5r0zkx.png)


Step -5 Deploy the API


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7oo9fnexugsl7cld2izm.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oscly418jh9ichlw7j54.png)

Step -6 Create DynamoDB Table


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8d1ml7sl0pyl0b02fn3z.png)


**6. TEST ALL APIs**

POST: For a single product

`{
    "productId": "bag",
    "color": "black",
    "price": "100",
    "quantity": "1500"
}`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdo276h3r6rsa05kybfq.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bz98as97qf4v4qujiane.png)

GET: For a single product


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dzrrvhst0bm821o1hly.png)


PATCH: For a single product


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p55t1wh1gahmysjw14qc.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uew71tx52cmbay8t1ov0.png)


DELETE: For a single product


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9kfxomsr1m4yapluqnf.png)


GET multiple products:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s17o6n5usr2p7g38tokb.png)


Notes: You will need POST few products before trying to GET all of them.


**7. References**

https://aws.amazon.com/lambda/
https://aws.amazon.com/api-gateway/
https://aws.amazon.com/dynamodb/
https://aws.amazon.com/cloudwatch/
https://boto3.amazonaws.com/v1/documentation/api/latest/index.html



Happy coding!
END - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

If you like my work and want to support me…
The BEST way is following me on dev.to here

Feel free to give likes, or writing comments so I know how helpful this post was for you.

Gauthier Kwatatshey__
Connect with me on [LinkdIn](https://www.linkedin.com/in/gauthier-kwatatshey-b9a66715b)

[Medium](https://medium.com/@gauthier.kwatatshey/build-a-crud-serverless-api-from-scratch-using-aws-lambda-api-gateway-and-dynamodb-847ef5b48834)