Break a Monolithic Application Into Microservices With AWS Migration Hub Refactor Spaces, AWS Copilot
AWS lets you decompose a monolith and focus on innovation.
Step 2: Download and Open the Project
Module Two: Containerize and Deploy the Monolith
Dependency Control and Improved Pipeline
Density and Resource Efficiency
What You Will Accomplish in Module Two
Step 1: Create an AWS Copilot Application
Step 2: Create the Environment
Step 3: Deploy the Environment
Step 4: Create the Monolith AWS Copilot Service
Step 5: Confirm the Deployment
Module Three: Deploy the Refactor Environment
What You Will Accomplish in Module Three
Step 2: Deploy Refactor Spaces
Module Four: Break the Monolith
What You Will Accomplish in Module Four
Step 1: Create the Environments
Step 3: Edit the Path in the manifest.yml for Each Microservice
Module Five: Deploy Microservices
What You Will Accomplish in Module Five
Step 1: Deploy the Microservices
Step 2: Register Your Microservice With Refactor Spaces
Step 3: Shut Down the Monolith
Step 4: Test Your Microservices
What You Will Accomplish in Module Six
Step 1: Delete the Application
strangler fig
pattern, leave and layer
pattern, or refactor using a multi-account strategy
. Each of these approaches helps your business to improve the application efficacy by reducing the change risk for the application consumers.About | |
---|---|
✅ AWS Level | Intermediate - 200 |
⏱ Time to complete | 140 minutes |
🧩 Prerequisites | - An AWS account: If you don't already have an account, follow the Setting Up Your AWS Environment tutorial for a quick overview. - Install and configure the AWS CLI. - Install and configure AWS Copilot. - Install and configure Docker. - A text editor. For this tutorial, we will use VS Code, but you can use your preferred IDE. |
💻 Code Sample | - Code sample for application on GitHub - AWS CloudFormation scripts for Refactor Services on AWS Samples |
📢 Feedback | Any feedback, issues, or just a 👍 / 👎 ? |
⏰ Last Updated | 2023-11-16 |
- An AWS account: If you don't already have an account, follow the Setting Up Your AWS Environment tutorial for a quick overview.
- Install and configure the AWS CLI.
- Install and configure AWS Copilot.
- Install and configure Docker.
- A text editor. For this tutorial, we will use VS Code, but you can use your preferred IDE.
- Check that sufficient quota is available for all required services. For example, this tutorial uses five Virtual Private Clouds (VPCs) and there is a default quota of five VPCs per Region.
- Setup (20 minutes): In this module, you will install and configure the AWS CLI, install AWS Copilot, and install Docker.
- Containerize and deploy the monolith (30 minutes): In this module, you will containerize the application. You will instantiate a managed cluster of Fargate on Amazon ECS compute instances using AWS Copilot. You will also deploy your image as a container running on the cluster.
- Deploy the refactor environment (20 minutes): In this module, you will deploy a Refactor Spaces environment. This will set up the infrastructure to incrementally refactor your application. You will then register the monolith from the previous step as a default route in Refactor Spaces.
- Break the monolith (20 minutes): In this module, you will break the Node.js application into several interconnected services. Then you will push each service's image to an Amazon Elastic Container Registry (Amazon ECR) repository.
- Deploy microservices (30 minutes): In this module, you will deploy your Node.js application as a set of interconnected services behind an Application Load Balancer. Then, you will use Refactor Spaces to re-route traffic from the monolith to the microservices.
- Clean up (10 minutes): In this module, you will terminate the resources you created during the tutorial. You will stop the services running on Amazon ECS, delete the Application Load Balancer, and delete the AWS CloudFormation stack to terminate all underlying resources.
- ⏱ Time to complete: 20 minutes
- An AWS account: If you don't have an account with AWS, sign up here. All the exercises in this tutorial are designed to be covered under the AWS Free Tier. Note: Some of the services you will be using may require your account to be active for more than 12 hours. If you have a newly created account and encounter difficulty provisioning any services, wait a few hours and try again.
- Docker: You will use Docker to build the image files that will run as containers. Docker is an open-source project. You can download it for macOS or for Windows. After Docker is installed, verify it is running by entering Docker --version in the terminal. The version number should display, for example: Docker version 19.03.5, build 633a0ea.
- AWS CLI:
- You will use the AWS Command Line Interface (AWS CLI) to push the images to Amazon ECR. To learn about and download the AWS CLI, see Getting started with the AWS CLI.
- After AWS CLI is installed, verify it is running by entering aws --version in the terminal. The version number should display, for example:
aws-cli/1.16.217 Python/2.7.16 Darwin/18.7.0 botocore/1.12.207
. - If you already have AWS CLI installed, run the following command in the terminal to validate you are using the latest version:
pip install awscli --upgrade --user
. - If you have not used AWS CLI before, you can configure your credentials.
- AWS Copilot: AWS Copilot is an open-source command line interface that helps developers to build, release, and operate production-ready containerized applications. This can be done on AWS App Runner, Amazon ECS, and AWS Fargate. On macOS, you can use brew to install AWS Copilot.
- Developers build and run the container locally.
- Continuous integration server runs the same container and runs integration tests to make sure it passes expectations.
- The same container is shipped to a staging environment where its runtime behavior can be checked using load tests or manual QA.
- The same container is shipped to production.
- Client The client makes a request over port 443 to the refactor proxy URL.
- Elastic Load Balancer (ELB) AWS Copilot creates an ELB and registers the monolith to the target group.
- Containerized Node.js Monolith The Node.js cluster parent is responsible for distributing traffic to the workers within the monolithic application. This architecture is containerized, but still monolithic because each container has all the same features of the rest of the containers.
- ⏱ Time to complete: 30 minutes
copilot env init
and choose profile default
. Name the Environment monolith
.Yes, use default
.manifest.yml
that is used to configure the environment. Once the creation is done, you should see similar output to this:copilot env deploy --name monolith
in the terminal.copilot svc init
and choose Load Balanced Web Service
.monolith
.2-containerized/services/api/Dockerfile
./amazon-ecs-nodejs-microservices/copilot/monolith
and examine manifest.yml
to see how the service is configured. Note that the parameter http
defines the path for the app. The Node.js application, server.js
, defines the base route as /
.copilot svc deploy --name monolith
in the terminal. When you deploy the service, the container is built locally by Docker and pushed to your Elastic Container Registry. The service pulls the container from the registry and deploys it in the environment. The monolith
application is running when the deployment is complete.- Reducing the time to set up a refactor environment.
- Reducing the complexity for iteratively extracting capabilities as new microservices and re-routing traffic.
- Simplifying management of existing apps and microservices as a single application with flexible routing control, isolation, and centralized management.
- Helping dev teams achieve and accelerate tech and deployment independence by simplifying development, management, and operations while apps are changing.
- Simplifies refactoring to multiple AWS accounts. Refer to the following architecture reference for additional details.
- Client The client makes a request over port 443 to the refactor proxy URL.
- AWS Migration Hub Refactor Spaces Refactor Spaces provides an application that models the Strangler Fig pattern for incremental refactoring.
- Elastic Load Balancer (ELB) AWS Copilot creates an ELB and registers the monolith to the target group.
- Containerized Node.js Monolith The Node.js cluster parent is responsible for distributing traffic to the workers within the monolithic application. This architecture is containerized, but still monolithic because each container has all the same features of the rest of the containers.
- ⏱ Time to complete: 30 minutes
rs.yaml
and rs-service-op.yaml
files into the repository that you downloaded in Module 1. You can also do this with curl
/ Invoke-WebRequest
without cloning the whole repository:- Run the following command at the root directory for this project to deploy a refactor environment. Replace the
<<Stack Name>>
with a name of your choice, and<<MonolithUrl>>
with the Copilot CLI output from the last module with/api
appended since the monolith listens on the/api
. For example,[http://api-m-Publi-5SPO2C-558916521.us-east-1.elb.amazonaws.com/api](http://api-m-Publi-5SPOLPJTUB2C-558916521.us-east-1.elb.amazonaws.com/api)
Successfully created/updated stack - monolith-tutorial
.rsProxyURL
value from the output above, append /users
, or /threads
, or /posts
and paste into a web browser. The following screenshot is from Firefox optimized to see JSON format.- Client The client makes traffic requests over port 80.
- Load Balancer The Application Load Balancer routes external traffic to the correct service. The Application Load Balancer inspects the client request and uses routing rules to direct the request to an instance and port for the target group.
- Target Groups Each service has a target group that tracks the instances and ports of each container running for that service.
- Microservices Amazon ECS deploys each service into a container across an EC2 cluster. Each container only handles a single feature.
- ⏱ Time to complete: 20 minutes
api
Application. You can reuse the application to deploy new environments for the microservices. In this module, the code for the application has been divided into three microservices: posts, threads, and users. Each one will be deployed as a service in a new environment. The monolith
environment and service was deployed using the CLI menu. In this module, you can deploy services by specifying the flags for the copilot env init
command. Create the posts
microservice first.Yes, use default
.copilot env deploy —-name posts
, you will see similar output to this:users
, threads
environments.Posts
using the flags.users
, threads
microservices.server.js
is api/<<service name>>
. Edit the path in each microservice manifest, and add api/
to the path.- Switch the Traffic This is the starting configuration. The monolithic Node.js app running in a container on Amazon ECS.
- Deploy Microservices Using the three container images built and pushed to Amazon ECR by AWS Copilot, you will deploy three microservices.
- Register with Refactor Spaces Register the microservices' load balancer DNS with Refactor Spaces to route traffic from the monolith transparently.
- Shut down the Monolith Shut down the monolith by deleting the Copilot environment. Refactor Spaces routes traffic to the running microservices for the respective endpoints.
- ⏱ Time to complete: 20 minutes
copilot svc deploy --name <microservice> --env <microservice>
. Similar to the monolith service
, AWS Copilot builds a container for the microservice, pushes to a repository, and deploys on an Amazon ECS cluster running on Fargate.Users
and Threads
microservices- To register service and create a route in Refactor Spaces, replace the
ApplicationID
andEnvironmentId
parameters with appropriate values from the output of Module 3. Then pass theApplication Load Balancer URLs
for all three services from the Copilot commands in the previous step. - This command registers all three microservices deployed previously and registers them as services with Refactor Spaces.
monolith
Service. Enter copilot svc delete --name monolith
in the terminal.- ⏱ Time to complete: 20 minutes
<<stackname>>
to the following command.Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.