Smooth Sailing from AWS EC2 to ECS: A Comprehensive Migration Guide
Tags: aws, ecs, docker, migration
Published: 2023-12-04 09:54:29

## Introduction

Having an application running behind a Load Balancer with Auto Scaling policies is already a good choice.

With the [migration from Bare metal to AWS EC2](https://dev.to/lpossamai/from-bare-metal-to-aws-cloud-in-90-minutes-2b2b) and the [upgrade of our main database from PostgreSQL 9.2 to PostgreSQL 14](https://dev.to/lpossamai/postgresql-live-migration-from-92-to-14-with-bucardo-32n9), we were ready to the next challenge.

### Current architecture

![current aws architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4h6904ddh6rnrv3uultq.png)

## Why Migrate to ECS?

ECS brings many benefits. Better integration with other AWS services, simplified container management and improved scalability and resource optimization are some of them.

In our case, we were using [Bamboo](https://www.atlassian.com/software/bamboo) as our CI solution to deploy application changes to our AWS environments. Each build would take around 50 minutes to complete. Imagine you're a developer and we wanted to test a change in the Test environment. You'd have to wait for ~50 minutes to see if your change succeeded or not. No, there was no local development solution in place either.

![bamboo build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wahu21w1o093kb21gyvd.png)

## Choose your weapons!

### Infrastructure as Code

There are many IaC (Infrastructure as Code) solutions out there. We decided to go with [Terraform](https://www.terraform.io/), as it's easier to manage and to find engineers who have work experience with it.

<aside class="notice">
💡 **Tip:** Using Terraform community modules such as [terraform-aws-modules](https://github.com/terraform-aws-modules) and [cloudposse](https://github.com/cloudposse) may help you speed up the creation of those resources.
</aside>

Github Actions will connect to our AWS Accounts using an [OIDC connection](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services).

![github actions with terraform](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6h9br32jdgwg94kex9s.png)

#### Terraform state file

Managing the state of your IaC can be tricky if you have other team members creating, updating and deleting resources at the same time as you are.

Terraform has a feature that allows you to [manage the state of your infrastructure using an AWS S3 bucket and DynamoDB tables](https://spacelift.io/blog/terraform-state). This is a huge improvement for us given the fact we had resources in CloudFormation. 

### CI/CD

Terraform alone is of no help. You must have a good CI solution to deploy your infrastructure changes.

We've opted to go with [Github Actions](https://github.com/features/actions), as the business was already using Github for other repositories.

![github actions workflow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fkxeh0quii4tfd09kd9z.png)

## Conclusion

Our cluster has been handling the traffic really well.

![ecs metrics last 7 days](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnc47a3sclngf65wmwwo.png)

### Improved response time just by moving platform

#### Before ECS migration

![ecs new relic before migration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mo759wu39jif80g212x3.png)

#### After ECS migration

![ecs new relic after migration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/319npv45hyow83axwqxv.png)

#### Our new AWS architecture

![aws new architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3s6okpkevlxgr8w2x5q2.png)