使用 Terraform 部署 VPC

瞭解如何使用基礎架構管理工具部署虛擬私有雲 (VPC)。

本快速入門導覽課程會使用儲存在公開 GitHub 存放區中的 Terraform 設定。這項設定會定義要佈建的 VPC。

事前準備

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Set up authentication:

    1. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with a name for the service account.

    2. Grant the roles/config.agent IAM role to the service account:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/config.agent

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  12. Make sure that billing is enabled for your Google Cloud project.

  13. Set up authentication:

    1. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with a name for the service account.

    2. Grant the roles/config.agent IAM role to the service account:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/config.agent

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account

授予設定中資源的權限

您已授予執行 Infra Manager 所需的權限,但也需要授予部署設定中所述資源的專屬權限。

授予 VPC 權限,這是在 Terraform 設定中定義的資源。

gcloud projects add-iam-policy-binding PROJECT_ID \
    --member=serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \
    --role=roles/compute.networkAdmin

更改下列內容:

  • SERVICE_ACCOUNT_NAME:服務帳戶名稱。
  • PROJECT_ID:您的專案 ID。

預覽部署作業

您可以在建立部署作業前先建立部署作業的預覽。您可以使用這項預覽功能,驗證預計要配置的資源。

以下指令會填入四個沒有預設值的值。這些值分別是:專案 ID、服務帳戶名稱、位置 us-central1,以及您要建立的網路名稱 quickstart-vpc

如要建立預覽畫面,請使用下列指令:

    gcloud infra-manager previews create projects/PROJECT_ID/locations/us-central1/previews/quickstart-preview \
        --service-account projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \
        --git-source-repo=https://github.com/terraform-google-modules/terraform-google-network \
        --git-source-directory=modules/vpc \
        --git-source-ref=main \
        --input-values=project_id=PROJECT_ID,network_name=quickstart-vpc

建立預覽後,您可以查看預覽結果。本快速入門課程會略過這個步驟,但如需更多詳細資訊,請參閱匯出並查看預覽結果

建立 Deployment

使用 Infra Manager 建立部署作業。這表示 Infra Manager 會佈建 Terraform 設定中定義的資源。

本快速入門的設定包含四個沒有預設值的值。下列指令會新增以下值:專案 ID、服務帳戶名稱、位置 us-central1 以及您要建立的網路名稱 quickstart-vpc

gcloud infra-manager deployments apply projects/PROJECT_ID/locations/us-central1/deployments/quickstart-deployment \
    --service-account=projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \
    --git-source-repo=https://github.com/terraform-google-modules/terraform-google-network \
    --git-source-directory=modules/vpc \
    --git-source-ref=main \
    --input-values=project_id=PROJECT_ID,network_name=quickstart-vpc

部署完成後,您會看到:

Creating the deployment...done

虛擬私有雲現在已建立並設定,如設定所述。

在 Cloud Build 中查看建構結果

如要查看 Infra Manager 用來建立部署作業的 Cloud Build 工作,請在 Google Cloud 控制台中開啟「Build History」(建構歷史記錄) 頁面:

開啟「Build History」(建構記錄) 頁面

查看部署作業狀態

部署作業完成後,您可以查看部署作業的說明,瞭解相關資訊,包括狀態。

查看部署作業的說明:

gcloud infra-manager deployments describe projects/PROJECT_ID/locations/us-central1/deployments/quickstart-deployment

輸出內容包含部署作業的詳細資料,包括部署作業的時間戳記、最新修訂版本的名稱和狀態。

您會看到狀態為 ACTIVE

查看已佈建虛擬私有雲的詳細資料

查看已佈建虛擬私有雲的詳細資料:

gcloud infra-manager resources list --revision=projects/PROJECT_ID/locations/us-central1/deployments/quickstart-deployment/revisions/REVISION_ID

REVISION_ID 替換為最新修訂版本的 ID。除非您多次部署,否則這個 ID 會是 r-0。您可以在上一個部分的部署說明中,查看最新修訂版本的 ID。

在控制台中查看 VPC

在主控台中查看 VPC:

前往「VPC networks」(虛擬私有雲網路)

您會看到名為 quickstart-vpc 的虛擬私有雲。這是 Infra Manager 佈建的 VPC。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請務必刪除不再需要的資源。

刪除虛擬私有雲

刪除虛擬私有雲和部署項目的中繼資料:

gcloud infra-manager deployments delete projects/PROJECT_ID/locations/us-central1/deployments/quickstart-deployment

選用步驟:刪除專案

如果您在新的 Google Cloud 專案中部署解決方案,且不再需要該專案,請完成下列步驟將其刪除:

  1. 前往 Google Cloud 控制台的「管理資源」頁面。

    前往「管理資源」

  2. 在專案清單中選取要刪除的專案,然後按一下「刪除」
  3. 輸入提示中的專案 ID,然後按一下「Shut down」

後續步驟