In this module, you will use Amazon Elastic Container Service (Amazon ECS) to instantiate a managed cluster of EC2 compute instances and deploy your image as a container running on the cluster. Start Building

architecture overview

a. Client
The client makes a request over port 80 to the load balancer.

b. Load Balancer
The load balancer distributes requests across all available ports.

c. Target Groups
Instances are registered in the application's target group.

d. Container Ports
Each container runs a single application process which binds the node.js cluster parent to port 80 within its namespace.

e. 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.

Amazon Elastic Container Service (Amazon ECS) is a highly scalable, high performance container management service that supports Docker containers and allows you to easily run applications on a managed cluster of Amazon EC2 instances. With simple API calls, you can launch and stop Docker-enabled applications, query the complete state of your cluster, and access many familiar features like security groups, Elastic Load Balancing, EBS volumes, and IAM roles.

You can use Amazon ECS to schedule the placement of containers across your cluster based on your resource needs and availability requirements. You can also integrate your own scheduler or third-party schedulers to meet business or application specific requirements.

There is no additional charge for Amazon ECS. You pay for the AWS resources (for example, EC2 instances or EBS volumes) you create to store and run your application.


Follow the step-by-step instructions below to deploy the node.js application using Amazon ECS. Select each step number to expand the section.

break-the-monolith
  • Step 1. Launch an ECS Cluster using AWS CloudFormation

    Create an Amazon ECS cluster deployed behind an Application Load Balancer.

    1. Navigate to the AWS CloudFormation console.
    2. Select Create stack.
    3. Select Upload a template file and choose the ecs.yml file from the GitHub project at amazon-ecs-nodejs-microservice/2-containerized/infrastructure/ecs.yml then select Next.
    4. For the stack name, enter BreakTheMonolith-Demo. Verify that the other parameters have the following values:
      1. Desired Capacity = 2
      2. InstanceType = t2.micro
      3. MaxSize = 2
    5. Select Next.
    6. On the Configure stack options page, keep the default options and scroll down and select Next.
    7. On the Review BreakTheMonolith-Demo page scroll to the bottom of the page, acknowledge the Capabilities statement by selecting the checkbox, and select Create stack.

    You will see your stack with the status CREATE_IN_PROGRESS. You can select the refresh button at the top right of the screen to check on the progress. This process typically takes under 5 minutes.

    create stack

    ⚐ NOTE: Optionally, you can use the AWS Command Line Interface (AWS CLI) to deploy AWS CloudFormation stacks. Run the following code in the terminal from the folder amazon-ecs-nodejs-microservices/3-microservices and replace [region] with your AWS Region.

    $ aws cloudformation deploy \
       --template-file infrastructure/ecs.yml \
       --region [region] \
       --stack-name BreakTheMonolith-Demo \
       --capabilities CAPABILITY_NAMED_IAM
  • Step 2. Check your Cluster is Running

    Amazon ECS Verify the Cluster
    • Select the cluster BreakTheMonolith-Demo, then select the Tasks tab to verify that there are no tasks running.
    Amazon ECS Cluster Tasks
    • Select the ECS Instances tab to verify there are two Amazon EC2 instances created by the AWS CloudFormation template.
      ⚐ Note: If you receive a message that the ECS agent is outdated, select Learn more for instructions to update the ECS agent.
    Amazon ECS Instances
  • Step 3. Write a Task Definition

    Task definitions specify how Amazon ECS deploys the application containers across the cluster.

    • From the Amazon ECS left navigation menu, select Task Definitions.
    • Select Create new Task Definition.
    • On the Select launch type compatibility page, select the EC2 option then select Next step.
    • On the Configure task and container definitions page, do the following:
      • In the Task Definition Name field, enter api.
      • Scroll down to Container Definitions and select Add container.
      • In the Add container window:
        • Parameters that are not defined can be either left blank or with the default settings.
        • In the Container name field, enter api.
        • In the Image field, enter [account-ID].dkr.ecr.[region].amazonaws.com/api:v1
          Replace [account-ID] and [region] with your specific information. Ensure the tag v1 matches the value you used in Module 1 to tag and push the image. This is the URL of your ECR repository image that was created in the previous module.
        • In the Memory Limits field, verify Hard limit is selected and enter 256 as the value.
        • Under Port mappings, Host port = 0 and Container port = 3000.
        • Scroll to ENVIRONMENT, CPU units = 256.
    • Select Add.
      You will return to the Configure task and container definitions page.
    • Scroll to the bottom of the page and select Create.

    Your Task Definition is listed in the console.

    task definition
  • Step 4. Configure the Application Load Balancer: Target Group

    The Application Load Balancer (ALB) lets your service accept incoming traffic. The ALB automatically routes traffic to container instances running on your cluster using them as a target group.

    Check your VPC Name: If this is not your first time using this AWS account, you may have multiple VPCs. It is important to configure your Target Group with the correct VPC.

    • Navigate to the Load Balancer section of the EC2 Console.
    • Locate the appropriate load balancer.
      ⚐ Note: The load balancer name is based on the stack name you provided when you deployed the AWS CloudFormation template. If you used the suggested name BreakTheMonolith-Demo, then the load balancer name may start with Break.
    • Select the checkbox next to the appropriate load balancer to see the Load Balancer details.
    • In the Description tab, locate the VPC attribute (in this format: vpc-xxxxxxxxxxxxxxxxx).
      ⚐ Note: You will need the VPC attribute in the next step when you configure the ALB target group.
    vpc attribute

    Configure the ALB Target Group

    • Navigate to the Target Group section of the EC2 Console.
    • Select Create target group.
    • Configure the following Target Group parameters (for the parameters not listed below, keep the default values):
      • For the Target group name, enter api.
      • For the Protocol, select HTTP.
      • For the Port, enter 80.
      • For the VPC, select the value that matches the one from the Load Balancer description. This is most likely NOT your default VPC.
      • Access the Advanced health check settings and edit the following parameters as needed: 
        • For Healthy threshold, enter 2.
        • For Unhealthy threshold, enter 2.
        • For Timeout, enter 5.
        • For Interval, enter 6.
    • Select Create.
    create target groups
  • Step 5. Configure the Application Load Balancer: Listener

    The ALB listener checks for incoming connection requests to your ALB.

    Add a Listener to the ALB

    • Navigate to the Load Balancer section of the EC2 Console.
    • Select the checkbox next to the appropriate load balancer to see the Load Balancer details.
    • Select the Listeners tab.
    • Select Add listener and edit the following parameters as needed:
      • For Protocol:port, select HTTP and enter 80.
      • For Default action(s), select Forward to and in the Target group field, enter api.
    • Select Save.
    listener to ALB
  • Step 6. Deploy the Monolith as a Service

    Deploy the monolith as a service into the cluster.

    • Navigate to the Amazon ECS console and select Clusters from the left menu bar.
    • Select the cluster BreakTheMonolith-Demo, select the Services tab then select Create.
    • On the Configure service page, edit the following parameters (and keep the default values for parameters not listed below): 
      • For the Launch type, select EC2.
      • For the Service name, enter api
      • For the Number of tasks, enter 1.
      • Select Next step.
    • On the Configure network page, Load balancing section, select Application Load Balancer.
      Additional parameters will apear: Service IAM role and Load balancer name.
      • For the Service IAM role, select BreakTheMonolith-Demo-ECSServiceRole.
      • For the Load balancer name, verify that the appropriate load balancer is selected.
    • In the Container to load balance section, select Add to load balancer.
      Additional information labeled api:3000 is shown.
    • In the api:3000 section, do the following:
      • For the Production listener port field, select 80:HTTP.
      • For the Target group name, select your group: api.
    • In the Service discovery section, Enable service discovery integration option, clear the checkmark. This option should not be enabled.
    • Select Next step.
    • On the Set Auto Scaling page, leave the default setting and select Next step.
    • On the Review page, review the settings then select Create Service.
    • After the service has been created, select View Service.
    Amazon ECS Service API

    Nice work! You now have a running service. It may take a minute for the container to register as healthy and begin receiving traffic.

  • Step 7. Test your Monolith

    Validate your deployment by checking if the service is available from the internet and pinging it.

    To Find your Service URL:

    • Navigate to the Load Balancers section of the EC2 Console.
    • Select the appropriate load balancer.
    • In the Description tab, copy the DNS name and paste into a new browser tab or window.
    • You should see the message Ready to receive requests.


    See Each Part of the Service:
    The node.js application routes traffic to each worker based on the URL. To see a worker, simply add the worker name api/[worker-name] to the end of the DNS Name as follows:

    • http://[DNS name]/api/users
    • http://[DNS name]/api/threads
    • http://[DNS name]/api/posts

    You can also add a record number at the end of the URL to drill down to a particular record. For example: http://[DNS name]/api/posts/1 or http://[DNS name]/api/users/2

    user demo