So as to deploy a Java utility into AWS ECS (Elastic Container Carrier) the use of Terraform, we want to believe a couple of various things.
Step 1 – Java Software
Create a record known as HelloWorld.java and upload the next code to it:
public magnificence HelloWorld {
public static void primary(String[] args) {
Device.out.println("Hi, International!");
}
}
We now want to construct our magnificence as follows:
javac HelloWorld.java
As soon as that is executed, we will be able to bundle our utility right into a jar record:
jar cvf my-app.jar HelloWorld.magnificence
Step 2 – Dockerfile
Subsequent create a record known as Dockerfile and duplicate the next code into it:
FROM openjdk:11-jre-slim
WORKDIR /app
COPY goal/my-app.jar /app
CMD ["java", "-jar", "my-app.jar"]
Observe that goal/my-app.jar on this code is the relative trail from the Dockerfile to the my-app.jar that we packaged in step 1 above.
Step 3 – Terraform
Subsequent we will be able to center of attention at the Terraform. To try this, we will be able to both create other Terraform information, however on this instance, we will be able to merely create a unmarried record known as primary.tf.
On this record, we will be able to first create an ECS assignment definition:
useful resource "aws_ecs_task_definition" "my_task_definition" {
circle of relatives = "my-task-definition"
container_definitions = jsonencode([
{
name = "my-container"
image = "my-docker-image"
cpu = 256
memory = 512
portMappings = [
{
containerPort = 8080
hostPort = 8080
}
]
}
])
}
Adopted through an ECS provider:
useful resource "aws_ecs_service" "my_service" {
title = "my-service"
cluster = aws_ecs_cluster.my_cluster.identity
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 – Operating the Terraform
Now we want to run the Terraform code, which we will be able to do from the terminal as follows:
terraform init
terraform observe