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

Deploy Java microservices on Amazon ECS using Amazon ECR and AWS Fargate

Created by Vijay Thompson (AWS)

R Type: N/A

Source: Containers

Target: Amazon ECS

Created by: AWS

Environment: PoC or pilot

Technologies: Websites & web apps; Containers & microservices

AWS services: Amazon ECS

Summary

This pattern guides you through the steps for deploying Java microservices as containerized applications in Amazon Elastic Container Service (Amazon ECS). The pattern also uses Amazon Elastic Container Registry (Amazon ECR) to manage your container, and AWS Fargate to run your container.

Prerequisites and limitations

Prerequisites

  • An existing Java microservices application running on premises on Docker

  • An active AWS account

  • Familiarity with Amazon ECR, Amazon ECS, AWS Fargate, and AWS Command Line Interface (AWS CLI)

  • Familiarity with Java and Docker software

Product versions

  • AWS CLI version 1.7 or later

Architecture

Source technology stack

  • Java microservices (for example, developed using Spring Boot) and deployed on premises

  • Docker

Source architecture 

Target technology stack  

  • Amazon ECR

  • Amazon ECS

  • AWS Fargate

Target architecture 

Tools

Tools

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

  • Amazon ECS - Amazon Elastic Container Service (Amazon ECS) is a highly scalable, high-performance container orchestration service that supports Docker containers and allows you to easily run and scale containerized applications on AWS. Amazon ECS eliminates the need for you to install and operate your own container orchestration software, manage and scale a cluster of virtual machines, or schedule containers on those virtual machines.

  • AWS Fargate - AWS Fargate is a compute engine for Amazon ECS that allows you to run containers without having to manage servers or clusters. With AWS Fargate, you no longer have to provision, configure, and scale clusters of virtual machines to run containers. This removes the need to choose server types, decide when to scale your clusters, or optimize cluster packing.

  • Docker - Docker is a platform that lets you build, test, and deliver applications in packages called containers.

Code

The following DockerFile specifies the Java Development Kit (JDK) version that is used, where the Java archive (JAR) file exists, the port number that is exposed, and the entry point for the application.

FROM openjdk:8 ADD target/Spring-docker.jar Spring-docker.jar EXPOSE 8080 ENTRYPOINT ["java","-jar","Spring-docker.jar"]

Epics

Task Description Skills required
Create a repository.

Open the Amazon ECR console at https://console.aws.amazon.com/ecr/repositories, and create a repository. For instructions, see the links provided in the References section.

Developer, System Admin
Upload the project.

Open the repository and choose "View push commands". Follow the steps displayed to upload the project (these steps work only when you use AWS CLI version 1.7 or later). When the upload is complete, copy the URL of the build in the repository. You will use this URL when you create a container in Amazon ECS.

Developer, System Admin
Task Description Skills required
Create a task definition.

Running a Docker container in Amazon ECS requires a task definition. Open the Amazon ECS console at https://console.aws.amazon.com/ecs/, choose "Task definitions," and create a new task definition.

Developer, System Admin
Choose launch type.

Choose Fargate as the launch type.

Developer, System Admin
Configure the task.

Define a task name and configure the application with the appropriate amount of task memory and CPU.

Developer, System Admin
Define the container.

Add the container, providing a name, the URL of the Amazon ECR repository, memory limits, and port mapping. Port 8080 is configured for port mappings. Configure the remaining settings based on your application requirements.

Developer, System Admin
Create the task.

When the task and container configurations are in place, create the task. For detailed instructions, see the links in the References section.

Developer, System Admin
Task Description Skills required
Create or choose a cluster.

An Amazon ECS cluster provides a logical grouping of tasks or services. You can opt to use an existing cluster or create a new cluster. If you decide to create a new cluster, choose the cluster type based on your requirements. In our example, we selected a networking cluster. Provide a name for the cluster and choose whether you want to create a new virtual private cloud (VPC) to use for Fargate tasks.

Developer, System Admin
Create a service.

Inside the cluster, choose "Create service."

Developer, System Admin
Choose launch type.

Choose Fargate as the launch type.

Developer, System Admin
Choose task definition, revision, and platform version.

Choose the task that you want to run, followed by the revision of the task definition, and the platform version.

Developer, System Admin
Select the cluster.

Select the cluster in which to create your service from the dropdown list.

Developer, System Admin
Provide a service name.

Type a unique name for the service that you are creating.

Developer, System Admin
Specify the number of tasks.

Configure the number of tasks that should run when the service launches. If you're launching with two or more tasks, a load balancer is required to balance the tasks.

Developer, System Admin
Set the minimum and maximum healthy percentages.

Configure the minimum and maximum healthy percentages for the application, or accept the default option that is provided.

Developer, System Admin
Configure deployment settings.

Choose the deployment type based on your requirements. You can choose a rolling update or a blue/green deployment.

Developer, System Admin
Configure the cluster VPC, subnets, and security groups.

Configure the cluster VPC, the subnets on which you want to deploy the application, and the security groups for providing access to inbound/outbound connections.

Developer, System Admin
Configure public IP settings.

Enable or disable the public IP, depending on whether you want to use a public IP address for Fargate tasks.

Developer, System Admin
Configure load balancing.

Configure the load balancer, if you're launching the service with more than one task. You must create a load balancer and its target group before you launch the service.

Developer, System Admin
Configure automatic scaling.

Configure your service to use Amazon ECS Service Auto Scaling to adjust the desired number of tasks up or down, depending on your requirements.

Developer, System Admin
Review settings and create the service.

Review your service settings, and then choose "Create service."

Developer, System Admin
Task Description Skills required
Test your application.

Test the application by using the public DNS, and then cut over. If your application has a load balancer, test the application by using it, or use the public IP that's created when the task is deployed.

Developer, System Admin