Deploy Java microservices on Amazon ECS using Amazon ECR and load balancing - AWS Prescriptive Guidance

Deploy Java microservices on Amazon ECS using Amazon ECR and load balancing

R Type: N/A

Source: Java

Target: Amazon ECS

Created by: AWS

Environment: PoC or pilot

Technologies: Web & mobile apps; Containers & microservices

AWS services: Amazon ECS

Summary

This pattern outlines steps for deploying a containerized Java microservices architecture on Amazon Elastic Container Service (Amazon ECS) to make it easier to scale and faster to develop your applications. This helps enable innovation and accelerates time-to-market for new features. 

The pattern also uses Amazon Elastic Container Registry (Amazon ECR) to store and manage the Docker-based containers, and an AWS CloudFormation template with a Python script to automate the setup of your infrastructure. The pattern is based on the post Deploying Java Microservices on Amazon Elastic Container Service, which is published on the AWS Compute blog.

Microservices provide an architectural and organizational approach to software development, where software is composed of small, independent services that communicate over well-defined application programming interfaces (APIs). Small, self-contained teams own these services. 

Amazon ECS is a highly scalable, high-performance container orchestration service. It supports Docker containers and enables you to run and scale containerized applications on AWS quickly. With Amazon ECS, you no longer have to install and operate your container orchestration software, manage and scale a cluster of virtual machines (VMs), or schedule containers on those VMs.

With simple API calls, you can launch and stop Docker-enabled applications, query the complete state of your request, and access many natural features, such as AWS Identity and Access Management (IAM) roles, security groups, load balancers, Amazon CloudWatch Events, AWS CloudFormation templates, and AWS CloudTrail logs.

Prerequisites and limitations

Prerequisites

  • An active AWS account  

  • Java microservices source code, with Java Development Kit version 1.7 or later

  • An access key and secret access key for a user in the account

  • AWS Command Line Interface (AWS CLI)

  • Java, AWS Software Development Kit (SDK) for Python (Boto3), and Docker software

  • Familiarity with the use of the preceding technologies

  • Familiarity with AWS services such as Amazon ECS, AWS CloudFormation, and Elastic Load Balancing

Architecture

Source technology stack

  • Microservices implemented in Java and deployed on Apache Tomcat in an on-premises environment

Target technology stack

  • The Application Load Balancer that inspects the client request. Based on routing rules, the load balancer directs the request to an instance and port from the target group that matches the state.

  • A target group for each microservice. The target groups are used by the corresponding services to register available container instances. Each target group has a path, so when you call the way for a particular microservice, it maps to the correct target group. This enables you to use one Application Load Balancer to serve all the microservices, accessed by the path. For example, https:///owner/* would map and direct to the Owner microservice.

  • An Amazon ECS cluster that hosts the containers for each microservice.

  • An Amazon Virtual Private Cloud (Amazon VPC) network for hosting the Amazon ECS cluster and associated security groups.

  • An Amazon Elastic Container Registry (Amazon ECR) repository for each microservice.

  • A service or task definition for each microservice, which spins up containers on the instances of the Amazon ECS cluster.

Target architecture

Tools

  • Amazon ECS – Amazon ECS lets you launch and stop container-based applications with simple API calls, enables you to get the state of your cluster from a centralized service, and gives you access to many familiar Amazon Elastic Compute Cloud (Amazon EC2) features.

  • Amazon ECR – Amazon Elastic Container Registry (Amazon ECR) is a fully managed registry that makes it easy for developers to store, manage, and deploy Docker container images. Amazon ECR is integrated with Amazon ECS to simplify your development-to-production workflow. Amazon ECR hosts your images in a highly available and scalable architecture so you can reliably deploy containers for your applications. Integration with AWS Identity and Access Management (IAM) provides resource-level control of each repository.

Epics

TaskDescriptionSkills required
Provision an Amazon EC2 Linux instance, install Docker, and create a Docker file for each microservice.Ops
Set up Docker images on Amazon ECR.

Use the Dockerfile for the image to push, build the image, and tag it for your new repository. Do the same for each microservice. Push the newly tagged images to the repository.

Ops
Create an AWS CloudFormation template.

Create an AWS CloudFormation template to provision the virtual private cloud (VPC), Amazon ECS cluster, and Amazon Relational Database Service (Amazon RDS).

Ops
TaskDescriptionSkills required
Create the AWS infrastructure by using the CloudFormation template you created earlier.

Use the Python script at https://github.com/awslabs/amazon-ecs-java-microservices/blob/master/2_ECS_Java_Spring_PetClinic_Microservices/setup.py to invoke the AWS CloudFormation template you created earlier. This template creates the AWS infrastructure you need for the target environment.

Ops
Create Amazon ECR repositories, tasks, services, the Application Load Balancer, and target groups.

The Python script reads the outputs of the AWS CloudFormation template and uses BOTO3 API calls to create Amazon ECR repositories, tasks, services, the Application Load Balancer, and target groups.

Ops