All Products
Document Center

Cloud Shell:Use Terraform to manage Alibaba Cloud resources

Last Updated:May 10, 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 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. 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 

    The following code shows a sample Terraform template used to create Elastic Compute Service (ECS) instances. Copy the code to the 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 = ""
    resource "alicloud_vswitch" "vsw" {
      vpc_id     =
      cidr_block = ""
      zone_id    =
    resource "alicloud_security_group" "default" {
      name   = "default"
      vpc_id =
    resource "alicloud_instance" "instance" {
      # cn-beijing
      availability_zone =
      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                 =
      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 =
      cidr_ip           = ""
  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>