查看資產
本文說明如何在 Cloud Shell 中使用 Cloud Asset Inventory 和 Google Cloud CLI 查看資產。
事前準備
如要完成本文中的操作說明,您需要啟用 Cloud Asset Inventory API,並設定存取 Cloud Asset Inventory 和 Compute Engine 的權限。
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
如要使用現有專案進行本指南中的操作,請確認您具有完成本指南所需的權限。如果您建立新專案,則已具備必要權限。
-
Enable the Cloud Asset Inventory API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
如要使用現有專案進行本指南中的操作,請確認您具有完成本指南所需的權限。如果您建立新專案,則已具備必要權限。
-
Enable the Cloud Asset Inventory API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
如要查看資產中繼資料:Cloud Asset 檢視者 (
roles/cloudasset.viewer) -
如要建立及刪除 Compute Engine VM 執行個體:
- Compute 執行個體管理員 (v1) (
roles/compute.instanceAdmin.v1) -
服務帳戶使用者 (
roles/iam.serviceAccountUser)
- Compute 執行個體管理員 (v1) (
-
如要列出資產中繼資料:
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
-
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
點選「建立執行個體」。
輸入執行個體的名稱。
點選「建立」。
等待執行個體建立完成。
在 Google Cloud 控制台中,按一下「啟用 Cloud Shell」。
在 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 控制台的「VM instances」(VM 執行個體) 頁面。
找出您建立的 VM 執行個體,然後按一下該列中的「More actions」(更多動作)。
按一下「Delete」(刪除),然後確認刪除。
- 如要瞭解更多查看資產的相關選項,請參閱「列出資產」。
必要的角色
如要取得完成這些操作說明所需的權限,請要求管理員在組織、資料夾或專案中,授予您下列 IAM 角色:
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
這些預先定義的角色具備完成這些操作說明所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:
所需權限
如要完成這些操作說明,必須具備下列權限:
查看資產
在所選專案中建立 Compute Engine VM 執行個體,以便在 Cloud Asset Inventory 中查看:
列出專案中的所有 VM 執行個體:
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。