Use Terraform to manage Alibaba Cloud resources
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
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 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
-
Compile a Terraform template in Cloud Shell.
Use
Vimto 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.tfThe following sample Terraform template creates Elastic Compute Service (ECS) instances. Copy the code to the
main.tffile. 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" } -
Run the
initcommand to initialize Terraform.terraform initExpected output:
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. -
Run the
plancommand to preview the execution result of the Terraform template.terraform plan -
Run the
applycommand to create an ECS instance. During the execution, typeyesas prompted and press Enter. Wait for the command to complete. If the following output is displayed, the ECS instance is created.terraform applyExpected output:
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
Verify the resources
Run the following command to query the details of the resources created by Terraform:
terraform show
Expected output:
# 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
Log in to the ECS console and verify the created instance. In the left navigation pane, click Instances to go to the Instances page. Confirm the instance ID and running status of the target instance.
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.
-
Check the Terraform version preinstalled in Cloud Shell.
tfenv list
-
Switch to the desired Terraform version.
tfenv use <terraform_version>