Terraform集成示例

本文介绍如何使用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.