查看素材資源

本文說明如何在 Cloud Shell 中,使用 Cloud Asset Inventory 和 Google Cloud CLI 查看資產。

事前準備

如要完成本文件中的操作說明,您必須啟用 Cloud Asset Inventory API,並設定 Cloud Asset Inventory 和 Compute Engine 的存取權限。

  1. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Enable the Cloud Asset Inventory API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  7. Enable the Cloud Asset Inventory API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

必要的角色

如要取得完成這些操作說明所需的權限,請要求管理員在組織、資料夾或專案中,授予您下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

這些預先定義的角色具備完成這些操作說明所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:

所需權限

您必須具備下列權限,才能完成這些操作說明:

  • 如要列出資產中繼資料,請按照下列步驟操作: cloudasset.assets.listResource
  • 如要建立 Compute Engine VM 執行個體,請按照下列步驟操作:
    • compute.disks.create
    • compute.instances.create
    • compute.instances.delete
    • compute.instances.setServiceAccount
    • compute.projects.get
    • compute.subnetworks.use
    • compute.subnetworks.useExternalIp
    • compute.zoneOperations.get

您或許還可透過自訂角色或其他預先定義的角色取得這些權限。

查看素材資源

在您選擇的專案中建立 Compute Engine VM 執行個體,以便在 Cloud Asset Inventory 中查看:

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  2. 點選「建立執行個體」。

  3. 輸入執行個體的名稱。

  4. 點選「建立」。

  5. 等待執行個體建立完成。

列出專案中的所有 VM 執行個體:

  1. 在 Google Cloud 控制台,按一下「 啟用 Cloud Shell」。

  2. 在 Cloud Shell 中執行下列指令:

    gcloud asset list \
        --project=PROJECT_ID \
        --asset-types=compute.googleapis.com/Instance \
        --content-type=resource
    

    將 PROJECT_ID 替換為要列出資產的專案 ID。

    在上述程式碼範例中,資產類型 compute.googleapis.com/Instance 僅用於列出 Compute Engine VM 執行個體。如要查看所有資產類型,請參閱「資產類型」。

    內容類型也已設為 resource。這會指定回應中也應傳回 resource 中繼資料。如未設定內容類型,系統只會傳回各項資產的基本資訊,例如資產名稱、上次更新時間,以及所屬專案。

清除所用資源

為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  2. 在您建立的 VM 執行個體名稱所在資料列中,按一下 「More actions」(更多動作)。

  3. 按一下「Delete」(刪除),然後確認刪除。

後續步驟

  • 如要探索更多與查看資產相關的選項,請參閱「列出資產」。