Use Terraform to create a Container Registry Enterprise Edition instance.
Run this sample code with a single click. Run sample code
Prerequisites
-
Avoid using the Alibaba Cloud root account directly because it has full resource permissions and poses security risks if credentials leak. Instead, Create a RAM user and Create an AccessKey pair.
-
Grant the RAM user the AliyunContainerRegistryFullAccess permission to manage Container Registry. Grant permissions to a RAM user.
{ "Version": "1", "Statement": [ { "Action": "cr:*", "Resource": "*", "Effect": "Allow" }, { "Action": "bss:*", "Resource": "*", "Effect": "Allow" } ] } -
Set up a Terraform runtime environment using one of the following methods:
-
Terraform Explorer: A free online environment for running Terraform commands. Ideal for quick debugging and trials.
-
Quickly create resources with Terraform: Terraform is pre-installed in Cloud Shell with identity credentials pre-configured.
-
Install and configure Terraform on your local machine: Best for poor network conditions or custom development environments.
ImportantTerraform v0.12.28 or later is required. Run
terraform --versionto verify. -
Create an instance with Terraform
This example creates a Container Registry Enterprise Edition instance.
-
Create a working directory with a configuration file named
main.tf. Themain.tffile defines the resources to deploy. The following table describes the parameters.Parameter
Required
Description
Example
payment_type
No
Billing method.
Subscription
period
No
Subscription period in months. Valid values: 1, 2, 3, 6, 12, 24, 36, 48, and 60. For annual subscriptions, use a multiple of 12.
NoteIf you set
renewal_statustoAutoRenewal, this value must be greater than 0. Otherwise, auto-renewal will fail.1
renew_period
No
Auto-renewal period in months. For annual subscriptions, use a multiple of 12.
1
renewal_status
No
Auto-renewal status. Valid values:
-
AutoRenewal: Auto-renews the instance.
-
ManualRenewal: Requires manual renewal.
Defaults to
ManualRenewal.AutoRenewal
instance_type
Yes
Instance type. Valid values:
-
Basic: The Basic Edition.
-
Advanced: The Advanced Edition.
Advanced
instance_name
Yes
Instance name.
This example uses the
var.namevariable.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 } -
-
Initialize the
Terraformruntime environment.terraform initExpected output on success:
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. -
Preview changes with an execution plan.
terraform plan -
Create the Container Registry Enterprise Edition instance.
terraform applyWhen prompted, enter
yesand press Enter. Expected output on success: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. -
Verify the results.
Terraform show command
View the managed resources:
terraform showshell@Alicloud:~/ens/rongqi$ terraform show # alicloud_cr_ee_instance.default: resource "alicloud_cr_ee_instance" "default" { created_time = "2024-11-21T03:16:00Z" end_time = "2024-12-21T16:00:00Z" id = "xxx" instance_name = "tf-example" instance_type = "Advanced" payment_type = "Subscription" period = 1 renew_period = 1 renewal_status = "AutoRenewal" status = "RUNNING" }Verify in the console
Log in to the Container Registry console. On the Instance List page, verify that tf-example has a status of Running and a specification of Advanced Edition.
Resource cleanup
Terraform cannot release subscription-based Container Registry instances. The terraform destroy command removes the resource from the state file but does not terminate the subscription. Manually unsubscribe from the instance in the console to avoid further charges.
Complete example
Run this sample code with a single click. Run sample code
Explore product-specific examples in Quickstarts. The Parameter description table lists all supported parameters.
Example code
References
-
Terraform resource for creating a Container Registry Enterprise Edition instance: alicloud_cr_ee_instance.