DEV Community

Cover image for Build a CRUD Serverless API from scratch using AWS Lambda (Python), API Gateway, and DynamoDB.

Build a CRUD Serverless API from scratch using AWS Lambda (Python), API Gateway, and DynamoDB.

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

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

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

Image description

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

Image description

Step -3 - Build the REST API

Image description

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

Image description

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

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

Image description

Step -5 Deploy the API

Image description

Image description

Step -6 Create DynamoDB Table

Image description

6. TEST ALL APIs

POST: For a single product

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

Image description

Image description

GET: For a single product

Image description

PATCH: For a single product

Image description

Image description

DELETE: For a single product

Image description

GET multiple products:

Image description

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

Medium

Oldest comments (0)