CodeGym /Courses /Module 5. Spring /Lecture 150: Deploying a Spring Boot application to cloud...

Lecture 150: Deploying a Spring Boot application to cloud services (AWS, GCP, Azure)

Module 5. Spring
Level 22 , Lesson 9
Available

Cloud services are the MVP (Most Valuable Players) of modern IT. They give us:

  • Easy scaling. If your users suddenly decide to hit your site all at once, you don't need to line up for hardware — the cloud will handle it for you.
  • Time and cost savings. No need to maintain your own infrastructure, pay for electricity, or upgrade hardware.
  • Flexibility. Clouds offer tons of services: from databases to machine learning. Want a database? Boom — it's ready.
  • Resilience. Service outage in the cloud? Forget sleepless nights — the cloud will recover things for you.

But don't forget: cloud ≠ magic. If you deploy bad code, the cloud will just spread it faster. Deploying to the cloud should be done thoughtfully.


Overview of cloud platforms

You can use many platforms to deploy Spring Boot applications. Today we'll look at the three most popular:

Platform Description
AWS Market leader in cloud tech. Has a huge set of services for containerization and deployment
GCP Easy Kubernetes integration, developer-friendly
Azure Microsoft's cloud platform. Great for enterprise solutions

Each of these platforms has its own perks and quirks. So let's work with each one and try to deploy our app.


Preparing for deployment

Before you start you'll need to do a few steps:

  1. Create an account on the cloud platform.
    • Signing up on AWS, GCP or Azure is your ticket to the cloud world. They usually offer free trial periods.
  2. Install the CLI for managing the cloud.
    • AWS CLI, Google Cloud SDK (gcloud) or Azure CLI — the tools we'll use to automate deployments.
  3. Prepare the Docker image.
    • In previous lectures we created a Docker image of our app. Let's remind how your Dockerfile looked:
      
      FROM openjdk:17-jdk-slim
      ARG JAR_FILE=target/my-spring-boot-app-1.0.jar
      COPY ${JAR_FILE} app.jar
      ENTRYPOINT ["java", "-jar", "/app.jar"]
      
    • You can build the image with:
      
      docker build -t my-spring-boot-app:1.0 .
      

Now let's "fly" through the clouds a bit.


Deploying to AWS

Creating an Elastic Container Registry (ECR)

AWS provides Elastic Container Registry to store Docker images.

  1. Login to AWS CLI:
    
    aws configure
    
    Enter Access Key, Secret Key and choose a region.
  2. Create a repository:
    
    aws ecr create-repository --repository-name my-spring-boot-app
    
  3. Get Docker login:
    
    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <your_repo_url>
    
  4. Push the Docker image to ECR:
    docker tag my-spring-boot-app:1.0 <your_repo_url>/my-spring-boot-app:1.0
    docker push <your_repo_url>/my-spring-boot-app:1.0
    

Deploying to Elastic Beanstalk

AWS Elastic Beanstalk makes app deployment easier. Do the following:

  1. Install the Elastic Beanstalk CLI.
  2. Initialize the project:
    
    eb init
    
    Choose Docker as the platform.
  3. Start the app:
    
    eb create spring-boot-env
    

AWS will set up the infra, pull your container and run the app.


Deploying to Google Cloud Platform (GCP)

Uploading the Docker image to Google Container Registry (GCR)

  1. Log in to GCP:
    
    gcloud auth login
    gcloud config set project <your_project_id>
    
  2. Push the Docker image to GCR:
    
    docker tag my-spring-boot-app gcr.io/<your_project_id>/my-spring-boot-app:1.0
    docker push gcr.io/<your_project_id>/my-spring-boot-app:1.0
    

Deploying on Google Kubernetes Engine (GKE)

  1. Create a Kubernetes cluster:
    
    gcloud container clusters create spring-boot-cluster \
      --num-nodes=1
    
  2. Configure kubectl:
    
    gcloud container clusters get-credentials spring-boot-cluster
    
  3. Create a deployment.yaml file:
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: spring-boot-app
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: spring-boot-app
      template:
        metadata:
          labels:
            app: spring-boot-app
        spec:
          containers:
          - name: spring-boot-app
            image: gcr.io/<your_project_id>/my-spring-boot-app:1.0
            ports:
            - containerPort: 8080
    
  4. Apply the configurations:
    
    kubectl apply -f deployment.yaml
    
  5. Make sure the app is running:
    
    kubectl get pods
    

Deploying to Microsoft Azure

Uploading the image to Azure Container Registry (ACR)

  1. Create a resource in Azure for ACR:
    
    az acr create --resource-group myResourceGroup --name myRegistry --sku Basic
    
  2. Log in to ACR:
    
    az acr login --name myRegistry
    
  3. Push the Docker image:
    
    docker tag my-spring-boot-app myregistry.azurecr.io/my-spring-boot-app:1.0
    docker push myregistry.azurecr.io/my-spring-boot-app:1.0
    

Deploying to Azure App Service

  1. Create an App Service:
    
    az webapp create --resource-group myResourceGroup --plan myAppServicePlan \
      --name my-spring-boot-app --deployment-container-image-name myregistry.azurecr.io/my-spring-boot-app:1.0
    
  2. Set up automatic container updates:
    
    az webapp config container set --name my-spring-boot-app \
      --resource-group myResourceGroup --docker-custom-image-name myregistry.azurecr.io/my-spring-boot-app:1.0
    

Useful tips and common mistakes

  1. Don't forget about network settings. For example, on AWS or GCP you might forget to open security ports.
  2. Watch your image versioning. Don't use latest unless absolutely necessary.
  3. Monitor the logs. It's super convenient to do this in the cloud using built-in monitoring systems.

Now your app lives in the cloud! And not just lives — it's fully ready for real user load. P.S. If your service starts to hang, remember: sometimes clouds get into a "storm" too.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION