全部產品
Search
文件中心

Container Registry:Terraform整合樣本

更新時間:Jun 30, 2024

本文介紹如何使用Terraform建立容器鏡像企業版。

前提條件

已安裝並配置Terraform

配置許可權

執行Terraform模板需要RAM使用者權限,我們需要建立一個RAM使用者,並擷取AccessKey,添加權限原則。您需要為RAM使用者添加AliyunContainerRegistryFullAccess許可權,更多詳情請參見授予RAM使用者系統策略

編寫模板

建立一個名為main.tf檔案,填入以下內容。

resource "alicloud_cr_ee_instance" "default" {
  payment_type   = "Subscription"              # 聲明執行個體的付費類型。
  period         = 1                           # 聲明預付費資源的購買時間長度。
  renew_period   = 0                           # 設定自動續約的時間長度。0表示不自動續訂。
  renewal_status = "ManualRenewal"             # 設定續約策略,ManualRenewal,表示執行個體將不會自動續約,需要手動續約。
  instance_type  = "Advanced"                  # 執行個體的類型。Advanced,選擇了企業版中的進階版執行個體。
  instance_name  = "terraform-example"         # 執行個體的名稱。
}

更多關於alicloud_cr_ee_instance資源類型的配置資訊,請參見alicloud_cr_ee_instance

運行模板

  1. 執行以下命令,初始化Terraform運行環境。

    terraform init

    返回資訊如下,Terraform初始化成功。

    Initializing provider plugins...
    
    The following providers do not have any version constraints in configuration,
    so the latest version was installed.
    
    To prevent automatic upgrades to new major versions that may contain breaking
    changes, it is recommended to add version = "..." constraints to the
    corresponding provider blocks in configuration, with the constraint strings
    suggested below.
    
    * provider.alicloud: version = "~> 1.209"
    
    Terraform has been successfully initialized!
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  2. 執行以下命令,產生資源規劃。

    terraform plan

    返回資訊如下,資源規劃產生成功。

    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    ...
    Plan: 1 to add, 0 to change, 0 to destroy.
    ...
  3. 執行以下命令,建立容器鏡像企業版。

    terraform apply

    返回資訊如下,輸入yes,按Enter鍵,執行個體建立成功。

    ...
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    ...
    alicloud_cr_ee_instance: Creation complete after 1m26s [id=************]
    
    Apply complete! Resources: 1 added, 0 changed, 1 destroyed.