Git Product home page Git Product logo

django-ecs-terraform's Introduction

Deploying Django to AWS ECS with Terraform

Sets 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
  • Auto scaling config
  • RDS
  • Health Checks and Logs

Want to learn how to build this?

Check out the tutorial.

Want to use this project?

  1. Install Terraform

  2. Sign up for an AWS account

  3. Create two ECR repositories, django-app and nginx.

  4. Fork/Clone

  5. Build the Django and Nginx Docker images and push them up to ECR:

    $ cd app
    $ docker build -t <AWS_ACCOUNT_ID>.dkr.ecr.us-west-1.amazonaws.com/django-app:latest .
    $ docker push <AWS_ACCOUNT_ID>.dkr.ecr.us-west-1.amazonaws.com/django-app:latest
    $ cd ..
    
    $ cd nginx
    $ docker build -t <AWS_ACCOUNT_ID>.dkr.ecr.us-west-1.amazonaws.com/nginx:latest .
    $ docker push <AWS_ACCOUNT_ID>.dkr.ecr.us-west-1.amazonaws.com/nginx:latest
    $ cd ..
  6. Update the variables in terraform/variables.tf.

  7. Set the following environment variables, init Terraform, create the infrastructure:

    $ cd terraform
    $ export AWS_ACCESS_KEY_ID="YOUR_AWS_ACCESS_KEY_ID"
    $ export AWS_SECRET_ACCESS_KEY="YOUR_AWS_SECRET_ACCESS_KEY"
    
    $ terraform init
    $ terraform apply
    $ cd ..
  8. Terraform will output an ALB domain. Create a CNAME record for this domain for the value in the allowed_hosts variable.

  9. To apply the migrations, run the following command, making sure to replace YOUR_SUBNET_1, YOUR_SUBNET_2, and YOUR_SECURITY_GROUP with the values that were outputted to your terminal from the terraform apply command:

    $ aws ecs run-task \
        --cluster production-cluster \
        --task-definition django-migration-task \
        --launch-type FARGATE \
        --network-configuration "awsvpcConfiguration={subnets=[YOUR_SUBNET_1, YOUR_SUBNET_2],securityGroups=[YOUR_SECURITY_GROUP],assignPublicIp=ENABLED}"
  10. Now you can open https://your.domain.com/admin. Note that http:// won't work.

  11. To collect the static files, navigate to the "deploy" folder, create and activate a Python virtual environment, install the requirements, and then run the following command, making sure to replace <AWS_ACCOUNT_ID> with your AWS account ID:

    (env)$ python update-ecs.py \
            --cluster=production-cluster \
            --service=production-service \
            --image="<AWS_ACCOUNT_ID>.dkr.ecr.us-west-1.amazonaws.com/django-app:latest" \
            --container-name django-app

    You can use the same command to bump the Task Definition and update the Service.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.