Monday, October 23, 2023
HomeSoftware EngineeringThe way to Deploy a Java Utility in AWS ECS utilizing Terraform

The way to Deploy a Java Utility in AWS ECS utilizing Terraform


With a view to deploy a Java utility into AWS ECS (Elastic Container Service) utilizing Terraform, we have to think about just a few various things.

Step 1 – Java Utility

Create a file referred to as HelloWorld.java and add the next code to it:

public class HelloWorld {
    public static void primary(String[] args) {
        System.out.println("Hey, World!");
    }
}

We now have to construct our class as follows:

javac HelloWorld.java

As soon as that is accomplished, we will package deal our utility right into a jar file:

jar cvf my-app.jar HelloWorld.class

Step 2 – Dockerfile

Subsequent create a file referred to as Dockerfile and replica the next code into it:

FROM openjdk:11-jre-slim

WORKDIR /app

COPY goal/my-app.jar /app

CMD ["java", "-jar", "my-app.jar"]

Notice that goal/my-app.jar on this code is the relative path from the Dockerfile to the my-app.jar that we packaged in step 1 above.

Step 3 – Terraform

Subsequent we are going to deal with the Terraform. To do that, we will both create completely different Terraform information, however on this instance, we are going to merely create a single file referred to as primary.tf.

On this file, we are going to first create an ECS process definition:

useful resource "aws_ecs_task_definition" "my_task_definition" {
  household                   = "my-task-definition"
  container_definitions    = jsonencode([
    {
      name      = "my-container"
      image     = "my-docker-image"
      cpu       = 256
      memory    = 512
      portMappings = [
        {
          containerPort = 8080
          hostPort      = 8080
        }
      ]
    }
  ])
}

Adopted by an ECS service:

useful resource "aws_ecs_service" "my_service" {
  identify            = "my-service"
  cluster         = aws_ecs_cluster.my_cluster.id
  task_definition = aws_ecs_task_definition.my_task_definition.arn
  desired_count   = 1

  network_configuration {
    subnets          = [aws_subnet.my_subnet.id]
    security_groups  = [aws_security_group.my_security_group.id]
    assign_public_ip = true
  }
}

Step 4 – Working the Terraform

Now we have to run the Terraform code, which we will do from the terminal as follows:

terraform init
terraform apply



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments