Alibaba CloudのCloud Shellは、Terraformに付属する無料のO&Mサービスで、認証資格情報が設定されています。 したがって、Cloud ShellでTerraformコマンドを実行できます。
手順
ブラウザを使用してCloud Shellにアクセスします。
説明Cloud Shellの使用方法の詳細については、「Cloud Shellの使用」をご参照ください。
実行ディレクトリを作成し、ディレクトリにアクセスします。
説明Terraformプロジェクトごとに実行ディレクトリを作成する必要があります。
mkdir /usr/local/terraform cd /usr/local/terraformvimコマンドを実行して、Terraform. tfという名前のterraformテンプレートファイルを設定します。例:
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-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 = [] }, ] }