本文介紹如何使用Terraform建立容器鏡像企業版執行個體。
前提條件
已安裝並配置Terraform。
當前範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作方式請參見建立RAM使用者和建立AccessKey。
使用以下樣本為RAM使用者授權,需要為該RAM使用者授予以下許可權:管理Container RegistryAliyunContainerRegistryFullAccess的許可權。具體操作方式請參見為RAM使用者授權。
{ "Version": "1", "Statement": [ { "Action": "cr:*", "Resource": "*", "Effect": "Allow" }, { "Action": "bss:*", "Resource": "*", "Effect": "Allow" } ] }準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。
在Terraform Explorer中使用Terraform:阿里雲提供了Terraform的線上運行環境,您無需安裝Terraform,登入後即可線上使用和體驗Terraform。適用於零成本、快速、便捷地體驗和調試Terraform的情境。
Cloud Shell:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。
在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。
請確保Terraform版本不低於v0.12.28。如需檢查現有版本,請運行terraform --version命令。
本教程樣本包含的容器資源會產生一定費用,請在不需要時及時進行退訂。
使用的資源
alicloud_cr_ee_instance:建立容器鏡像。
使用Terraform建立容器鏡像企業版執行個體
本樣本將建立一個Container Registry企業版執行個體。
建立一個工作目錄,並且在工作目錄中建立以下名為
main.tf的設定檔。main.tf是Terraform主檔案,定義了將要部署的資源。以下是參數說明。參數名稱
是否必選
描述
樣本值
payment_type
否
容器鏡像企業版執行個體的訂閱類型。
Subscription
period
否
預付費周期。單位為月。有效取值包括:1、2、3、6、12、24、36、48、60。對於按年付費的產品,請輸入 12 的整數倍。
說明如果將
renewal_status設定為AutoRenewal(自動續約),那麼該值必須大於 0,否則自動續約將失效。1
renew_period
否
自動續約周期。單位為月,按年付費產品請輸入12的整數倍。單位:月。
1
renewal_status
否
自動續約狀態。取值:
AutoRenewal:自動續約。
ManualRenewal:手動續約。
預設
ManualRenewal。AutoRenewal
instance_type
是
容器鏡像企業版執行個體的類型。取值:
Basic:基礎執行個體。
Advanced:進階版執行個體。
Advanced
instance_name
是
容器鏡像企業版執行個體的名稱。
樣本中用
var.name變數表示。variable "region" { default = "cn-heyuan" } provider "alicloud" { region = var.region } variable "name" { default = "tf-example" } resource "alicloud_cr_ee_instance" "default" { payment_type = "Subscription" period = 1 renew_period = 1 renewal_status = "AutoRenewal" instance_type = "Advanced" instance_name = var.name }執行以下命令,初始化
Terraform運行環境。terraform init返回如下資訊,表示Terraform初始化成功。
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/alicloud... - Installing hashicorp/alicloud v1.234.0... - Installed hashicorp/alicloud v1.234.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. 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.建立執行計畫,並預覽變更。
terraform plan執行以下命令,建立一個Container Registry企業版執行個體。
terraform apply在執行過程中,根據提示輸入
yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示建立Container Registry企業版執行個體成功。Plan: 1 to add, 0 to change, 0 to destroy. 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.default: Creating... alicloud_cr_ee_instance.default: Still creating... [10s elapsed] alicloud_cr_ee_instance.default: Still creating... [20s elapsed] alicloud_cr_ee_instance.default: Still creating... [31s elapsed] alicloud_cr_ee_instance.default: Still creating... [41s elapsed] alicloud_cr_ee_instance.default: Still creating... [51s elapsed] alicloud_cr_ee_instance.default: Still creating... [1m1s elapsed] alicloud_cr_ee_instance.default: Still creating... [1m11s elapsed] alicloud_cr_ee_instance.default: Still creating... [1m21s elapsed] alicloud_cr_ee_instance.default: Still creating... [1m31s elapsed] alicloud_cr_ee_instance.default: Still creating... [1m41s elapsed] alicloud_cr_ee_instance.default: Still creating... [1m51s elapsed] alicloud_cr_ee_instance.default: Still creating... [2m1s elapsed] alicloud_cr_ee_instance.default: Creation complete after 2m9s [id=cri-4bsyebi*****] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.驗證結果 。
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料:
terraform show
登入Container Registry控制台
建立完成後,您可以登入Container Registry控制台,查看所建立的Container Registry執行個體。

清理資源
目前無法通過Terraform退訂預付費的Container Registry執行個體,執行terraform destroy後會將資源從狀態檔案中移除,請及時到控制台上進行退訂!
完整樣本
當前範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
如果您想體驗更多完整樣本,請前往更多完整樣本中對應產品的檔案夾查看。執行個體參數詳情請參見參數說明。