All Products
Search
Document Center

Cloud Shell:Gunakan Terraform untuk mengelola resource Alibaba Cloud

Last Updated:Aug 28, 2026

Cloud Shell telah dilengkapi Terraform yang terpasang sebelumnya. Terraform adalah alat open source yang memungkinkan Anda menyediakan dan mengelola infrastruktur Alibaba Cloud secara aman dan efisien.

Mulai Cloud Shell

Untuk memulai Cloud Shell, gunakan salah satu metode berikut:
Saat memulai Cloud Shell, perhatikan hal-hal berikut:
  • Saat pertama kali menghubungkan ke Cloud Shell, sebuah virtual machine (VM) akan dibuat. Proses pembuatan memakan waktu paling lama 30 detik.

  • Jika Anda membuka beberapa jendela Cloud Shell, semua jendela tersebut terhubung ke VM yang sama. Jumlah VM tidak bertambah saat Anda membuka jendela Cloud Shell baru.

Kelola resource Alibaba Cloud

  1. Buat templat Terraform di Cloud Shell.

    Gunakan Vim untuk membuat dan mengedit templat Terraform.

    Jalankan perintah berikut untuk membuat direktori proyek dan file templat:

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

    Templat Terraform contoh berikut membuat instance Elastic Compute Service (ECS). Salin kode tersebut ke file main.tf. Cloud Shell secara otomatis mendapatkan kredensial identitas akun yang sedang login, sehingga Anda tidak perlu mengonfigurasi variabel lingkungan.

    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. Jalankan perintah init untuk menginisialisasi Terraform.

    terraform init

    Output yang diharapkan:

    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.
  3. Jalankan perintah plan untuk melihat pratinjau hasil eksekusi templat Terraform.

    terraform plan
  4. Jalankan perintah apply untuk membuat instance ECS. Selama eksekusi, ketik yes saat diminta, lalu tekan Enter. Tunggu hingga perintah selesai. Jika output berikut ditampilkan, berarti instance ECS telah berhasil dibuat.

    terraform apply

    Output yang diharapkan:

    Apply complete! Resources: 5 added, 0 changed, 0 destroyed.

Verifikasi resource

Jalankan perintah berikut untuk menampilkan detail resource yang dibuat oleh Terraform:

terraform show

Output yang diharapkan:

# alicloud_instance.instance:
resource "alicloud_instance" "instance" {
    availability_zone               = "cn-shanghai-a"
    cpu                             = 2
    create_time                     = "2024-11-13T08:52Z"
    deletion_protection             = false
    dry_run                         = false
    enable_jumbo_frame              = false
    expired_time                    = "2099-12-31T15:59Z"
    host_name                       = "iZuxxx"
    http_put_response_hop_limit     = 0
    id                              = "i-ufxxx"
    image_id                        = "ubuntu_18_04_64_20G_alibase_xxx vhd"
    instance_charge_type            = "PostPaid"
    instance_name                   = "test_fofo"
    instance_type                   = "ecs.n4.large"
    internet_charge_type            = "PayByTraffic"
    internet_max_bandwidth_in       = 500
    internet_max_bandwidth_out      = 10
    ipv6_address_count              = 0
    ipv6_addresses                  = []
    maintenance_action              = "AutoRecover"
    maintenance_notify              = false
    memory                          = 4096
    network_interface_id            = "eni-uf6ixxx"
    network_interface_traffic_mode  = "Standard"
    os_name                         = "Ubuntu  18.04 64位"
    os_type                         = "linux"
    primary_ip_address              = "xxx"
    private_ip                      = "xxx"
    public_ip                       = "xxx"
    queue_pair_number               = 0

Login ke Konsol ECS dan verifikasi instance yang telah dibuat. Di panel navigasi kiri, klik Instances untuk membuka halaman Instances. Pastikan ID instans dan status Berjalan dari instance yang dituju.

Ganti versi Terraform

Versi Terraform default di Cloud Shell adalah 0.12.31. Untuk menggunakan versi yang lebih baru, jalankan perintah tfenv untuk mengganti versi.

  1. Periksa versi Terraform yang telah terpasang di Cloud Shell.

tfenv list
  1. Ganti ke versi Terraform yang diinginkan.

tfenv use <terraform_version>

Referensi