Deploying the Spring-Boot Backend Application on AWS EC2 instance on mac

An EC2 (Elastic Compute Cloud) instance is nothing but virtual server in Amazon Web Services. Its a web service that provides secure, resizable computing capacity in a cloud.

Step 1: Create basic Spring Boot Application with REST API endpoint

  • Visit url https://start.spring.io/
  • Choose Maven Project
  • Fill all the metadata fields
  • Add required dependencies like Spring Web
  • Once all done, click on Generate to create your maven spring-boot project
  • The .zip file will be downloaded in your system.

Open it using IntelliJ or Eclipse, and write the required code for creating REST API endpoint.

You can test the application using basic REST endpoint as follows

package com.blogspot.javasolutionsguide.springbootproject;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/*** @author JavaSolutionsGuide**/@RestController@RequestMapping(value = "/api")public class Hello {@GetMapping(value = "/helloworld")public String sayHelloWorld() {    return "Hello JavaSolutionsGuide Readers";}}

Step 2: Login to AWS console and create EC2 instance.

  • Visit url https://aws.amazon.com/
  • Click “Sign In to the Console” available on top right corner.
  • Create root user account, if already not exists, otherwise login using your credentials.
  • Once logged-in, you will see list of services like below,
  • Click on EC2 service, then click on “Launch Instance” in EC2 dashboard.
  • Choose “Amazon Linux 2 AMI”. This environment provides the long term support with latest linux configurations
  • Finally, select instance for running your application. Instances are virtual servers that can run applications. Select t2.micro instance as it comes under the free tier.
  • 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 can import your key-pair certificate anytime available inside EC2 > Key pairs on your device.

.pem file will be downloaded in your system after clicking on “Import key pair”.

  • Once done, you will see the following page
  • Click on View Instances.
  • Select the instance created and click on Actions available on top.
  • Then select Connect
  • The following page will open, copy ssh command available in step 4 in below picture.

Step 3: Setup Maven on mac using command line and create jar file for your project.

  • Open Terminal
  • Run brew -v to check if homebrew is already installed.
  • If not, download Homebrew package manager on mac.
  • Run mvn -version to check if maven is already installed.
  • If not, run below command to install Maven
    brew install maven
  • Once done, cd into your project folder and run mvn package to package your project into .jar file
  • The .jar file will be created inside target folder available inside parent directory

Step 4: Connect to EC2 instance using Terminal

  • Open Terminal
  • Run the ssh command copied in Step 2.
  • Now your ec2 instance will be connected and you will be redirected to virtual ec2 remote server.

Step 5: Remove JDK 7 and install JDK compatible with your project over EC2 instance

  • Run following command to remove JDK 7
sudo yum remove java-1.7.0-openjdk
  • Refer to Java Oracle site for required version filename
  • Run sudo yum install {filename} to required JDK on EC2

Step 6: Copy .jar file on EC2 instance using command-line.

  • Open new windows Terminal (stay connected to ec2 on previous terminal)
  • cd into your project directory
  • Run following command,
scp -i {path to .pem file}/{.pem} /target/{.jar} ec2-user@ec2-18-220-155-145.compute-1.amazonaws.com

Step 7: Alternate method if Step 6 doesn’t work

Step 8: Run .jar file on EC2 server.

  • Open Terminal window where EC2 is connected
  • Use following command to run .jar file on EC2 server
java -jar {.jar}

Once done and your .jar file runs successfully, you can test your api call by hitting on url:

http://ec2–18–220–155–145.us-east-2.compute.amazonaws.com:8080/helloworld

All set, your backend project is successfully deployed on AWS EC2 and available to use from anywhere (Just make sure your instance is on running state on AWS console 😄).. Cheers! ✌🏻

Syracuse University Grad//Klokwork Shop Management SE Intern/ iOS Developer @Roundglass Curofy ..// IIIT Allahabad UnderGrad// Believe in learning and growing..

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store