在Cloud Shell中使用Terraform

阿里云Cloud Shell是一款帮助您运维的免费产品,预装了Terraform的组件,并配置好身份凭证(credentials)。因此您可直接在Cloud Shell中运行Terraform的命令。

操作步骤

  1. 打开浏览器,访问Cloud Shell

    说明

    更多Cloud Shell入口及使用请参见使用云命令行

    Cloud Shell

  2. 创建执行目录并进入。

    说明

    需要为每个Terraform项目创建一个独立的执行目录。

    mkdir /usr/local/terraform
    cd /usr/local/terraform
  3. 使用vim编写Terraform模板(terraform.tf)。

    以查询可用区信息为例:

    vim terraform.tf

    模板内容如下:

    data "alicloud_db_zones" "queryzones" {
      instance_charge_type= "PostPaid"
      engine = "PostgreSQL"
      db_instance_storage_type = "cloud_essd"
    }
    说明

    vim命令配置完成后,需按Esc后输入:wq保存退出。

  4. 执行terraform init命令初始化配置。

    执行结果示例:

    shell@Alicloud:/usr/local/terraform$ terraform init
    
    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.186.0...
    
    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.186"
    
    
    Warning: registry.terraform.io: For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers.
    
    
    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.
  5. 执行terraform plan命令预览配置。

    执行结果示例:

    shell@Alicloud:/usr/local/terraform$ 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.
    
    data.alicloud_db_zones.queryzones: Refreshing state...
    
    ------------------------------------------------------------------------
    
    No changes. Infrastructure is up-to-date.
    
    This means that Terraform did not detect any differences between your
    configuration and real physical resources that exist. As a result, no
    actions need to be performed.
  6. 执行terraform apply应用配置。

    执行结果示例:

    shell@Alicloud:/usr/local/terraform$ terraform apply
    data.alicloud_db_zones.queryzones: Refreshing state...
    
    Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
  7. 执行terraform show查看查询结果。

    执行结果示例:

    shell@Alicloud:/usr/local/terraform$ terraform show
    # data.alicloud_db_zones.queryzones:
    data "alicloud_db_zones" "queryzones" {
        db_instance_storage_type = "cloud_essd"
        engine                   = "PostgreSQL"
        id                       = "491248936"
        ids                      = [
            "cn-hangzhou-g",
            "cn-hangzhou-j",
            "cn-hangzhou-k",
        ]
        instance_charge_type     = "PostPaid"
        multi                    = false
        multi_zone               = false
        zones                    = [
            {
                id             = "cn-hangzhou-g"
                multi_zone_ids = []
            },
            {
                id             = "cn-hangzhou-j"
                multi_zone_ids = []
            },
            {
                id             = "cn-hangzhou-i"
                multi_zone_ids = []
            },
            {
                id             = "cn-hangzhou-j"
                multi_zone_ids = []
            },
            {
                id             = "cn-hangzhou-k"
                multi_zone_ids = []
            },
        ]
    }