/Simple-API-DynamoDB

A simple server-less Restful API on Django using the following tech stacks: Python - Django - AWS DynamoDB - S3

Primary LanguagePythonMIT LicenseMIT

Simple-API-DynamoDB

made-with-python Code style: black Imports: isort License: MIT

A simple server-less Restful API on Django using the following tech stacks: Python - Django - AWS DynamoDB - S3
Ready to deploy on AWS Lambda using Zappa.

Table of Contents

How to run the project?

Clone the repository :

$ git clone https://github.com/erfanghorbanee/Simple-API-DynamoDB.git
$ cd Simple-API-DynamoDB

Create a virtualenv and activate it :

$ python -m venv venv
$ . venv/bin/activate

Or on Windows cmd :

python -m venv venv
venv\Scripts\activate.bat

Install the requirements :

cd simple_api/

pip install -r requirements.txt

Configure your AWS credentials :

aws configure

now enter your credentials and you're good to go!

AWS Access Key ID [None]: MYACCESSKEY
AWS Secret Access Key [None]: MYSECRETKEY
Default region name [None]: MYREGION
Default output format [None]: json

Run dynamodb migrator :

You can find it in Simple-API/simple_api/dynamodb_migrator.py directory inside the project.
this is similar to the usual makemigrations command that we run all the time, it creates the table we want for this project.
feel free to change it the way you need.

python dynamodb_migrator.py

Check if your table is created successfully using this command :

aws dynamodb list-tables

Config your secret variables!

Local:

As you might know, it's not secure to put important variables such as SECRET_KEY directly in the code,
so instead in the Simple-API/simple_api/ directory create a .env file,
this will be where we put our variables and fetch them in settings.py using Python Decouple

example:

SECRET_KEY='MYSECRETKEY'
DEBUG=True

to learn more, you can check this article.

Production:

In this project, I used Zappa to deploy on aws lambda. therefore I have a zappa_settings.json file and I'm going to store my environment variables in it!

"environment_variables":{
   "AWS_ACCESS_KEY_ID":"",
   "AWS_SECRET_ACCESS_KEY":"",
   "AWS_STORAGE_BUCKET_NAME":"",
   "AWS_S3_CUSTOM_DOMAIN":"",
   "SECRET_KEY":""
}

Run the development server :

This code is for production, so you have to make a few changes before running it on the local server.

NOTE: Make sure to configure settings.py properly:

SECRET_KEY = "SECRET_KEY"
DEBUG = True
STATIC_URL = '/static/'
# STATICFILES_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"  # Comment this one on local server!

NOTE 2: Put your own aws credentials in devices/api/views.py to connect to your dynamodb :

# Get the service resource.
dynamodb = boto3.resource(
    "dynamodb",
    aws_access_key_id="MYACCESSKEY",
    aws_secret_access_key="MYSECRETKEY",
    region_name="MYREGION",
)
table = dynamodb.Table("Devices")

and we're good to go!

python manage.py runserver

Run the tests :

python manage.py test

Final Result

We have two APIs at the moment, one for creating an instance in dynamo db and another one for getting it.

Request 1:

HTTP POST
URL: http://127.0.0.1:8000/api/v1/devices/
Body (application/json):
{
"id": "/devices/id1",
"deviceModel": "/devicemodels/id1",
"name": "Sensor",
"note": "Testing a sensor.",
"serial": "A020000102"
}

Response 1 - Success:

image

Response 1 - Failure 1:

If any of the payload fields are missing:

image


Request 2:

HTTP GET
URL: http://127.0.0.1:8000/api/v1/devices/id1/
Example: GET https://api123.amazonaws.com/api/devices/id1

Response 2 - Success:

image

Response 2 - Failure 1:

image

NOTE: YOU CAN CHECK YOUR TABLES AND SEE IF THE INSTANCES WERE CREATED SUCCESSFULLY:

aws dynamodb scan --table-name Devices

Frequently Asked Questions

Question 1

Is there any advantage of using an integer hash key over a string hash key?

Answer

Serialized numbers are sent to Amazon DynamoDB as String types, which maximizes compatibility across languages and libraries, so there shouldn't be any advantage to using an integer hash key over a string hash key.

Question 2

How can I deploy this on AWS Lambda?

Answer

Use the following commands in order:

$ pip install zappa
$ zappa init
$ zappa deploy

for more information, check out these links: