Deploying a Java Code using AWS EC2

 

In this post, we will see how to deploy the JAR on a EC2 instance with Auto Scaling Group with Load Balancer.

We can do the same using AWS Code Deploy with orchestration using AWS Code Pipeline. We are taking a different path.

In the previous post, https://rsinfomindss.blogspot.com/2026/04/aws-codebuild.html 

Once the build is stored in the S3. We have to perform the below steps in a EC2 instance to run the code.

  • 1)       Download Java.
  • 2)       Create a folder for the application.
  • 3)       Download the JAR from S3.
  • 4)       Start the JAR.

I will start with creating a Launch Template. Refer to https://rsinfomindss.blogspot.com/2023/04/aws-launch-template.html 


Make sure IAM EC2 instance profile is create with the permission to download S3 objects (JAR) from the EC2 instance. Refer to example below. S3 bucket "demo-java-application-artifact" is where the JAR is located.


Finally, update the "User Data". EC2 has a feature called "User Data" which can be used as boot/startup script.



Our launch template is ready.

Let's create a Auto Scaling Group from the above Launch Template with a Load Balancer.





Finally, we are setting the desired capacity limit.



We are done and we could see our EC2 instances are HEALTHY.



And, we could access the application using Load Balancer DNS.




Whenever a new code is pushed, all we need to do it replace the EC2 instance by stopping/terminate them. So, that ASG creates a new instance with new JAR.


Comments

Popular posts from this blog

K8s - ETCD

Agentic AI - Series 3

Agentic AI - Series 4