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
- Launch Config and Auto Scaling Group
- RDS
- Health Checks and Logs
Want to learn how to build this?
Check out the post.
Want to use this project?
-
Install Terraform
-
Sign up for an AWS account
-
Fork/Clone
-
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 -t <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/django-app:latest . $ docker push -t <AWS_ACCOUNT_ID>.dkr.ecr.us-west-1.amazonaws.com/django-app:latest $ cd ..
-
Update the variables in terraform/variables.tf.
-
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 ..
-
You can also run the following script to bump the Task Definition and update the Service:
$ cd deploy $ python update-ecs.py --cluster=production-cluster --service=production-service