Saturday, October 1, 2022

Deploying a Java web application to Amazon ECS

 You can deploy a Java web application that uses the AWS SDK for Java (v2) to Amazon Elastic Container Service (Amazon ECS). Amazon ECS is a highly scalable and fast container management service. You can use it to run, stop, and manage containers on a cluster. With Amazon ECS, your containers are defined in a task definition that you use to run an individual task or task within a service.

This AWS tutorial walks you through deploying a Java web application to an AWS Fargate container. With AWS Fargate, you don't need to manage servers, handle capacity planning, or isolate container workloads for security. Fargate handles the infrastructure management aspects of your workload for you. For more information, see What is Amazon Elastic Container Service.

To deploy a Java web application to AWS Fargate, create a Docker image from your Spring Boot project. Then, you can deploy your Docker image to Docker Hub, which is a hosted repository service. Finally, configure AWS Fargate to import the Docker image located on Docker Hub into a container, as shown in the following illustration.


After you complete this tutorial, you will have a Java web application running in a container.



Topics

  • Prerequisites
  • Deploy your Java web application to Docker Hub
  • Deploy the Docker image to Amazon ECS
  • Run the application

Prerequisites

To complete the tutorial, you need the following:
  • An AWS account.
  • A Java IDE. (This example uses IntelliJ.)
  • Java 1.8 SDK and Maven.
  • Docker Desktop. (For information about installing, see Docker Desktop overview.)

If you are using a Windows operating system, install Docker Desktop for Windows. For more information, see Install Docker Desktop on Windows. We recommend that you verify that Docker is installed on your system by running the following command.

Docker --version

If this command is successful, the version is displayed.



Important

  • The AWS services used in this document are included in the AWS Free Tier.
  • This code has not been tested in all AWS Regions. Some AWS services are available only in specific Regions. For more information, see AWS Regional Services.
  • Running this code might result in charges to your AWS account.
  • Be sure to delete all of the resources that you create while going through this tutorial so that you won't be charged.

Creating the resources

To follow along with this tutorial, complete Creating your first AWS Java web application. This is the Java web application that is deployed to a container.

Modify the Java code in the project to use a StaticCredentialsProvider. This way, your credentials can be used within the container. To use this provider, create a service client object using the following Java code.

  AwsBasicCredentials credentials = AwsBasicCredentials.create("<Key value>", "<Secret Key value>");
  StaticCredentialsProvider staticCredentials = StaticCredentialsProvider.create(credentials);
  Region region = Region.US_EAST_1;
  DynamoDbClient ddb = DynamoDbClient.builder()
            .region(region)
            .credentialsProvider(staticCredentials)
            .build();


Deploy your Java web application to Docker Hub

First, create a Docker image from your Spring Boot application.

Note: As stated at the start of this document, make sure to complete the steps in Creating your first AWS Java web application. All of the Java logic can be found in that document.

Add the Docker file to your project

Add the Docker file to your project. This file is used to assemble a Docker image. The following illustration shows the location of this file.


The following code shows the content of this file.

FROM openjdk:8-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=target/my-application.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Build your Docker image

You can build a Docker image from your Spring Boot project by using the following command.

mvn spring-boot:build-image

Note: Make sure that your project name in the POM.xml file is lowercase. Otherwise, an error occurs.

Verify that the Docker image was created by using the following command.

docker image ls

All the Docker images are displayed, as shown in the following illustration.



Next, execute the run command and specify 8080 as the port.

docker run --tty --publish 8080:8080 firstappdocker:1.0

Run the Docker tag command.

 docker tag firstappdocker:1.0 scmacdon/firstappdocker:1.0

In this example, scmacdon refers to the repo name in Docker Hub. Change this value to reflect your repository name.

Verify that the Docker image with a tag was created by using the following command.

docker image ls



Deploy the Docker image to Docker Hub

You can push the Docker image to Docker Hub by using the following command. You must use the tag value and the Docker image name.

docker push scmacdon/firstappdocker:1.0

After deployment, you will see the Docker image in Docker Hub at:

https://hub.docker.com/

The following illustration shows the Docker image deployed to Docker Hub.



After the Docker image is located on Docker Hub, you can import it into Amazon ECS.

Deploy the Docker image to Amazon ECS

You can deploy the Docker image located on Docker Hub to Amazon ECS. After you complete this task, you can run the application.

To deploy the Docker image to Amazon ECS, perform these steps.

1, Sign in to Amazon ECS at the following URL:

https://us-west-2.console.aws.amazon.com/ecs/home?region=us-west-2#/clusters

2. Choose Task Definitions.

3. Choose Create new Task Definition.



4. Choose Fargate.

5. Choose Next step.

6. In the Task definition name field, enter a name. For example, springboot-docker.

7. For Task role, select None.



8. For Task memory (GB), specify 1 GB.

9. For Task CPU (vCPU), specify 0.5.

10. Choose Add container.

11. Specify a container name. For example, first-java-container.

12. For Image, specify a URL of docker.io/[Repo Name]/[Image Name]:[Tag]. For example, docker.io/scmacdon/firstappdocker:1.0.

13. For Memory limits, specify a Hard limit of 1024.



14. Specify two Port Mappings values, 8080 and 80.

15. Choose Add.

16. Choose Create. A message informs you that the task definition was created.





17. Choose View Definition.

18. Choose Clusters, Create Cluster.

19. Choose Networking Only.

20. Click Next step.

21. Specify a cluster name. For example, my-aws-cluster.


22. Choose Create. You will see a confirmation message.


23. Choose View Cluster.

24. Choose Tasks.

25. Choose Run New Task.


26. Choose Fargate as Launch Type.

27. Choose Task Definition (select the one that you created).


28. Select Cluster VPC and Subnet values.

29. Configure a security group for this cluster. For information about setting up security group inbound rules, see Controlling Access with Security Groups.

30. Choose Run Task. You will see a message that shows it's running. (You might have to wait a few seconds and then refresh.)


31. Choose the link under Task. Then, scroll under Network. You see the public IP address that you can use to access the application. Remember to specify the port number, also.



Deploying a Java web application to Amazon ECS

 You can deploy a Java web application that uses the AWS SDK for Java (v2) to Amazon Elastic Container Service (Amazon ECS). Amazon ECS is a...