logo
Tutorial

Deploy a Java Application on Linux

As a cloud engineer you will deploy a variety of applications on different types of servers in the cloud. Learn how to deploy a SpringBoot Java application on a AWS Lightsail Linux VPS.

Sophia Parafina
Sophia Parafina
Amazon Employee
Published Jul 10, 2023

The previous tutorial demonstrated how to create a Windows Server VPS and deploy an ASP.NET Core application. A common and similar task is deploying a Java application on a Linux server. While Javascript frameworks such as Express.js have increased in popularity for applications in the cloud, Java frameworks such as SpringBoot are equally popular in enterprise environments.

This tutorial demonstrates how to deploy a Java application on a Linux Virtual Private Server (VPS) with AWS Lightsail. If you’re unfamiliar with AWS Lightsail check out the overview in the previous tutorial. We’ll use the previous DevOps scenario where the software is compiled by a CI/CD and stored in an S3 bucket. You copy the software or artifact from AWS S3 bucket or object store to deploy on a Linux VPS.

  • How to deploy and configure a Linux server on AWS Lightsail.
  • Configure and deploy a Java SpringBoot application with a launch script or User data.
  • Configure the VPS network with a static IP and open a port for the Java application
✅ AWS Level
200 - Intermediate
⏱ Time to complete
45 minutes
💰 Cost to complete
Free when using the AWS Free Tier or USD 1.01
🧩 Prerequisites
- An AWS account: If you don't already have an account, follow the Setting Up Your Environment tutorial. For a quick overview for creating account follow the Create Your AWS Account instructions.
- AWS credentials: Follow the instructions in Access Your Security Credentials to get your AWS credentials
- A git client: Follow the instructions to Install Git for your operating system.
- Java installed locally.
- Maven installed locally.
- Java installed locally.
- Maven installed locally.
💻 Code Sample
📢 Feedback
⏰ Last Updated
2023-05-31

The Java application for the tutorial is on Github. You will clone the repository (optional if you have completed this in a previous tutorial). The project uses Maven to compile and package the application into a .jar file.

The application for this tutorial is on GitHub. Clone the application (optional if you have done this in a previous tutorial).

1
git clone https://github.com/build-on-aws/practical-cloud-guide-code

Compile the application to a .jar with maven.

1
2
cd ./practical-cloud-guide-code/run-to-build/linux-app-deploy/
mvn package

Maven will compile the code, create a target directory, and create a HelloBuilder.jar file.

In DevOps, applications are typically built with Continuous Integration (CI) software. Code is pushed into the CI by developers where it is built and tested and released into cloud storage.

In this module, the software is in a GitHub repository. You will clone the repository to copy the files to your local drive. The next step is to use Lightsail create an S3 bucket to store the files that can be accessed by a Linux Server deployed with AWS Lightsail.

Steps 1-2 are optional if you completed the Deploy an ASP.NET Core Application on Windows Server with AWS Lightsail tutorial.

Open AWS Lightsail

Choose Storage.

Choose Storage in the Lightsail menu

In the Create a new bucket page choose the 5GB storage plan and name the bucket with a unique name such as my-practical-cloud-guide. Please note that S3 bucket names are globally unique, so make sure to pick a name that is available. For the rest of this tutorial, when you see <my>-practical-cloud-guide for the S3 bucket name, please replace it with your bucket name. Select Create Bucket.

Create an S3 bucket

You will see a menu page for the <my>-practical-cloud-guide bucket, choose Objects.

Open the Objects menu

The Object list displays the objects in the bucket. Choose Upload to put the application and service file in the bucket.

Upload files to S3

Choose File.

Choose File

Select HelloBuilder.jar from the ./practical-cloud-guide-code/run-to-build /linux-app-deploy/target/ and choose Open.

Choose the HelloBuilder.jar

Upload the hellobuilder.service file to your S3 bucket. This file congifures the application as a service and will restart the application after a reboot. Select the file and choose Open.

Choose the HelloBuilder.jar

The files will be added to the Object list.

Files listed

In this section, you will create an Amazon Linux server, install Java, and deploy a SpringBoot Java application with a script that runs when the instance is launched. Configuration and deployment are performed through the Lightsail console without logging into the Linux server. You will also use the Lightsail console to open a port for the application and attach a static IP address to the server.

You will use the AWS Lightsail console to instantiate an AWS Linux VPS and configure it using the User Data option when creating the Linux VPS.

Choose Create instance.

Create a Linux VPS

  1. Select Linux/Unix for a platform.
  2. For a Blueprint, choose OS Only.
  3. Choose Amazon Linux 2
Choose Amazon Linux

Choose Add launch script.

Add a launch script

Configure an instance at launch by adding a shell script that runs when instance starts. The script exports your access key, secret key, and AWS region. These are temporary credentials only available at launch. The script needs your credentials to copy the SpringBoot application, HelloBuilder.jar, from your S3 bucket. Note that AWS CLI commands are available to use in the script. After the script copies the jar file, it installs Java, configures the application as a service, and starts the application.

To install the application as a service, use systemd, a Linux system and service manager. Use a service file to configure the service.

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=Manage Java service

[Service]
WorkingDirectory=/opt/app
ExecStart=/bin/java -Xms128m -Xmx256m -jar HelloBuilder.jar
User=appadmin
Type=simple
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

Copy the script add your credentials, AWS region, and the name of your S3 bucket.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#! /bin/bash
export AWS_ACCESS_KEY_ID=<your_access_key>
export AWS_SECRET_ACCESS_KEY=<your_secret_key>
export AWS_REGION=<your_aws_region>

# create directory for the app
mkdir /opt/app

# download application and system service file
aws s3 cp s3://my-practical-cloud-guide/hellobuilder.service /etc/systemd/system/hellobuilder.service
aws s3 cp s3://my-practical-cloud-guide/HelloBuilder.jar /opt/app/HelloBuilder.jar

# install java
sudo yum -y install java-17-amazon-corretto-headless

# add application admin group and user
sudo groupadd -r appgrp
sudo useradd -r -s /bin/false -g appgrp appadmin

sudo chown -R appgrp:appadmin /opt/app

# load the service file and start the service
sudo systemctl daemon-reload
sudo systemctl start hellobuilder.service

# restart service on boot
sudo systemctl enable hellobuilder
Copy code to launch script window

Choose your instance plan. The smallest instance works for this tutorial, but larger instances are more performant.

Choose your instance plan.

Name your instance, for example Amazon_Linux_2_Java. Choose Create instance to instantiate the VPS.

Name the instance.

The Lightsail Linux VPS includes both a private and public IP address. However, only port 22 and port 80 are open by default. The Java application is running but available on port 8080. In this module, you will configure networking to open port 8080 and add a static IP address.

Select the name of Linux VPS to open the menu for the instance.

Select the instance.

Choose Networking to open the networking menu.

Choose Networking.

The VPC has a public and private IP and the IPV Firewall opens port 22 and port 80.

Open the firewall.

To open a port, select Add rule.

Add rule to the firewall.

Choose Custom and add 8080 as a port. Select Create.

Open port 8080 in the firewall.

Port 8080 added to the firewall.

Port 8080 open.

When a VPS is restarted, a new public IP address is assigned to the instance. You can assign a static IP address so that the application is available at the same address when restarted.

From the Lightsail Home menu, choose Networking.

Choose Networking.

Choose Create static IP.

Choose Create static IP.

Attach a static IP to and instance. Choose the VPS you created.

Attach a static IP.

Give the static IP a unique name. Choose Create.

Name the static IP.

Open a browser to the static IP on port 80 and the Java application returns a page with a message the time.

Open browser to see application

To prevent additional costs, delete the Linux VPS created for the tutorial. Deleting the S3 bucket is optional. You can keep the S3 bucket to use with other tutorials.

Choose Instances in the Lightsail menu and select the three red dots. Choose Delete.

Delete the instance.

Choose Yes, delete.

Choose Delete.

Choose Storage on the Lightsail menu. Select the three vertical dots.

Select dots.

Choose Delete.

Choose Delete.

Choose Force Delete to delete the files and the S3 bucket.

Choose Force Delete.

In this module you deployed a Linux VPS using Lightsail. The instance was configured by providing a script to download and install the jar file and a Java JDK to run the SpringBoot application. In a future tutorial, you will use the User Data feature to configure virtual machines or Amazon Elastic Cloud Compute (EC2) instances with a launch script. Note that your AWS credentials were only available at launch and not retained by the VPS.

Only ports 22 and 80 are open by default in a VPS. With Lightsail, you can open and close ports using the web console instead of manually configuring the network on the server. This is also true for adding a static IP address that ensures the application has a fixed IP address regardless if the instance has been restarted.

Deploying a VPS, either Windows or Linux. on AWS Lightsail reduces that amount of infrastructure configuration by providing the resources for a Virtual Private Cloud (VPC) including networking, storage, and even RDP or SSH sessions in the browser. However the key take away from these tutorials is that the cloud is programmable. You can use a Powershell script in the VPS or a shell script in User Data to use other cloud resources. For example, applications were downloaded from your S3 bucket. You can use the cloud’s Application Programming Interface (API) to build infrastructure that can be tested and replicated. The end goal of the Practical Cloud Guide is teach you how to build infrastructure by programming the cloud.

The next set of tutorials shows how to migrate and deploy a relational database in the cloud. For now. you will continue to use AWS Lightsail, but you will perform the database deployment and migration with the AWS Command Line Interface (CLI) instead of the web console. Learning how to use the CLI is the first step towards building infrastructure with code.