Cloud Shell in Alibaba Cloud is a free O&M service that comes with Terraform and is configured with authentication credentials. Therefore, you can run Terraform commands in Cloud Shell.
Procedure
Access Cloud Shell by using a browser.
NoteFor more information about how to use Cloud Shell, see Use Cloud Shell.
Create an execution directory and access the directory.
NoteYou must create an execution directory for each Terraform project.
mkdir /usr/local/terraform cd /usr/local/terraform
Run the
vim
command to configure the Terraform template file named terraform.tf.Example:
vim terraform.tf
For this example, add the following information to the file:
data "alicloud_db_zones" "queryzones" { instance_charge_type= "PostPaid" engine = "PostgreSQL" db_instance_storage_type = "cloud_essd" }
NoteAfter you configure the file by using the
vim
command, press Esc and enter:wq
to save the configuration and exit.Run the
terraform init
command to initialize the configuration.Sample output:
shell@Alicloud:/usr/local/terraform$ terraform init Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.186.0... The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.alicloud: version = "~> 1.186" Warning: registry.terraform.io: For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers. 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
terraform plan
command to preview the configuration.Sample output:
shell@Alicloud:/usr/local/terraform$ terraform plan Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. data.alicloud_db_zones.queryzones: Refreshing state... ------------------------------------------------------------------------ No changes. Infrastructure is up-to-date. This means that Terraform did not detect any differences between your configuration and real physical resources that exist. As a result, no actions need to be performed.
Run the
terraform apply
command to apply the configuration.Sample output:
shell@Alicloud:/usr/local/terraform$ terraform apply data.alicloud_db_zones.queryzones: Refreshing state... Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Run the
terraform show
command to query the result.Sample output:
shell@Alicloud:/usr/local/terraform$ terraform show # data.alicloud_db_zones.queryzones: data "alicloud_db_zones" "queryzones" { db_instance_storage_type = "cloud_essd" engine = "PostgreSQL" id = "491248936" ids = [ "cn-hangzhou-g", "cn-hangzhou-h", "cn-hangzhou-i", "cn-hangzhou-j", "cn-hangzhou-k", ] instance_charge_type = "PostPaid" multi = false multi_zone = false zones = [ { id = "cn-hangzhou-g" multi_zone_ids = [] }, { id = "cn-hangzhou-h" multi_zone_ids = [] }, { id = "cn-hangzhou-i" multi_zone_ids = [] }, { id = "cn-hangzhou-j" multi_zone_ids = [] }, { id = "cn-hangzhou-k" multi_zone_ids = [] }, ] }