將已部署的資源匯入 Infrastructure Manager


本教學課程說明如何將 Google Cloud 資源匯入 Infrastructure Manager 部署作業。您只能匯入使用 Terraform 佈建的資源。

如果您有 Terraform 設定,但尚未在Google Cloud上部署,請參閱使用 Infrastructure Manager 部署基礎架構

本教學課程首先會使用 Terraform CLI,將虛擬私有雲 (VPC) 部署至Google Cloud 。接著,本教學課程會示範如何將這些已部署的資源匯入 Infrastructure Manager,以便將資源視為 Infrastructure Manager 部署作業進行管理。

您可以透過本教學課程中的範例,瞭解如何將其他Google Cloud 資源匯入 Infra Manager。如要將部署作業匯入 Infra Manager,用於佈建資源的 Terraform 設定必須符合限制,且資源必須使用支援的 Terraform 版本佈建。

費用

本教學課程使用下列 Google Cloud的計費元件:

事前準備

  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. Verify 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. Verify 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. 安裝Terraform支援版本
  17. 將資源部署至 Google Cloud

    您在本教學課程中部署的 Google Cloud 資源是 VPC。本節說明如何部署資源,做為將已部署資源匯入 Infra Manager 的範例。

    本教學課程使用 us-central1 區域。如要使用其他區域,可以使用 Infra Manager 執行的任何位置。如需有效位置清單,請參閱「Infrastructure Manager 位置」。

    授予設定中資源的權限

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

    授予權限,以便建立虛擬私有雲網路,也就是 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。

    使用 Terraform CLI 部署資源

    1. 使用下列 Terraform 設定建立虛擬私有雲網路。如要進行此操作,請執行以下指令:

      mkdir vpc_to_import
      cd vpc_to_import
      cat <<EOF > main.tf
      variable "project_id" {
        type = string
      }
      
      resource "google_compute_network" "vpc_network" {
        name = "my-custom-mode-network"
        project = var.project_id
      }
      EOF
      
    2. 使用 Terraform CLI 部署設定,方法是執行下列指令:

      terraform init
      echo "**************  TERRAFORM APPLY  ******************"
      terraform apply -var="project_id=PROJECT_ID" -auto-approve
      cd ..
      

    VPC 佈建完成後,您會看到以 Creation complete 開頭的輸出內容。

    Terraform 設定中定義的 VPC 現在已部署在 Google Cloud上。Terraform 會建立狀態檔案,這個檔案位於檔案系統中,與 main.tf 檔案相鄰。

    在 Infrastructure Manager 中建立預留位置部署作業

    如要將資源匯入 Infra Manager,您必須在 Infra Manager 中建立部署作業。

    本教學課程使用儲存空間 bucket 儲存 Terraform 設定。如果您是透過本教學課程瞭解如何將其他部署作業匯入 Infra Manager,則可將 Terraform 設定儲存在儲存空間 bucket、公開 Git 存放區或本機電腦上。

    1. 在儲存空間 bucket 中新增空白的 Terraform 設定:

      gcloud storage buckets create gs://import-deployment-configuration --project=PROJECT_ID
      
      mkdir placeholder_deployment
      cd placeholder_deployment
      cat <<EOF > main.tf
      EOF
      
      gcloud storage cp main.tf gs://import-deployment-configuration
      cd ..
      
    2. 使用 Infra Manager 建立部署作業。

      gcloud infra-manager deployments apply projects/PROJECT_ID/locations/us-central1/deployments/import-deployment \
      --gcs-source gs://import-deployment-configuration \
      --input-values project_id=PROJECT_ID \
      --service-account projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT
      

      部署完成後,您會看到:

      Creating the deployment...done
      

    您現在已擁有 Infra Manager 部署作業。接著,您需要將狀態檔案和 Terraform 設定新增至這個部署作業,以便由這個部署作業管理 VPC。

    匯入狀態檔和 Terraform 設定

    如要使用 Infra Manager 管理已部署的資源 (VPC),您需要將狀態檔案和 Terraform 設定新增至 Infra Manager。

    鎖定部署作業

    鎖定 Infra Manager 部署作業,以便變更其狀態。

    LOCK_ID=$(gcloud infra-manager deployments lock projects/PROJECT_ID/locations/us-central1/deployments/import-deployment --format="get(lockId)")
    

    將 Terraform 狀態檔案匯入 Infrastructure Manager

    匯入已部署資源的狀態檔案。您使用 Terraform CLI 部署 VPC 時,Terraform 會建立這個狀態檔案。

    將 Terraform 狀態檔案上傳至 Infra Manager 部署作業。

    SIGNED_STATE_UPLOAD_URL=$(gcloud infra-manager deployments import-statefile projects/PROJECT_ID/locations/us-central1/deployments/import-deployment --lock-id ${LOCK_ID} --format="get(signedUri)")
    cd vpc_to_import
    curl -s -X PUT --upload-file terraform.tfstate $SIGNED_STATE_UPLOAD_URL
    

    將 Terraform 設定上傳至 Cloud Storage 值區

    上傳 Terraform 狀態檔後,您還需要上傳用於建立 VPC 網路的 Terraform 設定。Infra Manager 需要 Terraform 狀態檔案,這樣在執行 Infra Manager 預覽時,就不會發現資源有任何變更。

    使用下列指令將 Terraform 設定上傳至 Cloud Storage:

    gcloud storage cp main.tf gs://import-deployment-configuration
    

    解鎖部署作業

    如果您是透過本教學課程瞭解如何將任何資源匯入 Infra Manager,請確保狀態檔案和 Terraform 設定沒有差異。如果狀態檔和 Terraform 設定之間有差異,請確認狀態檔說明您要部署的資源。解鎖部署作業後,如果狀態檔與 Terraform 設定之間有差異,Infra Manager 會建立或刪除資源,以符合狀態檔。

    您必須解鎖部署作業,才能使用 Infra Manager 管理資源。在本教學課程中,狀態檔案和 Terraform 設定描述的是相同設定,因此 Infra Manager 不會建立或刪除任何資源。

    執行下列指令,從部署作業釋放鎖定。

    gcloud infra-manager deployments unlock projects/PROJECT_ID/locations/us-central1/deployments/import-deployment \
    --project PROJECT_ID \
    --location us-central1 \
    --lock-id ${LOCK_ID}
    

    狀態檔案和 Terraform 設定已新增至 Infra Manager 部署作業,您已完成匯入已部署的資源。部署的虛擬私有雲現在由 Infra Manager 管理。

    預覽資源變更

    預覽部署作業,確認 Terraform 狀態和部署在 Google Cloud 上的資源是否同步。

    1. 使用下列指令建立預覽畫面:

      gcloud infra-manager previews create projects/PROJECT_ID/locations/us-central1/previews/import-deployment-preview \
      --gcs-source gs://import-deployment-configuration \
      --input-values project_id=PROJECT_ID \
      --deployment projects/PROJECT_ID/locations/us-central1/deployments/import-deployment \
      --service-account projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
      
    2. 取得預覽詳細資料,確認預覽已順利完成。

      gcloud infra-manager previews describe projects/PROJECT_ID/locations/us-central1/previews/import-deployment-preview
      
    3. 匯出預覽畫面,查看變更。

      SIGNED_BIN_PLAN_URL=$(gcloud infra-manager previews export projects/PROJECT_ID/locations/us-central1/previews/import-deployment-preview --format "get(result.binarySignedUri)")
      curl -X GET --output tfplan.out $SIGNED_BIN_PLAN_URL
      
    4. 確認本機有設定,並使用下列指令初始化:

      terraform init
      
    5. 執行 Terraform show,檢查 Infrastructure Manager 部署狀態與設定之間的變更。

      terraform show tfplan.out
      

    如果 Google Cloud 上的 VPC 資源和狀態檔案同步,則 terraform show 會輸出下列內容,驗證 Infrastructure Manager 在部署狀態和設定之間未發現任何變更。畫面會顯示類似以下的輸出:

    No changes. Your infrastructure matches the configuration.
    
    Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
    

    您已匯入部署的資源,現在這些資源由 Infra Manager 管理。舉例來說,您現在可以使用 Infra Manager 更新部署作業

    您也可以更新部署作業,驗證部署作業是否已成功匯入 Infra Manager。

    清除所用資源

    如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

    刪除專案

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

    刪除個別資源

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

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

    後續步驟