DEV Community

ShivangDholaria
ShivangDholaria

Posted on

Deploying a Spring Boot Application on AWS: A Complete Guide

Now that our application is running perfectly in the local environment, lets move it to the cloud. My preferred choice for cloud provider is AWS since its widely used and provides with 12-month of free usage.

Setting the cloud Infrastructure

I am assuming that you will an account in AWS and logged into it. If not, then it is really easy to setup the account. And yes, if you are creating an account for the first time, you will be needing a credit/debit card since it will use it for verification purposes. You will see a small amount debited, which will be refunded the next day once the account creation is successful.

Now that you are logged into your AWS account, we will be going to our server which will host our application. Creating it is simple, just navigate to EC2 from search bar.

EC2

Open it and click on launch instance, You will see this.

EC2 Launch screen

Given any name you want to your server.

Server name

Select Ubuntu from Application and OS images and keep the default as is.

OS selection

Keep the instance type as t2.micro as it is free tier eligible.

Image description

For key-pair, if you already have one, select that. If not click create new pair and create one by keeping the defaults as is.

Key-pair creation

Keep the network settings default too. Make sure that Allow SSH traffic from is enabled and Anywhere is selected.

Network Settings

Now click on launch instance to create your server. You will see this once the creation is successful.

Instance creation successful

Once created, wait till you see your status check as 2/2 checks passed. This confirms that the server is now fully up and running.

Instance check pass

Now lets log into the server and deploy our applicatio. Click on the server and then click on connect.

Connect to instance

You will see any options. Do the following if you are:

  • a Windows user, setting the permission on the key is a tricky process, so we will use the EC2 Instance Connect option.

Instance connect

  • a Linux/Mac user, open the SSH client option and follow through the steps.

Instance Connect

Once connected, you should be able to see this.

Connect log in

Run sudo apt update command to update the packages for the server.

Before cloning your repo, you will first need to install java. We wont be installing maven as the repo has a mvnw executable, which a maven wrapper. Install java using the following command:

sudo apt install openjdk-21-jre-headless -y
sudo apt install openjdk-21-jdk-headless -y

Once installed, clone the repo and cd into it. Make the mvnw executable by using the following:

sudo chmod +x mvnw

One more step and we are done, just enter the API key in the application.properties file and save it. Now just enter the command to run the application.

./mvnw spring-boot:run

Once you run that command, you will see successful execution. You will be thinking now if I open the browser and type localhost:<PORT>, I should be able to see the application running. Well, the short answer is no since you are running a different machine.

Localhost

To access it, you need to enter the server's public IP followed by the port number to access it. Type the public IP address (which you can find at the homepage on Instance), followed by the port number in the browser. You still won't see the application since by default our server blocks any incoming connection to it on that port. To allow them, we need to edit its network rules.

Instance application

This can be done by editing the security group of the instance. You can find the security group of the instance in its security tab. For me it is launch-wizard-4.

Security group

Open the security group and then click on edit inbound rules.

Inbound rules

Click on add rule, keep the type as Custom TCP, Port range as your port number (I have kept mine as 8001 since thats the port which my application uses), Source as Anywhere and click on search bar next to it and select the CIDR range as 0.0.0.0/0 and Save rules

Image description

Once you are done, you should see the added rule, and now your app should be opening.

App running in instance

In part 3 we will be discussing the automation process.

Top comments (0)