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) and Sandeep Bondugula (AWS)

Environment: PoC or pilot

Source: Containers

Target: Amazon ECS

R Type: N/A

Technologies: Containers & microservices; Web & mobile apps

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 

Source architecture for deploying Java microservices on Amazon ECS

Target technology stack  

  • Amazon ECR

  • Amazon ECS

  • AWS Fargate

Target architecture 

Target architecture for deploying Java microservices on Amazon ECS

Tools

Tools

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

TaskDescriptionSkills required

Create a repository.

Sign in to the AWS Management Console, and open the Amazon ECR console at https://console.aws.amazon.com/ecr/repositories. Create a private repository. For instructions, see Creating a private repository in the Amazon ECR documentation.

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
TaskDescriptionSkills 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. For more information, see Creating a task definition in the Amazon ECS documentation.

Developer, System Admin

Choose the 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. Ports 8080 and 80 are 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 Related resources section.

Developer, System Admin
TaskDescriptionSkills 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 the 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.

Provide 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. The minimum number of tasks to be configured is One.

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 (HTTP, HTTPS, and port 8080) 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
TaskDescriptionSkills required

Test your application.

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

Developer, System Admin

Related resources