DEV Community

AWS Copilot Deployment Tutorial: From Monolith to Microservice

This tutorial (for Mac) will guide you through deploying a Node.js application using AWS Copilot, first as a monolith and then breaking it down into microservices. By following these steps, you'll learn how to use AWS Copilot to deploy containerized applications to Amazon ECS.
I will leave a doc with all systems contemplated. Link: All Systems (Linux & Windows)

Image description

Tutorial Modules Overview

  1. Introduction: Understanding the application and architecture
  2. Setup: Installing and configuring prerequisites
  3. Containerize and Deploy the Monolith: Building and deploying the application as a monolith
  4. Break the Monolith: Separating the application into microservices
  5. Deploy Microservices: Deploying each microservice independently
  6. Clean Up: Removing all resources to avoid ongoing charges

Prerequisites

Before starting this tutorial, ensure you have the following:
  1. AWS Account: You need an active AWS account. Some services may require your account to be active for more than 12 hours after creation.
  2. Docker: Docker must be installed and running on your system.

For macOS:

Install Docker Desktop for Mac

Download from https://www.docker.com/products/docker-desktop/
Enter fullscreen mode Exit fullscreen mode

Verify Docker installation

docker --version
Enter fullscreen mode Exit fullscreen mode

Image description

Install AWS CLI v2 (for macOS)

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /
Enter fullscreen mode Exit fullscreen mode

Verify installation

aws --version
Enter fullscreen mode Exit fullscreen mode

Image description

Configure AWS credentials

aws configure
Enter fullscreen mode Exit fullscreen mode

Image description

AWS Copilot CLI: Install the AWS Copilot CLI

Install via Homebrew (macOS)

brew install aws/tap/copilot-cli
Enter fullscreen mode Exit fullscreen mode

Verify installation

copilot --version
Enter fullscreen mode Exit fullscreen mode

Image description

Part 1: Deploying the Monolithic Application

Step 1: Create an AWS Copilot Application

An AWS Copilot Application is a collection of services and environments.

# Clone the sample repository
git clone https://github.com/awslabs/amazon-ecs-nodejs-microservices
cd ./amazon-ecs-nodejs-microservices/

# Initialize a new Copilot application
copilot app init
# Name the application "api" when prompted
Enter fullscreen mode Exit fullscreen mode

Image description

Step 2: Create the Environment

Copilot environments represent the infrastructure where your application runs.

# Create a new environment
copilot env init
Enter fullscreen mode Exit fullscreen mode

When prompted:
Name the environment "api"
Choose "profile default" for credentials
Select "Yes, use default" for environment configuration

Image description

Step 3: Deploy the Environment

# Deploy the environment
copilot env deploy --name api
Enter fullscreen mode Exit fullscreen mode

This step creates the VPC, subnets, security groups, and other necessary infrastructure for your application.

Image description

Step 4: Create the Monolith AWS Copilot Service

A Copilot Service runs containers on ECS or Fargate.

# Create a new service
copilot svc init
Enter fullscreen mode Exit fullscreen mode

When prompted:
Select "Load Balanced Web Service" as the workload type
Name the service "monolith"
For the Dockerfile path, select or enter the appropriate path to the Dockerfile

Image description

Step 5: Deploy the Monolith Service

# Deploy the API
copilot env deploy --name api
Enter fullscreen mode Exit fullscreen mode

Image description

Deploy the service

copilot svc deploy --name monolith
Enter fullscreen mode Exit fullscreen mode

Image description

This builds the Docker container locally, pushes it to an Amazon Elastic Container Registry (ECR) repository, and deploys it on Amazon Elastic Container Service (ECS).

Image description

Image description

Step 6: Confirm the Deployment

After deployment completes, AWS Copilot will display a URL for your service. You can test the deployment by accessing the URL in a browser or using curl.
You can test the application deployment by entering queries in a browser, such as:

  • http:///api-ap-Publi-tqekqzwAKyhA-2052337664.us-east-1.elb.amazonaws.com/api/users/3 Image description
  • http:///api-ap-Publi-tqekqzwAKyhA-2052337664.us-east-1.elb.amazonaws.com/api/threads/2 Image description
  • http:///api-ap-Publi-tqekqzwAKyhA-2052337664.us-east-1.elb.amazonaws.com/api/posts/ Image description
  • http:///api-ap-Publi-tqekqzwAKyhA-2052337664.us-east-1.elb.amazonaws.com/api/posts/in-thread/1 Image description

Part 2: Breaking the Monolith into Microservices

Step 1: Create the Microservices

Create separate services for posts, threads, and users components.

Create the posts service (copy all the script up to Load Balanced Web Service)

copilot svc init --app api --dockerfile ./3-microservices/services/posts/Dockerfile --name posts --svc-type "Load Balanced Web Service"
Enter fullscreen mode Exit fullscreen mode

Image description

Create the threads service (copy all the script up to Load Balanced Web Service)

copilot svc init --app api --dockerfile ./3-microservices/services/threads/Dockerfile --name threads --svc-type "Load Balanced Web Service"
Enter fullscreen mode Exit fullscreen mode

Image description

Create the users service (copy all the script up to Load Balanced Web Service)

copilot svc init --app api --dockerfile ./3-microservices/services/users/Dockerfile --name users --svc-type "Load Balanced Web Service"
Enter fullscreen mode Exit fullscreen mode

Image description

Step 2: Edit the Path in the Manifest Files

AWS Copilot sets the path to a service based on the service name. Edit each microservice manifest to ensure proper routing.

Edit the posts service manifest

nano copilot/posts/manifest.yml
Enter fullscreen mode Exit fullscreen mode

Update the path under the http section to:

path: "api/posts"
Enter fullscreen mode Exit fullscreen mode

Image description

Repeat for the threads and users services, updating their respective paths to:

For threads service

nano copilot/threads/manifest.yml
Enter fullscreen mode Exit fullscreen mode

Edit to:

path: "api/threads"
Enter fullscreen mode Exit fullscreen mode

Image description

For users service

nano copilot/users/manifest.yml
Enter fullscreen mode Exit fullscreen mode

Edit to:

path: "api/users"
Enter fullscreen mode Exit fullscreen mode

Image description

Step 3: Deploy the Microservices

Deploy the posts service

copilot svc deploy --name posts
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Your service is accessible at http://api-ap-Publi-tqekqzwAKyhA-2052337664.us-east-1.elb.amazonaws.com/api/posts over the internet.

Deploy the threads service

copilot svc deploy --name threads
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Your service is accessible at http://api-ap-Publi-tqekqzwAKyhA-2052337664.us-east-1.elb.amazonaws.com/api/threads over the internet.

Deploy the users service

copilot svc deploy --name users
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Your service is accessible at http://api-ap-Publi-tqekqzwAKyhA-2052337664.us-east-1.elb.amazonaws.com/api/users over the internet.

Step 4: Shut Down the Monolith

Once all microservices are deployed and working, you can shut down the monolith service.

Delete the monolith service

copilot svc delete --name monolith
Enter fullscreen mode Exit fullscreen mode

Image description

Step 5: Verify the Deployment

Test the deployment by accessing the endpoints:

Image description

Image description

Image description

Image description

Environment on AWS:

  • VPC:

Image description

  • ECS:

Image description

Image description

  • ELB:

Image description

Image description

Step 6: Clean Up Resources

Best practice is to delete resources when the tutorial is complete to avoid ongoing charges for running services. This cleanup process will terminate all the resources you created during this tutorial.

Step 1: Delete the Application

To delete the entire application and all associated resources:

Delete all resources

copilot app delete --name api
Enter fullscreen mode Exit fullscreen mode

When you run this command:

  • AWS Copilot will list all the resources that will be deleted
  • You will be asked to confirm the deletion

The command will delete:

  • All services running on Amazon ECS
  • The Application Load Balancer
  • Amazon Elastic Container Registry repositories
  • AWS CloudFormation stacks
  • Underlying EC2 instances This process takes approximately 10 minutes to complete.

Resources Being Deleted
This cleanup will remove:

  • Amazon Elastic Container Service resources
  • Amazon Elastic Container Registry repositories
  • Elastic Load Balancer
  • Associated CloudFormation stacks
  • IAM roles created for your services
  • CloudWatch logs
  • Any other resources created by AWS Copilot

Image description

Thank you a lot! See you soon. I hope you enjoy it!

Top comments (0)