https://www.callicoder.com/assets/images/post/large/spring-boot-aws-deploy-elastic-beanstalk.jpg

Deploying/Hosting Spring Boot Applications on AWS EC2

As an app developer, I always wished for a simple way to get started with writing REST APIs and host them on a server. When I did some research, I found that it’s really easy to achieve this on AWS platform (I am sure other cloud providers also provide similar features like this). I am going to explain various steps which you need to follow to write your first backend API and get it running on AWS in no time. Let’s get started!

Step 1: Create Spring Boot Application using Initializr website

  1. Visit https://start.spring.io/ and choose Kotlin/Java language.
  2. Choose Gradle/Maven build tools as per your choice.
  3. Fill all the project metadata field as per your choice.
  4. Click on Add Dependencies button on the right side of the screen.
  5. Select Spring Web dependency.
  6. Click on Generate button at the bottom of the screen.

After clicking on Generate button, a zip file will be downloaded in your system. Unzip this file and open this project in the IDE (You can use IntelliJ IDEA as this is a very popular IDE). It will take some time to sync the project for the first time. Let’s not just wait but utilise this time to create EC2 instance on AWS.

Step 2: Create an EC2 instance on AWS console

  • Visit https://aws.amazon.com/ and do a sign-up / sign-in into the console.
  • Please note that AWS provides various free services up to a certain limit. You can know more about it by visiting this link. We’ll be using Amazon EC2 which comes under 12 MONTHS FREE category and has a free quota up to 750 hours per month.
  • Once you login to console, you should see something like below under Services section.
  • Click on EC2 under Compute section.
  • Click on Launch Instance button in the EC2 dashboard.
  • Choose AMI (Amazon Machine Image) for your EC2 instance. This is actually software configurations required to launch the instance. Click on Select to proceed to the next screen.
  • On the next screen, choose the instance type. Instances are virtual servers that can run applications. They have varying combinations of CPU, memory, storage, network capacity etc. to match your application requirements. Select t2.micro instance as it comes under the free tier.
  • Now keep all the default values for the next three sections and move to Configure Security Group section.
  • Security group basically defines a set of firewall rules that control the traffic for your instance. Click on Add Rule button to add a rule allowing TCP traffic on port 8080. Click on Review and Launch button at the bottom of the screen.
  • On Review Screen, verify all the details and click on Launch. You will get a pop-up window asking to create or select key pair. This key pair will be required to log into the ec2 instance. Download this file if you are creating a new key pair. Click on Launch Instances.
  • You should see something like below. Click on instance id to see your newly created EC2 instance.
  • Select the instance and click on Connect button on the top right. You should see something like this.

Copy the ssh command present on the last line. We’ll be using this to login into the console.

  • Open the console and move to the directory where you had downloaded the key pair file. Paste the below command into the console and voila! you are logged into the console. Please replace ec2–18–225–9–2.us-east-2.compute.amazonaws.com with your ec2 instance DNS entry.

ssh -i “aws-demo.pem” ec2-user@ec2–18–225–9–2.us-east-2.compute.amazonaws.com

Step 3: Deploy the REST API (spring boot application jar) in EC2

  • Open your IDE and create a controller class. We’ll use below class for demo purpose.
  • Now run ./gradlew bootJar command to generate the jar file. You can find this generated jar file at build/libs location. Now we need to copy this jar file to EC2 instance and execute java -jar command to start the server.
  • To avoid the manual process, I have created script files which take care of copying the jar file to ec2 instance, shut down already running server and restart the server with new changes.
  • Now execute deploy.sh file using ./deploy.sh command.

Now open a browser and enter this URL in the address bar. In the above gists and URL ec2–18–225–9–2.us-east-2.compute.amazonaws.com is the public DNS of my EC2 instance. Please check your instance DNS name and replace this with your entry. You can find this detail by clicking on instance id in EC2 dashboard.

Congratulations! You just deployed your first REST API on AWS. If you learned something from this article, please share it with your friends and colleagues. Your comments/feedbacks are always welcome. Keep Learning!

Senior Software Engineer 2 @Citrix | Passionate about learning and sharing knowledge | https://github.com/kgaurav23