Terraform is an open source tool that allows you to preconfigure and manage your cloud infrastructure in a secure and efficient manner. Cloud Shell is pre-installed with Terraform. In Cloud Shell, you can use Terraform to manage your Alibaba Cloud resources.

Start Cloud Shell

Select a method to start Cloud Shell.
Note the following when starting Cloud Shell:
  • A Linux VM will be created when the first time you connect to Cloud Shell. It usually takes 40 seconds.

  • The same VM is connected although multiple Cloud Shell windows are opened. The number of VMs does not increase when a new Cloud Shell window is opened.

Manage Alibaba Cloud resources

  1. Compile a Terraform template in Cloud Shell

    You can use Vim commands to compile a Terraform template. If you have activated Object Storage Service (OSS), you can upload the template that you compile to a bucket created for Cloud Shell.

    Run the following commands to create a project directory and a template file:
    mkdir terraform-project
    cd terraform-project 
    touch main.tf
    The following code shows a sample Terraform template used to create Elastic Compute Service (ECS) instances. Copy the code to the main.tf template file.
    provider "alicloud" {}
    
    resource "alicloud_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"
      availability_zone = "cn-beijing-b"
    }
    
    
    resource "alicloud_security_group" "default" {
      name = "default"
      vpc_id = "${alicloud_vpc.vpc.id}"
    }
    
    
    resource "alicloud_instance" "instance" {
      # cn-beijing
      availability_zone = "cn-beijing-b"
      security_groups = ["${alicloud_security_group.default. *.id}"]
    
      # series III
      instance_type        = "ecs.n2.small"
      system_disk_category = "cloud_efficiency"
      image_id             = "ubuntu_140405_64_40G_cloudinit_20161115.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 terraform init command to initialize the working directory that contains the Terraform template file.
    terraform init
  3. Run the terraform plan command to preview the execution result of the Terraform template.
    terraform plan
  4. Run the terraform apply command to create an ECS instance.
    terraform apply

References