Deploy a Spring Boot application on Amazon Lightsail

To deploy a Spring Boot application on Amazon Lightsail, follow these detailed steps. Lightsail is a simplified cloud service that provides an easy way to launch virtual private servers (instances) with various pre-configured environments. In this guide, we'll go through the process of deploying a Spring Boot application on a Lightsail instance.

Prerequisites:

  • AWS account.
  • Spring Boot application ready for deployment (JAR or WAR file).
  • Basic knowledge of using AWS Lightsail and SSH.
  • Java Development Kit (JDK) installed (usually OpenJDK for Spring Boot).

Step 1: Create a Lightsail Instance

  1. Login to AWS Lightsail:

  2. Create a new instance:

    • Click on the Create instance button.
    • Select Linux/Unix as the operating system.
    • Choose an instance image. You can either choose OS Only (such as Ubuntu) or a pre-configured application stack (e.g., WordPress, LAMP, etc.). For deploying Spring Boot, select OS Only and choose Ubuntu.
    • Select the instance plan (e.g., choose the smallest plan if you're just testing).
    • Provide a name for the instance.
    • Click on Create instance to launch the instance.
  3. Wait for the instance to be ready.

    • After creation, the instance status will change to Running once it's ready.

Step 2: Configure Networking (Optional but recommended)

  1. Set up a static IP:

    • In the Lightsail console, go to Networking.
    • Under Static IPs, click on Create static IP and assign it to your instance.
    • This static IP will ensure that your application has a fixed public IP address.
  2. Allow access to required ports:

    • Under Networking, configure your Firewall settings.
    • Open the TCP port 22 (for SSH) and TCP port 8080 (or the port your Spring Boot app uses).
    • If using a different port for HTTP (e.g., 80 or 443 for HTTPS), open those ports too.

Step 3: Connect to the Instance via SSH

  1. Connect to your Lightsail instance:
    • In the Lightsail console, go to Networking > Connect to open an SSH terminal.
    • Alternatively, you can connect via SSH using an SSH client like PuTTY (for Windows) or directly from your terminal (Mac/Linux).
      • Use the following command to connect via SSH:
        ssh -i /path/to/your-key.pem ubuntu@<static-ip>
    • If you used Lightsail's built-in SSH terminal, simply click Connect in the Lightsail console.

Step 4: Install Java on the Instance

Spring Boot requires Java to run. If Java isn't installed by default, you'll need to install it.

  1. Update the package index:

    sudo apt-get update
  2. Install OpenJDK (usually OpenJDK 8 or 11 for Spring Boot):

    sudo apt-get install openjdk-11-jdk -y
  3. Verify the installation:

    java -version

    This should output the version of Java installed.

Step 5: Upload the Spring Boot Application JAR File

You need to upload your Spring Boot JAR file to the Lightsail instance.

  1. Upload your JAR file using SCP (Secure Copy Protocol):
    • On your local machine, use scp to transfer the JAR file to the Lightsail instance:
      scp -i /path/to/your-key.pem /path/to/your/springboot-app.jar ubuntu@<static-ip>:/home/ubuntu/
      
    • Alternatively, you can use tools like FileZilla or WinSCP to upload the file.

Step 6: Run the Spring Boot Application

Once the JAR file is uploaded to the instance, you can start the Spring Boot application.

  1. Run the JAR file:

    java -jar /home/ubuntu/springboot-app.jar
  2. Ensure the application runs on a specific port (8080 by default).

    • If you'd like to run your application on a different port, you can specify it when running the JAR:
      java -jar /home/ubuntu/springboot-app.jar --server.port=8080
  3. Check the status of the application:

    • You can use ps aux | grep java to see if the Java process is running.
  4. Access your application:

    • Open a web browser and go to http://<static-ip>:8080 (replace <static-ip> with the IP address of your instance).
    • You should see your Spring Boot application running.

Step 7: Set Up Spring Boot as a Service (Optional)

To ensure your Spring Boot application keeps running even after you log out or reboot the instance, you can configure it as a system service using Systemd.

  1. Create a service file:

    sudo nano /etc/systemd/system/springboot-app.service
  2. Add the following content to the service file:

    [Unit]
    Description=Spring Boot Application
    After=network.target
    
    [Service]
    User=ubuntu
    ExecStart=/usr/bin/java -jar /home/ubuntu/springboot-app.jar
    SuccessExitStatus=143
    
    [Install]
    WantedBy=multi-user.target
  3. Reload Systemd and start the service:

    sudo systemctl daemon-reload
    sudo systemctl start springboot-app.service
    sudo systemctl enable springboot-app.service
  4. Check the status of your service:

    sudo systemctl status springboot-app.service

Step 8: Set Up Domain Name (Optional)

If you want to associate a domain name with your Spring Boot application, you can set up Route 53 for DNS management or use a third-party domain provider.

  1. Configure DNS settings:

    • Point your domain's A record to the static IP of your Lightsail instance.
    • Update DNS settings in your domain provider’s control panel.
  2. Use SSL/TLS for secure connections (Optional but recommended):

    • For secure HTTP (HTTPS), you can use Let’s Encrypt to install a free SSL certificate.
    • Install Certbot and follow the instructions for your instance.

Step 9: Monitor and Scale

  • Monitoring:
    • AWS Lightsail offers built-in monitoring tools like metrics and logs. You can use CloudWatch for detailed metrics.
  • Scaling:
    • If you need more power or traffic management, consider upgrading the Lightsail instance or using Auto Scaling (though Auto Scaling typically applies to EC2).

Conclusion:

By following these steps, you can successfully deploy a Spring Boot application on AWS Lightsail. Lightsail offers an easy-to-use platform for simple applications, and it integrates well with other AWS services to scale as needed.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete