阿里云Cloud Shell是一款幫助您運維的免費產品,預裝了Terraform的組件,并配置好身份憑證(credentials)。因此您可直接在Cloud Shell中運行Terraform的命令。
操作步驟
打開瀏覽器,訪問Cloud Shell。
說明更多Cloud Shell入口及使用請參見使用云命令行。
創建執行目錄并進入。
說明需要為每個Terraform項目創建一個獨立的執行目錄。
mkdir /usr/local/terraform cd /usr/local/terraform
使用
vim
編寫Terraform模板(terraform.tf)。以查詢可用區信息為例:
vim terraform.tf
模板內容如下:
data "alicloud_db_zones" "queryzones" { instance_charge_type= "PostPaid" engine = "PostgreSQL" db_instance_storage_type = "cloud_essd" }
說明vim
命令配置完成后,需按Esc后輸入:wq
保存退出。執行
terraform init
命令初始化配置。執行結果示例:
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.
執行
terraform plan
命令預覽配置。執行結果示例:
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.
執行
terraform apply
應用配置。執行結果示例:
shell@Alicloud:/usr/local/terraform$ terraform apply data.alicloud_db_zones.queryzones: Refreshing state... Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
執行
terraform show
查看查詢結果。執行結果示例:
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-j", "cn-hangzhou-k", ] instance_charge_type = "PostPaid" multi = false multi_zone = false zones = [ { id = "cn-hangzhou-g" multi_zone_ids = [] }, { id = "cn-hangzhou-j" multi_zone_ids = [] }, { id = "cn-hangzhou-i" multi_zone_ids = [] }, { id = "cn-hangzhou-j" multi_zone_ids = [] }, { id = "cn-hangzhou-k" multi_zone_ids = [] }, ] }
文檔內容是否對您有幫助?