Secrets, like passwords and API keys, are sensitive information that should be stored in a secure, encrypted storage which is access-controlled and auditable. On Google Cloud, you can use Secret Manager, a managed service, to securely store the secrets and control access to individual secrets using IAM.
In Spring Boot, you can use Spring Cloud GCP to easily access these secrets by referring to them as any other Spring properties.
In this lab, you store a secret in Secret Manager, build a Spring Boot app that retrieves and displays the secret, and deploy that app to Cloud Run.
What you'll learn
In this lab, you learn how to:
Create a Spring Boot Java application.
Configure Secret Manager and create a secret.
Retrieve a secret from Secret Manager in a Spring Boot app.
Deploy a Spring Boot app to Cloud Run.
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
Time to complete the lab—remember, once you start, you cannot pause a lab.
Note: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.
How to start your lab and sign in to the Google Cloud console
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method.
On the left is the Lab Details pane with the following:
The Open Google Cloud console button
Time remaining
The temporary credentials that you must use for this lab
Other information, if needed, to step through this lab
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account.
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}}
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}}
You can also find the Password in the Lab Details pane.
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials.
Note: Using your own Google Cloud account for this lab may incur extra charges.
Click through the subsequent pages:
Accept the terms and conditions.
Do not add recovery options or two-factor authentication (because this is a temporary account).
Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Note: To access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
Click Activate Cloud Shell at the top of the Google Cloud console.
Click through the following windows:
Continue through the Cloud Shell information window.
Authorize Cloud Shell to use your credentials to make Google Cloud API calls.
When you are connected, you are already authenticated, and the project is set to your Project_ID, . The output contains a line that declares the Project_ID for this session:
Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
(Optional) You can list the active account name with this command:
gcloud auth list
Click Authorize.
Output:
ACTIVE: *
ACCOUNT: {{{user_0.username | "ACCOUNT"}}}
To set the active account, run:
$ gcloud config set account `ACCOUNT`
(Optional) You can list the project ID with this command:
gcloud config list project
Output:
[core]
project = {{{project_0.project_id | "PROJECT_ID"}}}
Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Create a new Spring Boot REST service
Spring Boot simplifies the creation of self-contained, production-ready Spring applications, ready to run without extensive configuration.
Spring Initializr provides a straightforward path for creating a Spring Boot application. You can use curl to create the application by sending a curl request to the Spring Initializr service.
In this task, you use the command line to generate a new Spring Boot application.
This command downloads a Spring Boot project configuration from Spring Initializr, saves it as a zip file, unpacks it into a subdirectory, and then changes the current directory to the subdirectory.
curl https://start.spring.io/starter.zip : Fetch a Spring Boot project archive in ZIP format from the Spring Initializr service.
-d type=maven-project : This specifies that the generated project should use Maven as its build system.
-d packaging=jar : This indicates that the application should be packaged as a JAR file.
-d bootVersion=3.3.5 : This sets the Spring Boot version to 3.3.5.
-d dependencies=web,cloud-gcp : This defines the project's dependencies. web includes Spring Web support for creating web applications, and cloud-gcp adds the necessary dependencies for interacting with Google Cloud Platform services.
-d javaVersion=21 : This specifies that the project should use Java version 21.
-d name=HelloSecretManager : This sets the project name to "HelloSecretManager".
-d artifactId=HelloSecretManager : This sets the Maven artifact ID to "HelloSecretManager".
-o ~/hello-secret-manager.zip : This curl option redirects the downloaded zip file to a local file named hello-secret-manager.zip.
Create and test a REST controller
In Cloud Shell, to create a new class file, run the following command:
To start the REST service, run the following command:
cd ~/hello-secret-manager
./mvnw -DskipTests spring-boot:run | tee verify_response.txt
The Maven wrapper command invokes mvnw in the current directory. Any tests would be skipped. The Maven goal spring-boot:run compiles the code, and then packages and launches the application.
To access the REST service in the web browser, click Web Preview, and then select Preview on port 8080.
A new tab is opened in the browser, and the application is running. This is the root URL, which displays "Hi World!"
To quit the application, in Cloud Shell, enter CTRL-C.
Verify progress
To allow progress to be verified, run the following commands to copy the files to the Cloud Storage bucket named .
This starter simplifies the process of accessing secrets stored in Secret Manager from your Spring Boot application, allowing you to easily retrieve secrets as if they were regular application properties.
Add an application property
Navigate to ~/hello-secret-manager/src/main/resources/application.properties.
Below the spring.application.name property, add the following property:
spring.config.import=sm://
The property configures a Spring PropertySource for accessing Secret Manager secrets.
Modify the controller code to use the secret
Navigate to ~/hello-secret-manager/src/main/java/com/example/HelloSecretManager/HelloSecretManagerController.java.
Insert the following import statement beneath the other imports:
Navigate to ~/hello-secret-manager/src/main/resources/application.properties.
Below the spring.config.import property, add the following property:
server.port=${PORT:8080}
The property specifies that the environment variable PORT will be used as the port that listens for requests. By default, Cloud Run requests are sent to port 8080, so that is the default used.
Specify Java version in the build project descriptor
A build project descriptor file provides information to Cloud Build for the building of the containeraized app.
To create the build project descriptor file, run the following command:
cat > ~/hello-secret-manager/project.toml <<EOF
[[build.env]]
name = "GOOGLE_RUNTIME_VERSION"
value = "21"
EOF
The project.toml file in the app root directory specifies that the GOOGLE_RUNTIME_VERSION environment variable will be set to 21, indicating that Java version 21 should be used to compile the application.
Create a service account for the Cloud Run service
To ensure that only the required permissions are given to the Cloud Run app, a service account is created for the app.
To create a service account named hello-secret-manager-run-sa, run the following command:
gcloud iam service-accounts create hello-secret-manager-run-sa
To allow this service account to access secrets in Secret Manager, run the following command:
When the Cloud Run app is deployed, it should use this new version of the secret.
Deploy the app to Cloud Run
To deploy the app to Cloud Run, run the following command:
gcloud run deploy hello-secret-manager \
--region={{{ project_0.default_region | REGION }}} \
--allow-unauthenticated \
--service-account=hello-secret-manager-run-sa@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com \
--source .
This command deploys the app to Cloud Run with the name hello-secret-manager.
--region : The region for deployment.
--allow-unauthenticated : Makes your Cloud Run service publicly accessible without requiring any authentication. This flag should not be used for sensitive applications.
--service-account : Specifies the service account, and therefore the permissions allowed by the app.
--source : Specifies the source root directory. Specifying the source directory instead of a container indicates that Cloud Build should build the container.
After a few minutes, the deployment completes.
To get the application URL, run the following command:
echo "https://hello-secret-manager-$(gcloud projects describe $GOOGLE_CLOUD_PROJECT --format="value(projectNumber)").{{{ project_0.default_region | REGION }}}.run.app"
Click the link created by the previous command.
A new tab opens and the message "Greetings World!" is displayed.
Click Check my progress to verify the objective.
Deploy the app to Cloud Run
Congratulations!
In this lab, you stored a secret in Secret Manager, built a Spring Boot app that retrieves and displays the secret, and deployed that app to Cloud Run.
Next steps / Learn more
Check out the following resource to learn more about using Spring Boot on Google Cloud:
Copyright 2025 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
Lab membuat project dan resource Google Cloud untuk jangka waktu tertentu
Lab memiliki batas waktu dan tidak memiliki fitur jeda. Jika lab diakhiri, Anda harus memulainya lagi dari awal.
Di kiri atas layar, klik Start lab untuk memulai
Gunakan penjelajahan rahasia
Salin Nama Pengguna dan Sandi yang diberikan untuk lab tersebut
Klik Open console dalam mode pribadi
Login ke Konsol
Login menggunakan kredensial lab Anda. Menggunakan kredensial lain mungkin menyebabkan error atau dikenai biaya.
Setujui persyaratan, dan lewati halaman resource pemulihan
Jangan klik End lab kecuali jika Anda sudah menyelesaikan lab atau ingin mengulanginya, karena tindakan ini akan menghapus pekerjaan Anda dan menghapus project
Konten ini tidak tersedia untuk saat ini
Kami akan memberi tahu Anda melalui email saat konten tersedia
Bagus!
Kami akan menghubungi Anda melalui email saat konten tersedia
Satu lab dalam satu waktu
Konfirmasi untuk mengakhiri semua lab yang ada dan memulai lab ini
Gunakan penjelajahan rahasia untuk menjalankan lab
Gunakan jendela Samaran atau browser pribadi untuk menjalankan lab ini. Langkah ini akan mencegah konflik antara akun pribadi Anda dan akun Siswa yang dapat menyebabkan tagihan ekstra pada akun pribadi Anda.
In this lab, you store a secret in Secret Manager, then build simple Spring Boot microservices that retrieve the secret.