Use Terraform to manage Alibaba Cloud resources

更新时间:
复制 MD 格式

Cloud Shell comes with Terraform preinstalled. Terraform is an open source tool that allows you to provision and manage Alibaba Cloud infrastructure in a secure and efficient manner.

Start Cloud Shell

To start Cloud Shell, use one of the following methods:
  • Start Cloud Shell in the Alibaba Cloud Management Console

    In the Alibaba Cloud Management Console, click the Cloud Shell icon in the top navigation bar.

  • Start Cloud Shell as a standalone application

    Enter https://shell.aliyun.com in the address bar of a browser.

    You can open up to five Cloud Shell windows at the same time.

When you start Cloud Shell, take note of the following items:
  • When you connect to Cloud Shell for the first time, a virtual machine (VM) is created. The creation process takes at most 30 seconds.

  • If you open multiple Cloud Shell windows, all the windows are connected to the same VM. The number of VMs does not increase when you open a new Cloud Shell window.

Manage Alibaba Cloud resources

  1. Compile a Terraform template in Cloud Shell.

    Use Vim to create and edit a Terraform template.

    Run the following commands to create a project directory and a template file:

    mkdir terraform-project
    cd terraform-project 
    touch main.tf

    The following sample Terraform template creates Elastic Compute Service (ECS) instances. Copy the code to the main.tf file. Cloud Shell automatically obtains the identity credentials of the logged-on account, so you do not need to configure environment variables.

    provider "alicloud" {
      region = "cn-beijing"
    }
    
    data "alicloud_zones" "default" {
      available_disk_category     = "cloud_efficiency"
      available_resource_creation = "VSwitch"
    }
    
    resource "alicloud_vpc" "vpc" {
      vpc_name   = "tf_test_foo"
      cidr_block = "172.16.0.0/12"
    }
    
    resource "alicloud_vswitch" "vsw" {
      vpc_id     = alicloud_vpc.vpc.id
      cidr_block = "172.16.0.0/21"
      zone_id    = data.alicloud_zones.default.zones.0.id
    }
    
    resource "alicloud_security_group" "default" {
      name   = "default"
      vpc_id = alicloud_vpc.vpc.id
    }
    
    resource "alicloud_instance" "instance" {
      # cn-beijing
      availability_zone = data.alicloud_zones.default.zones.0.id
      security_groups   = alicloud_security_group.default.*.id
      # series III
      instance_type              = "ecs.n4.large"
      system_disk_category       = "cloud_efficiency"
      image_id                   = "ubuntu_18_04_64_20G_alibase_20190624.vhd"
      instance_name              = "test_foo"
      vswitch_id                 = alicloud_vswitch.vsw.id
      internet_max_bandwidth_out = 10
    }
    
    resource "alicloud_security_group_rule" "allow_all_tcp" {
      type              = "ingress"
      ip_protocol       = "tcp"
      nic_type          = "intranet"
      policy            = "accept"
      port_range        = "1/65535"
      priority          = 1
      security_group_id = alicloud_security_group.default.id
      cidr_ip           = "0.0.0.0/0"
    }
  2. Run the init command to initialize Terraform.

    terraform init
  3. Run the plan command to preview the execution result of the Terraform template.

    terraform plan
  4. Run the apply command to create an ECS instance.

    terraform apply

Switch the Terraform version

The default Terraform version in Cloud Shell is 0.12.31. To use a later version, run the tfenv command to switch versions.

  1. Check the Terraform version preinstalled in Cloud Shell.

tfenv list
  1. Switch to the desired Terraform version.

tfenv use <terraform_version>

References