Scalability is an essential software component. Prioritizing it from the start leads to lower maintenance costs, better user experience, and higher agility.
Thinking in that one of the biggest problem when talking about scalability is the database.
This project is an study case where we will build a very simple API with Django Rest Framework, which consumes data from a serverless postgres database provided by AWS. Then we will dockerize our app and deploy it in an "production environment" using AWS Fargate.
- Django Rest Framework >= 3.11.0 (powerful and flexible toolkit for building Web APIs)
- AWS Account (You need to have an AWS account to deploy the application and to create the database)
- Docker (We will deploy our app using docker containers for this reason you should know docker concepts to understand how things are working).
After clone or download this repository you must configure a file called .env
inside the main folder tutorial
. As you can see, inside this folder there is a sample file called .env.example
that you can copy or rename to .env
.
This file is where we can configure our environments variables using keys.
WARNING: be careful in production environment you must configure this variables using other aproach! |
---|
For running this project locally, just keep the file with two keys:
DEBUG=True
SECRET_KEY=${Put same random and unique value here}
When you don't configure a DATABASE_URL
key by default the app will use Sqlite DB Engine.
After create the .env
file just run the following command:
docker-compose up
If the command was successful your terminal will show:
The server will be available on http://0.0.0.0:8800/.
To create and activate a new AWS Account, please follow the steps covered by this article: How do I create and activate a new Amazon Web Services account?.
After that is it important to install AWS CLI which will allow us to do changes at our AWS Account from command line.
Finally, you can configure your aws credentials.
To know more about AWS Fargate you should read the oficial documentation.
This chapter will cover the configuration of a AWS Fargate Cluster where we will deploy our application.
As you have read AWS Fargate is a serverless compute engine for containers so we need to configure the container that will run our application.
To do this we have created the Dockerfile that contains the instructions to build a docker image thar can be used to create containeres to run our application.
We will store this docker image in a Elastic Container Repository (ECR). To create this repository you can run the following command:
aws ecr create-repository --repository-name django-aws-fargate-playground --region us-east-1
If the command is successful we should see:
{
"repository": {
"repositoryArn": "arn:aws:ecr:us-east-1:${accountId}:repository/django-aws-fargate-playground",
"registryId": "${accountId}",
"repositoryName": "django-aws-fargate-playground",
"repositoryUri": "${accountId}.dkr.ecr.us-east-1.amazonaws.com/django-aws-fargate-playground",
"createdAt": 1550555101.0
}
}
In AWS console you will see something like this:
After click on the repository name you will see all the images inside that repository. Click on View push commands
to see a list of commands that we need to run to be able to push our image to ECR. Follow the steps as they are given.
Now we have pushed our image in ECR.
After pushing the image you can see the second column called Image URI (we will use this info to configure the Task container in AWS Fargate).
Now, let us go to the link https://console.aws.amazon.com/ecs/home?region=us-east-1#/getStarted and create a new Fargate Application. Click on Get Started
.
Now select under the container definition choose Custom and click on Configure.
In the popup, enter a name for the container (django-aws-fargate-playground-container) and add the URL to the container image we have pushed to ECR in the step before.
Now we can see the status of the service we just created.After the steps being completed click on View Service
button.
Now we can test our fargate cluster. For that on the services page, click on the Tasks tab (as ilustrated in the image below) to see the different tasks running for our application. Click on the task id to see details about that task.
As you can see a public IP was atributed to our cluster so you can access the application going to the url http://52.91.33.228:8800
A last important point in this topic is about VPC
. AWS Fargate creates a new VPC, subnets and security group for our cluster. In the next topic we will see how to create Aurora Serverless Database and we have to run this database inside the same VPC of our AWS Fargate Cluster because that is the only way to grant access to the database to our container.
You can see the VPC created under service details tab:
Inside AWS Console go to RDS and create a new Database.
We choose postgres compatibility, but, you can choose mysql if you prefer.
In this project we will use Serveless database to delegate to AWS the responsibility to scale our infrastructure when it is necessary and we will pay only for the effective resources use.
And finally the point about VPC that we have commented, don't forget to choose the VPC created by AWS Fargate in the step before.
After that click on Create Database and wait until the creation process ends.
Click on DB Identifier to see database details. Inside this page you will see the instructions to connect to your database.
To see the oficial docs about how to connect to an Amazon Aurora DB Cluster access the url: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Connecting.html
Now that we can build the connection string to the database and configure the key DATABASE_URL
in the .env file, then generate a new docker image with the new version of the application and push to ECR.
In our case we can build the postgres connection url in the following way:
DATABASE_URL=psql://urser:password@host:5432/database_name
By default database_name
is postgres
. Unless you have specified a different name during the process of database creation.
After create this key into the file .env, repeat the steps to generate a new image and to push it to ECR repository we have created. You could review the steps clicking here
Now we have all the configurations necessary to run our application in AWS, but, we need to allow our container to access the database.
To do it we need to insert our container security group into the Inbound configuration of the database security group as you can see in the image below:
After that your container will have the grants to access the database.
Yes, please! This project was created just for study and to guide other developers who would like to use this tecnologies. Any other suggestion will be kindly accepted!
Any doubts or suggestions please contact me muriloamendola@gmail.com.