arrow_back

Developer Essentials: Creating Secrets with Secret Manager

登录 加入
访问 700 多个实验和课程

Developer Essentials: Creating Secrets with Secret Manager

实验 30 分钟 universal_currency_alt 1 积分 show_chart 入门级
info 此实验可能会提供 AI 工具来支持您学习。
访问 700 多个实验和课程

gem-secret-manager-create-secrets

Google Cloud self-paced labs logo

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.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

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 YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

  2. Your output should now look like this:

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Overview

In this lab, you'll learn how to create and manage secrets using Google Cloud Secret Manager. You will create a secret, store a secret value, and retrieve the secret value. This lab assumes you have basic familiarity with Google Cloud and the command line.

Task 1. Enable the Secret Manager API

Before using Secret Manager, you need to enable the API for your project.

  1. Enable the Secret Manager API using the following command.

    gcloud services enable secretmanager.googleapis.com --project={{{ project_0.project_id | "PROJECT_ID" }}} Note:
    This command enables the Secret Manager API for your project, allowing you to create and manage secrets.

Task 2. Create a Secret

Create a new secret in Secret Manager.

  1. Create a secret named my-secret.

    gcloud secrets create my-secret --project={{{ project_0.project_id | "PROJECT_ID" }}} Note:
    This command creates a secret named `my-secret` in your Google Cloud project. Secrets are used to store sensitive information, such as passwords, API keys, and certificates.

Task 3. Add a Secret Version

Add a version to the secret with the actual secret value.

  1. Add a new version to my-secret with the value super-secret-password.

    echo -n "super-secret-password" | gcloud secrets versions add my-secret --data-file=- --project={{{ project_0.project_id | "PROJECT_ID" }}} Note:
    This command adds a new version to the `my-secret` secret, storing the value "super-secret-password". The `-n` flag in the `echo` command prevents adding a newline character to the secret value. The `--data-file=-` flag specifies that the data is read from standard input.

Task 4. Access the Secret Value

Retrieve the secret value from Secret Manager.

  1. Access the latest version of my-secret and print the secret value.

    echo "$(gcloud secrets versions access latest --secret=my-secret --project={{{ project_0.project_id | "PROJECT_ID" }}})" Note:
    This command retrieves the secret value of the latest version of `my-secret` and prints it to the console. This allows you to verify the stored secret.
  2. Alternatively, you can store the secret value into an environment variable.

    export MY_SECRET=$(gcloud secrets versions access latest --secret=my-secret --project={{{ project_0.project_id | "PROJECT_ID" }}}) Note:
    This command retrieves the secret value and stores it in an environment variable named `MY_SECRET`. This allows you to use the secret in subsequent commands or scripts without directly exposing the secret value in your code.
  3. Access the environment variable and print the secret value.

    echo "${MY_SECRET}" Note:
    This command retrieves the environment variable of `my-secret`. This allows you to verify the environment variable secret.

Congratulations!

You have successfully created a secret in Secret Manager, stored a secret value, and retrieved it. Secret Manager is a secure and convenient way to manage sensitive data in your Google Cloud projects.

Additional Resources

Manual Last Updated Jul 25, 2025

Lab Last Tested Jul 25, 2025

准备工作

  1. 实验会创建一个 Google Cloud 项目和一些资源,供您使用限定的一段时间
  2. 实验有时间限制,并且没有暂停功能。如果您中途结束实验,则必须重新开始。
  3. 在屏幕左上角,点击开始实验即可开始

使用无痕浏览模式

  1. 复制系统为实验提供的用户名密码
  2. 在无痕浏览模式下,点击打开控制台

登录控制台

  1. 使用您的实验凭证登录。使用其他凭证可能会导致错误或产生费用。
  2. 接受条款,并跳过恢复资源页面
  3. 除非您已完成此实验或想要重新开始,否则请勿点击结束实验,因为点击后系统会清除您的工作并移除该项目

此内容目前不可用

一旦可用,我们会通过电子邮件告知您

太好了!

一旦可用,我们会通过电子邮件告知您

一次一个实验

确认结束所有现有实验并开始此实验

使用无痕浏览模式运行实验

请使用无痕模式或无痕式浏览器窗口运行此实验。这可以避免您的个人账号与学生账号之间发生冲突,这种冲突可能导致您的个人账号产生额外费用。