Deploying Django to AWS ECS with Terraform

In this tutorial, we’ll look at how to deploy a Django app to AWS ECS with Terraform.

Dependencies:

  1. Django v3.1
  2. Docker v19.03.12
  3. Python v3.8.5
  4. Terraform v0.13.0

Objectives

By the end of this tutorial, you will be able to:

  1. Explain what Terraform is and how you can use it to write infrastructure as code
  2. Utilize the ECR Docker image registry to store images
  3. Create the required Terraform configuration for spinning up an ECS cluster
  4. Spin up AWS infrastructure via Terraform
  5. Deploy a Django app to a cluster of EC2 instances manged by an ECS Cluster
  6. Use Boto3 to update an ECS Service
  7. Configure AWS RDS for data persistence
  8. Create an HTTPS listener for an AWS load balancer

Terraform

Terraform is an infrastructure as code (IaC) tool used for building, changing, and versioning infrastructure through code. It uses a high-level declarative configuration language that lets you describe the desired state of your cloud or on-prem infrastructure for running an application. Think of it as the single source of truth for your infrastructure, which makes it easy to create, update, and delete resources safely and efficiently. After describing the end state of your infrastructure, Terraform generates a plan and then executes it – e.g., provision and spin up the necessary infrastructure.

If you’re new to Terraform, review the Introduction to Terraform article and go through the Getting Started guide.

In this tutorial, using Terraform, we’ll develop the high-level configuration files required to deploy a Django application to ECS. Once configured, we’ll run a single command to set up the following AWS infrastructure:

  • Networking:
  • VPC
  • Public and private subnets
  • Routing tables
  • Internet Gateway
  • Key Pairs
  • Security Groups
  • Load Balancers, Listeners, and Target Groups
  • IAM Roles and Policies
  • ECS:
  • Task Definition (with multiple containers)
  • Cluster
  • Service
  • Launch Config and Auto Scaling Group
  • RDS
  • Health Checks and Logs

Amazon’s Elastic Container Service (ECS) is a fully managed container orchestration platform that’s used to manage and run containerized applications on clusters of EC2 instances.

If you’re new to ECS, it’s recommended to experiment with it in the web console first. Rather than configuring all the underlying network resources, IAM roles and policies, and logs manually, let ECS create these for you. You’ll just need to set up ECS, a Load Balancer, Listener, Target Group, and RDS. Once you feel comfortable then move on to an infrastructure as code tool like Terraform.

Architecture diagram:

AWS Architecture Diagram

Project Setup

Let’s start by setting up a quick Django project.

Create a new project directory along with a new Django project:

$ mkdir django-ecs-terraform && django-ecs-terraform
$ mkdir app && cd app
$ python3.8 -m venv env
$ source env/bin/activate

(env)$ pip install django==3.1
(env)$ django-admin.py startproject hello_django .
(env)$ python manage.py migrate
(env)$ python manage.py runserver

Navigate to http://localhost:8000/ to view the Django welcome screen. Kill the server once done, and then exit from the virtual environment. Go ahead and remove it as well. We now have a simple Django project to work with.

Add a requirements.txt file:

Django==3.1
gunicorn==20.0.4

Add a Dockerfile as well:

## pull official base image
FROM python:3.8.5-slim-buster

## set work directory
WORKDIR /usr/src/app

## set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

## install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

## copy project
COPY . .

For testing purposes, set DEBUG to True and allow all hosts in the settings.py file:

DEBUG = True

ALLOWED_HOSTS = ['*']

Next, build and tag the image and spin up a new container:

$ docker build -t django-ecs .

$ docker run \
    -p 8007:8000 \
    --name django-test \
    django-ecs \
    gunicorn hello_django.wsgi:application --bind 0.0.0.0:8000

Ensure you can view the welcome screen again at http://localhost:8007/.

Stop and remove the container once done:

$ docker stop django-test
$ docker rm django-test

Add a .gitignore file to the project root:

__pycache__
.DS_Store
*.sqlite3

Your project structure should now look like this:

├── .gitignore
└── app
    ├── Dockerfile
    ├── hello_django
    │   ├── __init__.py
    │   ├── asgi.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── manage.py
    └── requirements.txt

For a more detailed look at how to containerize a Django app, review the Dockerizing Django with Postgres, Gunicorn, and Nginx blog post.

#django

What is GEEK

Buddha Community

Deploying Django to AWS ECS with Terraform
Ahebwe  Oscar

Ahebwe Oscar

1620177818

Django admin full Customization step by step

Welcome to my blog , hey everyone in this article you learn how to customize the Django app and view in the article you will know how to register  and unregister  models from the admin view how to add filtering how to add a custom input field, and a button that triggers an action on all objects and even how to change the look of your app and page using the Django suit package let’s get started.

Database

Custom Titles of Django Admin

Exclude in Django Admin

Fields in Django Admin

#django #create super user django #customize django admin dashboard #django admin #django admin custom field display #django admin customization #django admin full customization #django admin interface #django admin register all models #django customization

Ahebwe  Oscar

Ahebwe Oscar

1620185280

How model queries work in Django

How model queries work in Django

Welcome to my blog, hey everyone in this article we are going to be working with queries in Django so for any web app that you build your going to want to write a query so you can retrieve information from your database so in this article I’ll be showing you all the different ways that you can write queries and it should cover about 90% of the cases that you’ll have when you’re writing your code the other 10% depend on your specific use case you may have to get more complicated but for the most part what I cover in this article should be able to help you so let’s start with the model that I have I’ve already created it.

**Read More : **How to make Chatbot in Python.

Read More : Django Admin Full Customization step by step

let’s just get into this diagram that I made so in here:

django queries aboutDescribe each parameter in Django querset

we’re making a simple query for the myModel table so we want to pull out all the information in the database so we have this variable which is gonna hold a return value and we have our myModel models so this is simply the myModel model name so whatever you named your model just make sure you specify that and we’re gonna access the objects attribute once we get that object’s attribute we can simply use the all method and this will return all the information in the database so we’re gonna start with all and then we will go into getting single items filtering that data and go to our command prompt.

Here and we’ll actually start making our queries from here to do this let’s just go ahead and run** Python manage.py shell** and I am in my project file so make sure you’re in there when you start and what this does is it gives us an interactive shell to actually start working with our data so this is a lot like the Python shell but because we did manage.py it allows us to do things a Django way and actually query our database now open up the command prompt and let’s go ahead and start making our first queries.

#django #django model queries #django orm #django queries #django query #model django query #model query #query with django

Ahebwe  Oscar

Ahebwe Oscar

1631810833

How to Deploy Django to AWS ECS with Terraform 2021 (Step by Step)

Learn How to Deploy Django to AWS ECS with Terraform 2021 (Step by Step)

By the end of this tutorial, you will be able to:

  1. Explain what Terraform is and how you can use it to write infrastructure as code
  2. Utilize the ECR Docker image registry to store images
  3. Create the required Terraform configuration for spinning up an ECS cluster
  4. Spin up AWS infrastructure via Terraform
  5. Deploy a Django app to a cluster of EC2 instances manged by an ECS Cluster
  6. Use Boto3 to update an ECS Service
  7. Configure AWS RDS for data persistence
  8. Create an HTTPS listener for an AWS load balancer

#django #aws #terraform 

Rory  West

Rory West

1619263860

Why Terraform? How to Getting Started with Terraform Using AWS

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

Traditional Infrastructure vs Modern Infrastructure

Traditional Infrastructure

  • Mutable
  • Operational Complexity
  • No Central Control on Infrastructure

Modern Infrastructure

  • Immutable
  • Less Operational Complexity
  • Faster time to the market
  • single point for state management

#terraform-aws #terraform #aws #aws-ec2

Rory  West

Rory West

1620959460

Complete Guide to Terraform AWS

We’re continuing our series on Terraform AWS with a post that breaks down the basics. The world of Terraform AWS can be described as complex — from AWS storage to AWS best practices, there’s a depth of knowledge necessary to get familiar with Terraform AWS.

Whether you’re an expert at Terraform AWS or just getting started, it’s our goal at InfraCode to provide you with clear and easy-to-understand information at every level. The number of resources out there is abundant but overwhelming. That’s why we create simplified guides that are immediately usable and always understandable.

In this article, we’ll dive into:

  • A Beginner’s Overview to Terraform AWS
  • Managing AWS Storage
  • Terraform AWS Best Practices

#aws-ec2 #aws #terraform #terraform aws