Create a Java Backend Application from 0 to 1 and Deploy It to AWS

2024-10-05

Project Background

I am preparing to create a personal blog service. Initially, I will develop and deploy the backend service, and later, after getting familiar with the frontend, I will develop the frontend project. Eventually, I will integrate both frontend and backend to provide a complete personal blog service.

Project Parameters

  • Backend Programming Language: Java
  • Backend Framework: Spring Boot
  • Web Server: Tomcat
  • Cloud Service: AWS
  • Container: Docker
  • Code Management: GitHub
  • Package Management: Gradle

Environment Configuration

  • Install Java JDK, choose version 17, and configure the environment variable: https://www.oracle.com/java/technologies/downloads/#java17
  • Install Git
  • Install Gradle package management tool: https://gradle.org/releases/
  • Install Docker Desktop: https://www.docker.com/products/docker-desktop/ https://juejin.cn/post/7382484880288399397
  • Install AWS CLI: https://aws.amazon.com/cn/cli/

Steps

  1. Use Spring Initializr to create the initial Spring Boot project structure. I selected a few commonly used packages such as spring-web, spring-actuator, spring-jdbc, and spring-security. After the project structure is generated, download it to the local machine.
  2. Modify the Maven repository configuration in the file and the Gradle wrapper properties mirror configuration. Prioritize using domestic mirrors for easier local compilation, packaging, and debugging. Be aware of the issue where the Gradle project version and the global version might not match.
  3. Test the application by running it locally.
  4. Package the generated JAR file into a Docker image. The main steps are as follows:
    • Create a Dockerfile
      • Create a file named Dockerfile in your project root directory with the following content (assuming your project is a Spring Boot application):
      • Use the base image
        FROM openjdk:17-jdk-slim
        Set the working directory
        WORKDIR /app
        Copy the project's JAR file into the image
        COPY build/libs/your-project-name.jar app.jar
        Expose the port the application runs on
        EXPOSE 8080
        Specify the command to run when the container starts
        CMD ["java", "-jar", "app.jar"]
    • Build the Docker image
      • Run the following command in the project root directory to build the Docker image:
        docker build -t your-image-name .
  5. Install AWS CLI. For Debian-based systems, the steps are as follows:
    • Update your package index
      sudo apt-get update
    • Install curl and unzip (if not already installed)
      sudo apt-get install curl unzip -y
    • Use curl to download the latest AWS CLI installation package
      curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    • Unzip the downloaded zip file
      unzip awscliv2.zip
    • Run the installation script
      sudo ./aws/install
    • After installation, verify that AWS CLI was successfully installed
      aws --version
    • Finally, configure AWS CLI using the following command
      aws configure
      Enter your AWS Access Key, Secret Key, region, and output format as prompted.
  6. AWS CLI Initialization
    • Log in to AWS ECR
      • Ensure you are logged into ECR:
        aws ecr get-login-password --region us-west-1 | docker login --username AWS --password-stdin 767397856910.dkr.ecr.us-west-1.amazonaws.com
    • Create an ECR repository (if not created already)
      • You can create a new ECR repository with the following command:
        aws ecr create-repository --repository-name your-repo-name --region us-east-1
    • Ensure your AWS CLI is correctly configured by using the following command to check your configuration:
      aws configure
    • Make sure your AWS IAM user or role has the necessary permissions to push images to ECR. At a minimum, the following permissions are required: ecr:PutImage
      ecr:InitiateLayerUpload
      ecr:UploadLayerPart
      ecr:CompleteLayerUpload
      ecr:DescribeRepositories
  7. Push the image to ECR
    • First, tag your image:
      docker tag your-image-name:latest <account-id>.dkr.ecr.us-west-1.amazonaws.com/your-repo-name:latest
    • Then push the image to ECR:
      docker push <account-id>.dkr.ecr.us-east-1.amazonaws.com/your-repo-name:latest
  8. Log into EC2. If it's not Amazon Linux, you will also need to install AWS CLI and grant the necessary permissions.
  9. Install Docker on EC2. For Debian-based systems, the steps are as follows:
    • Update your package index
      sudo apt-get update
    • Install some necessary dependencies that allow apt to use HTTPS
      sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y
    • Add Docker's GPG key
      curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
    • Add Docker's official APT repository
      sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
    • Update your package index again to include the new Docker repository
      sudo apt-get update
    • Install Docker
      sudo apt-get install docker-ce -y
    • Start the Docker service and enable it to start on system boot
      sudo systemctl start docker
      sudo systemctl enable docker
    • Verify Docker installation with the following command
      docker --version
  10. Pull and run the image
    docker pull <account-id>.dkr.ecr.us-east-1.amazonaws.com/your-repo-name:latest
    docker run -d -p 8080:8080 -e PARAM_KEY=123 -e PARAM_KEY=456 <account-id>.dkr.ecr.us-east-1.amazonaws.com/your-repo-name:latest
  11. Access the application
    • Test the actuator health endpoint using a browser, Postman, or command line
      curl -i http://localhost:8080/actuator/health