使用 Terraform 部署 VPC

瞭解如何使用 Infrastructure Manager 部署虛擬私有雲 (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. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  4. 如要初始化 gcloud CLI,請執行下列指令:

    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. Enable the Infrastructure Manager API:

    gcloud services enable config.googleapis.com
  8. 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
  9. Install the Google Cloud CLI.

  10. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  11. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  12. 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.

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

  14. Enable the Infrastructure Manager API:

    gcloud services enable config.googleapis.com
  15. 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
  16. 授予設定中資源的權限

    您已授予執行 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」(建構記錄) 頁面:

    開啟「建構記錄」頁面

    查看部署狀態

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

    查看部署作業的說明:

    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 的 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」(關閉)

    後續步驟