Going multi-tenant with aws ecs and terraform
Intro
This small tutorial will help you to setup up a basic environment to run your application in an docker container on the aws cloud in an ecs cluster and expose it through an load-balancer so that traffic can hit subdomains in the format:
https://*.<rootdomain> for example https://customer1.example.com
Load can be spread across multi containers / multi instances if needed. The setup is also multi AZ ready. You should have a basic understanding of terraform in case you need to adjust the templates provided in this tutorial.
What the article will not cover
The article wont cover how to make your application code multi tenant ready.
What the setup consists of
AWS
- load balancer
- route 53 zone entry
- ecr repository
- vpc
- ecs cluster
- ecs task
- cloud watch logs
- nat gateway
- amc (ssl certificate)
- …
Terraform
- s3 backed backend to store the terraform state
- outputs for an easy overview of the key variables
- easy specify your docker image in a variables.json file
Rough overview of the involved parts
What the setup provides
This setup is a cheap way of hosting docker containers on aws, you can start small with one t2.micro instance and scale up if needed. You are also free to add more ECS Tasks for example for an worker process. SSL certificates for your domain and the subdomains will be auto-renewed by AWS via ACM. The docker containers can be hosted in the ECR Registry, learning how to push to an ECR registry you can find here.
Code
You can find the source code: here