Automating Spring Boot Application Deployment with MySQL on Amazon Linux
Deploying a Spring Boot application on an Amazon Linux EC2 instance can be a straightforward task, but adding automated MySQL setup, secure GitHub repository access, and systemd service management can take your setup to the next level. In this blog post, I’ll walk you through a bash script that automates the entire process—from installing necessary packages to setting up your application as a service.
Overview
This script accomplishes the following tasks:
- Updates the system and installs required packages: Java 17, Git, Maven, and MySQL.
- Sets up MySQL: Installs MySQL, creates a database, and configures a user with the necessary privileges.
- Clones your Spring Boot application from GitHub: Securely clones the repository using a GitHub token.
- Builds and packages the Spring Boot application: Uses Maven to package your application.
- Creates and configures a systemd service: Ensures your application starts on boot and can be managed like any other service.
- Deploys the application and sets up a deploy script: A simple script is created to allow easy redeployment of updated code.
The Script
Here’s the bash script that automates the entire setup:
#!/bin/bash # Update the package repository and install necessary packages sudo yum update -y sudo yum install -y java-17 git maven # Set variables APP_DIR="/opt/my-spring-boot-app" GITHUB_TOKEN="your-github-token-here" # Set your GitHub token here REPO_URL="https://$GITHUB_TOKEN@github.com/
/api.git" SERVICE_NAME="my-spring-boot-app" JAR_NAME="app-0.0.1-SNAPSHOT.jar" SUBDIR="app" # Subdirectory where the pom.xml is located DB_NAME="myappdb" DB_USER="myappuser" DB_PASSWORD="mypassword" # Install MySQL from the official MySQL repository sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm sudo yum install -y mysql-community-server # Start MySQL service and enable it to start on boot sudo systemctl start mysqld sudo systemctl enable mysqld # Get the temporary root password generated by MySQL TEMP_PASSWORD=$(sudo grep 'temporary password' /"keyword">var/log/mysqld.log | awk '{print $NF}') # Run the MySQL secure installation with the temporary password sudo mysql --connect-expired-password -uroot -p"$TEMP_PASSWORD" <'root'@'localhost' IDENTIFIED BY '$DB_PASSWORD'; UNINSTALL COMPONENT 'file://component_validate_password'; ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY '$DB_PASSWORD'; CREATE DATABASE $DB_NAME; CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASSWORD'; GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost'; FLUSH PRIVILEGES; EXIT; EOF # Create the application directory "keyword">if it doesn't exist if [ ! -d "$APP_DIR" ]; then sudo mkdir -p $APP_DIR fi # Clone the repository if it doesn't already exist "keyword">if [ ! -d "$APP_DIR/.git" ]; then sudo git clone $REPO_URL $APP_DIR fi # Navigate to the application directory cd $APP_DIR # Pull the latest changes sudo git pull origin main # Navigate to the subdirectory where the pom.xml is located cd $SUBDIR # Build the application sudo mvn clean package # Create a systemd service file sudo bash -c "cat > /etc/systemd/system/$SERVICE_NAME.service < 143 Restart=on-failure [Install] WantedBy=multi-user.target EOF" # Reload systemd to recognize the new service sudo systemctl daemon-reload # Enable the service to start on boot sudo systemctl enable $SERVICE_NAME # Start the Spring Boot service sudo systemctl start $SERVICE_NAME # Set up the deployment script sudo bash -c "cat > /usr/local/bin/deploy.sh <# Make the deployment script executable sudo chmod +x /usr/local/bin/deploy.sh # Reboot the system to ensure everything is set up correctly sudo reboot
Step-by-Step Explanation
1. Updating the System and Installing Required Packages
The script begins by updating the package repository to ensure you’re installing the latest versions of Java, Git, and Maven:
sudo yum update -y sudo yum install -y java-17 git maven
2. Setting Up MySQL
MySQL isn’t always available in the default repositories, so the script adds the official MySQL repository and installs MySQL from there:
sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm sudo yum install -y mysql-community-server
After installation, MySQL is started, and its secure installation is automated using a temporary root password:
TEMP_PASSWORD=$(sudo grep 'temporary password' /"keyword">var/log/mysqld.log | awk '{print $NF}')
3. Cloning the Spring Boot Application
To clone your private GitHub repository securely, the script uses a GitHub token passed as a variable:
GITHUB_TOKEN="your-github-token-here" REPO_URL="https://$GITHUB_TOKEN@github.com/
/api.git"
This ensures that your credentials are not hardcoded into the script, making it more secure.
4. Building the Application
The script then navigates to the application directory and builds the Spring Boot application using Maven:
sudo mvn clean package
5. Setting Up the Application as a Systemd Service
A systemd service file is created to manage the Spring Boot application, allowing it to start on boot and be controlled with standard systemd commands:
sudo bash -c "cat > /etc/systemd/system/$SERVICE_NAME.service <
6. Deployment Script
The script sets up a simple deployment script that pulls the latest changes from the repository, rebuilds the application, and restarts the service:
sudo bash -c "cat > /usr/local/bin/deploy.sh <
This script can be executed anytime you need to deploy updates to your application.
Conclusion
This bash script simplifies the process of deploying a Spring Boot application on an Amazon Linux EC2 instance, making it ideal for hobby projects. It handles everything from MySQL installation and configuration to setting up your application as a systemd service. By integrating GitHub token-based authentication, it also ensures secure access to your private repositories.
While this setup is perfect for hobby projects or small personal applications, it's important to note that a production environment typically requires more robust solutions. For instance, using MySQL installed directly on your EC2 instance is convenient for development, but it lacks the reliability and scalability needed for a production application. In a production scenario, you would likely want to use Amazon RDS or another managed database service that provides automatic backups, scaling, and high availability.
This script is a great starting point for learning and experimenting with Spring Boot on AWS. As your project grows or transitions into production, consider upgrading your database and infrastructure to ensure data security, reliability, and performance.
Feel free to reach out if you have any questions or suggestions for improvements! Happy coding!