Best way to set up multiple dev 'environments' in ECS?

Hey all

Was wondering if anyone else currently does this in ECS. Looking to start using ECS, and I will need to be able to configure multiple ‘environments’ with different urls each with their own postgres/redis instance as well as custom environment variables.

I have done some research but there isn’t much clear and recent consensus on the best way to accomplish this. Would it be best to create totally separate clusters for each environment? We wouldn’t auto deploy through CI but I’d need a way to make a script that would update the container image and maybe some environment variables.

Thank you!

Parameterize as needed

https://github.com/1Strategy/fargate-cloudformation-example/blob/master/fargate.yaml

Only one command needed - aws cloudformation deploy

Could also just create a terraform module for your environment and then have the image tag and env vars added per dev / environment when you apply. When your done just destroy.

Or the same using a CDK stack with params

This is how I do it, except I use AWS Secrets for any sensitive env vars.

sorry i’m newish to terraform…could I create one module with all my vpcs/db/redid/ecs cluster and just somehow instantiate it multiple times with different config?

Yes

Yes you can. But I would be careful to separate your account setup and global (VPC, networking) from your app and database. For example I would personally provision my VPC with subnets, etc. in its own module then just pass those as parameters into the module for your ecs,db,redis module that you want to provision multiple times. There’s a lot of ways to do it but depending on your needs idk if you want that many VPCs and networking items being created if you don’t need it.

You absolutely can. Although im not sure you want a new vpc for every environment.

You can have a module which you use to create baseline infrastructure and then another more for your applications/dev environments which you use multiple times.

Choose one:

  1. environment by Namespace (same VPC, different names)
  2. environment by VPC (each in own VPC)
  3. environment by Account (each in own AWS Account)

more secure, but more complex, moving down

Then configure them using Parameter Store / Secrets Manager, which you can assign to environment variables in ECS

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/specifying-sensitive-data-parameters.html

If you use Fargate you can have multiple services with unique names running in one ECS cluster in a VPC. We’re planning on having multiple dev environments with the unique identifier being the Jira ticket ID in this kinda setup.

We recently did this .using terraform and workspaces on terraform .. it has worked very smoothly for us

You could create multiple AWS accounts, organized under one AWS organization, one account per environment. You can create as many accounts as you want for dev, sandbox, test, staging, production … whatever you need. It’s a bit complicated, but you get the value of true isolation. This approach costs you nothing monetarily, just a bit of keeping track of things. If you’re using an Infrastructure as Code solution (Terraform for example) then you can easily provision all your resources in as many accounts/environments as you like and easily tear down resources in the accounts you’re not actively using to avoid accruing charges.

If you find yourself needing other resources besides ECS, multiple accounts will solve that problem, too. Except for S3 buckets whose names need to be globally unique.

I can’t overstate this comes with complexity, but the benefits might be worth it.

This project deploys an ecs cluster for a django project that takes a variable to create a whole environment. I wrote it, thought it isn’t super well documented it should be pretty readable:

https://gitlab.com/uptimepros/uptimepros-oss/cdk-www-uptimepros

Command ends up looking like `inv deploy --environment=dev`. Setup to run from CI optionally.

I would suggest to use multiple accounts (one for each environment) and AWS CDK for deployment. You can setup a pipeline in each account and then associate GitHub branches such as develop, stage, and master, in GitHub actions to trigger pipeline in each corresponding environment. Full automation, just write your app code and enjoy. Just don’t forget to use non-overlapping CIDRs in each account VPC(s) in case you would ever want to connect those with VPC peering or Transit Gateway.

While VPCs are free, each one with a private subnet will need a NAT gateway to access the internet, which costs around $30/mo as a base.

I wrote an article about this and posted it here last week, here a link to that post: https://www.reddit.com/r/aws/comments/v9ycwh/my_approach_to_building_ad_hoc_developer/

Here’s the original article I wrote on my blog: https://briancaffey.github.io/2022/03/27/ad-hoc-developer-environments-for-django-with-aws-ecs-terraform-and-github-actions

I think my article will answer the different features you are looking for in your post:

- different urls
- postgres+ redis per environment
- custom environment variables per environment
- totally separate clusters for each environment? Yes.
- We wouldn’t auto deploy through CI - My example repo does this as a GitHub Action
- script that would update the container image: I also include a discussion of the script I made for doing this
- maybe some environment variables: I would try to keep all environment variables (that are not secrets/sensitive data) in *.tfvars files or TF_VAR_ values that you can set when you run your script to update the environment. To keep things simple, the script that runs the application update should ONLY change the image tag and env vars should be managed as infra, in my opinion.

Happy to answer any questions about this if you have any

I realize you wrote this two years ago, but this post is EXCELLENT. Thank you so much.

Thank you! Here is a more recent article on the same topic: https://briancaffey.github.io/2023/01/07/i-deployed-the-same-containerized-serverless-django-app-with-aws-cdk-terraform-and-pulumi