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.
$ git clone https://github.com/erfanghorbanee/Simple-API-DynamoDB.git
$ cd Simple-API-DynamoDB
$ python -m venv venv
$ . venv/bin/activate
python -m venv venv
venv\Scripts\activate.bat
cd simple_api/
pip install -r requirements.txt
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
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
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.
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":""
}
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
python manage.py test
We have two APIs at the moment, one for creating an instance in dynamo db and another one for getting it.
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"
}
If any of the payload fields are missing:
HTTP GET
URL: http://127.0.0.1:8000/api/v1/devices/id1/
Example: GET https://api123.amazonaws.com/api/devices/id1
NOTE: YOU CAN CHECK YOUR TABLES AND SEE IF THE INSTANCES WERE CREATED SUCCESSFULLY:
aws dynamodb scan --table-name Devices
Is there any advantage of using an integer hash key over a string hash key?
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.
How can I deploy this on AWS Lambda?
Use the following commands in order:
$ pip install zappa
$ zappa init
$ zappa deploy
for more information, check out these links: