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
Login to AWS Lightsail:
- Go to the AWS Lightsail console: https://lightsail.aws.amazon.com/.
- Sign in with your AWS credentials.
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.
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)
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.
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
- 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:
- Use the following command to connect via SSH:
- 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.
Update the package index:
Install OpenJDK (usually OpenJDK 8 or 11 for Spring Boot):
Verify the installation:
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.
- Upload your JAR file using SCP (Secure Copy Protocol):
- On your local machine, use
scp
to transfer the JAR file to the Lightsail instance: - Alternatively, you can use tools like FileZilla or WinSCP to upload the file.
- On your local machine, use
Step 6: Run the Spring Boot Application
Once the JAR file is uploaded to the instance, you can start the Spring Boot application.
Run the JAR file:
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:
- If you'd like to run your application on a different port, you can specify it when running the JAR:
Check the status of the application:
- You can use
ps aux | grep java
to see if the Java process is running.
- You can use
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.
- Open a web browser and go to
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.
Create a service file:
Add the following content to the service file:
Reload Systemd and start the service:
Check the status of your 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.
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.
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.