Skip to content

Sample project with a very simple API build with Django Rest Framework to illustrate the use of AWS Fargate and Aurora Serverless with postgres compatibility.

Notifications You must be signed in to change notification settings

muriloamendola/django-aws-fargate-playground

Repository files navigation

django-aws-fargate-playground

PRs Welcome

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.

Requirements

  • 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).

Running project locally

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: server running locally

The server will be available on http://0.0.0.0:8800/. server running locally on http://0.0.0.0:8800

Running project in AWS

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.

Creating AWS Fargate Cluster

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.

Elastic Container Repository (ECR)

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: ECR Repository

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.

ECR Repository list of images and push commands

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).

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. AWS Fargate configure custom container

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.

AWS Fargate custom container configuration AWS Fargate custom container creation AWS Fargate service definition AWS Fargate cluster definition AWS Fargate cluster review AWS Fargate cluster created

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. AWS Fargate service details AWS Fargate task details

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 AWS Fargate application successful

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: AWS Fargate service details

Creating Aurora Serverless Database

Inside AWS Console go to RDS and create a new Database. AWS RDS create 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. AWS RDS database features

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. AWS RDS database connectivity

After that click on Create Database and wait until the creation process ends. AWS RDS database list

Click on DB Identifier to see database details. Inside this page you will see the instructions to connect to your database. AWS RDS database details

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

Allow container to access the database

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:

AWS RDS database details security group AWS RDS database details security group

After that your container will have the grants to access the database.

Contributing

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.

About

Sample project with a very simple API build with Django Rest Framework to illustrate the use of AWS Fargate and Aurora Serverless with postgres compatibility.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published