All Products
Search
Document Center

Cloud Shell:Use Terraform to manage Alibaba Cloud resources

Last Updated:Aug 07, 2024

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

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.

    You can use Vim commands to compile 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 code shows a sample Terraform template used to create Elastic Compute Service (ECS) instances. Copy the code to the main.tf template file. Cloud Shell can automatically obtain the identity authentication information of a logon account. You do not need to set 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. If you require a later version, you can run the tfenv command to switch the version.

  1. Check the version of Terraform preinstalled in Cloud Shell.

tfenv list
  1. Switch to the required Terraform version.

tfenv use <terraform_version>

References