All Products
Search
Document Center

Container Registry:Use Terraform to create a Container Registry Enterprise Edition instance

Last Updated:May 08, 2025

This topic describes how to use Terraform to create a Container Registry Enterprise Edition instance.

Prerequisites

Terraform is installed and configured.

Note

You can run the sample code in this topic with a few clicks.

Prerequisites

  • An Alibaba Cloud account has full permissions on all resources that belong to this account. If the credentials of an Alibaba Cloud account are leaked, security risks may arise. We recommend that you use a Resource Access Management (RAM) user and create an AccessKey pair for the RAM user. For more information, see Create a RAM user and Create an AccessKey pair.

  • The AliyunContainerRegistryFullAccess permission policy that allows the RAM user to manage Container Registry resources is attached to the RAM user. For more information, see Grant permissions to a RAM user.

    {
        "Version": "1",
        "Statement": [
            {
                "Action": "cr:*",
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": "bss:*",
                "Resource": "*",
                "Effect": "Allow"
            }
        ]
    }
  • The runtime environment for Terraform is prepared by using one of the following methods:

    • Use Terraform in Terraform Explorer: Alibaba Cloud provides Terraform Explorer, an online runtime environment for Terraform. You can use Terraform after you log on to Terraform Explorer without the need to install Terraform. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at no additional costs.

    • Use Terraform in Cloud Shell: Terraform is preinstalled in Cloud Shell and identity credentials are configured. You can directly run Terraform commands in Cloud Shell. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at low costs.

    • Install and configure Terraform on your on-premises machine: This method is suitable for scenarios in which network connections are unstable or a custom development environment is required.

Important

You must install Terraform 0.12.28 or later. You can run the terraform --version command to query the Terraform version.

Note

You are charged for container resources in this example. Unsubscribe from the resources when you no longer need them.

Required resources

alicloud_cr_ee_instance: Creates a Container Registry instance resource.

Use Terraform to create a Container Registry Enterprise Edition instance

The following example shows how to use Terraform to create a Container Registry Enterprise Edition instance.

  1. Create a working directory and a configuration file named main.tf in the directory. main.tf is the main file of Terraform and defines the resources that you want to deploy. The following table describes the parameters in main.tf.

    Parameter

    Required

    Description

    Example

    payment_type

    No

    The billing method of the instance.

    Subscription

    period

    No

    The subscription duration of the instance. Unit: months. Valid values: 1, 2, 3, 6, 12, 24, 36, 48, and 60. If you want to subscribe to the service by year, enter an integral multiple of 12.

    Note

    If you set renewal_status to AutoRenewal, the value of this parameter must be greater than 0. Otherwise, the instance cannot be automatically renewed.

    1

    renew_period

    No

    The auto-renewal period of the instance. Unit: months. If you want to subscribe to the service by year, enter an integral multiple of 12.

    1

    renewal_status

    No

    The renewal status of the instance. Valid values:

    • AutoRenewal

    • ManualRenewal

    Default value: ManualRenewal.

    AutoRenewal

    instance_type

    Yes

    The sub-edition of the instance. Valid values:

    • Basic: Basic Edition

    • Advanced: Advanced Edition

    Advanced

    instance_name

    Yes

    The name of the instance.

    In the following example, the var.name variable is used to indicate the 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                   
    }
  2. Run the following command to initialize Terraform:

    terraform init

    If the following information is returned, Terraform is initialized.

    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.
  3. Create an execution plan and preview the changes.

    terraform plan
  4. Run the following command to create a Container Registry Enterprise Edition instance:

    terraform apply

    During the execution, enter yes as prompted and press the Enter key. Wait until the command is executed. If the following information is returned, the instance is created.

    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.
  5. Verify the instance is created.

    Run the terraform show command

    Run the following command to query the resources that are created by Terraform:

    terraform show

    image

    Log on to the Container Registry console

    You can log on to the Container Registry console to view all Container Registry instances. image

Resource cleanup

Important

You cannot use Terraform to unsubscribe from Container Registry instances that use the subscription billing method. After you run the terraform destroy command, the resources are removed from the status file. Then, log on to the Container Registry console to unsubscribe from Container Registry instances in time.

Sample code

Note

You can run the sample code with a few clicks.

If you want to view more sample code, visit the directory of the corresponding service on the Quickstarts page. For more information about instance parameters, see Parameter description.

Sample code

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                   
}