A REST API developed using Django Rest Framework, which uses AWS Dynamodb as its schemaless database. We will add an Admin dashboard to interact with the API, as a superuser. We use pynamodb, for its ORM, to write models and interface to Dynamodb database.
- Installation Instructions
- Setup Django RESTapi project
- Setup Dynamodb
- Deploying on Lambda-function
- Further Help
- License
First setup your python virtual environment and activate it.
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt
project
│ README.md
│ manage.py
| .env.example
| .env.local
│ .env.dev
│ .env.prod
│
└───junkie_django_api
│ __init__.py
│ asgi.py
│ settings.py
│ env.py
| urls.py
│ wsgi.py
└───apps
|
└───productionLine/
|
└───products/ # django-app
|
└───contactUs/ # django-app
We have setup multiple environments.
In our present case as AWS provides downloadable version of dynamodb which can be used for development purposes on local machine. Which further elliminates the requirement of connecting to a cloud hosted database to test our backend bussiness logic.
So, we have setup an addditional environment called local environment
, which uses AWS's downloadable Dynamodb server, installed and run on our local machine.
We have setup separate environments for locally run Dynamodb development environment(.env.local
), cloud hosted Dynamodb development environment(.env.dev
) and production environment(.env.prod
).
- Similarly
.env.prod
file.- Since, we are using Zappa to deploy our production ready code base, we wll fix many of the production environment variables inside the auto generated zappa_settings.json
Next import these environment variables inside django settings.py
file.
You can download AWS CLI and configure your AWS credentials. Make sure to use different IAM roles for production environment and development environments, for security concerns.
> aws configure
AWS Access Key ID : "your-access-key-id"
AWS SECRET ACCESS KEY : "your-secret-key"
AWS provides downladable version of dynamodb, allowing a developer to experiment without any fear to mess something up. Use this link to setup Dynamodb on your local machine.
To start a dynamodb server on your local machine:
- Put in following command in your powershell
> cd path\to\aws\dynamodb_local_latest
> java -D"java.library.path=./DynamoDBLocal_lib" -jar DynamoDBLocal.jar -dbPath path\to\write\database_file folder
By default the dynamodb-server runs at http://localhost:8000
To run the django local environment. Provide the relevant environment variable from console or run:
> $env:ENV = "local"
> python manage.py runserver 8080
or
> python manage.py runserver 8080 --env local/dev/prod
Follow the instructions provided on AWS website.
or you can setup the variables to be fixed dynamically inside Zappa settings file.
Using Pynamodb
"PynamoDB is a Pythonic interface to Amazon’s DynamoDB. By using simple, yet powerful abstractions over the DynamoDB API..."
If you are familiar with django's style of ORM(Object Relational Mapper) model, serialiser and class definition of views you can readily setup your RESTapi backend using pynamodb API.
Setup AWS account and confgure IAM roles, policies and permission so as to allow zappa to manage AWS resources for your API deployment.
References:
- How to create a serverless service in 15 minutes
- Creating a role to delegate permissions to an AWS service
you can check the AWS resources required for zappa deployment inside zappa_settings_example.json file
Finally we deploy our RESTApi using Zappa.
- Pay per usage
- Round the clock availability, Zero charges for hosting.
- minimal initial manual setup required
- Built-in logging system
Setup steps:
> zappa init # configure zappa_settings.json so generated to get access to AWS assets through separate IAMM role
> zappa deploy dev # for development server
> zappa deploy prod # for production environment
> zappa update <dev/prod> # to update changes in your code base
> zappa undeploy <dev/prod> # to remove the deployment
This project is an open-source initiative by Junkie Labs team.
For any questions or suggestions send a mail to junkielabs.dev@gmail.com or chat with the core-team on gitter.