このトピックでは、Terraform を使用して CloudOps Orchestration Service (OOS) テンプレートを作成および実行する方法について説明します。
前提条件
情報セキュリティリスクを軽減するために、最小権限の原則に基づいて RAM ユーザーに権限を付与し、その RAM ユーザーを使用してこのトピックの操作を実行することをお勧めします。 詳細については、「RAM ユーザーを作成する」および「RAM ユーザーに権限を付与する」をご参照ください。サンプルポリシー:
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "oos:CreateTemplate", "oos:GetTemplate", "oos:DeleteTemplate", "oos:StartExecution", "oos:ListExecutions", "oos:DeleteExecutions", "oos:ListTemplates" ], "Resource": "*" }, { "Effect": "Allow", "Action": "ecs:DescribeInstances", "Resource": "*" } ] }Terraform のランタイム環境は、次のいずれかの方法を使用して準備します。
Terraform Explorer で Terraform を使用する: Alibaba Cloud は Terraform 用のオンラインランタイム環境を提供しています。 Terraform Explorer 環境にログインして、Terraform をインストールせずに使用できます。 この方法は、費用をかけずに効率的かつ便利な方法で Terraform を使用およびデバッグする必要があるシナリオに適しています。
Cloud Shell で Terraform を使用する: Cloud Shell には Terraform がプリインストールされており、ID 資格情報が構成されています。 Cloud Shell で Terraform コマンドを実行できます。 この方法は、低コスト、効率的、かつ便利な方法で Terraform を使用およびアクセスする必要があるシナリオに適しています。
オンプレミス マシンに Terraform をインストールして構成する: この方法は、ネットワーク接続が不安定な場合や、カスタム開発環境が必要な場合に適しています。
必要なリソース
alicloud_oos_template: OOS テンプレートを作成します。
alicloud_oos_execution: OOS テンプレートを実行します。
ステップ 1: OOS テンプレートを作成する
作業ディレクトリと、そのディレクトリの下に main.tf という名前のファイルを作成します。 次に、以下の内容を main.tf ファイルにコピーします。
variable "region" { default = "cn-beijing" } provider "alicloud" { region = var.region } resource "random_integer" "default" { min = 10000 max = 99999 } resource "alicloud_oos_template" "example" { tags = { "Created" = "TF", "For" = "template Test" } content = <<EOF { "FormatVersion": "OOS-2019-06-01", "Description": "Update Describe instances of given status", "Parameters":{ "Status":{ "Type": "String", "Description": "(Required) The status of the Ecs instance." } }, "Tasks": [ { "Properties" :{ "Parameters":{ "Status": "{{ Status }}" }, "API": "DescribeInstances", "Service": "Ecs" }, "Name": "foo", "Action": "ACS::ExecuteApi" }] } EOF template_name = "terraform-test-${random_integer.default.result}" }次のコマンドを実行して、Terraform ランタイム環境を初期化します。
terraform init次の情報が返された場合、Terraform は初期化されています。
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.次のコマンドを実行して、OOS テンプレートを作成します。
terraform apply実行中に、
yesと入力し、[enter] キーを押します。コマンドの実行が完了するまで待ちます。 次の情報が返された場合、OOS テンプレートは作成されています。You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes Apply complete! Resources: 2 added, 0 changed, 0 destroyed.結果を確認します。
terraform show コマンドを実行する
作業ディレクトリで、次のコマンドを実行して、Terraform を使用して作成されたリソースの詳細をクエリします。
terraform show
OOS コンソールにログインする
OOS コンソールにログインします。 左側のペインで、 を選択します。 [カスタムテンプレート] ページで、上部のナビゲーションバーでリージョンを選択します。 この例では、[中国 (北京)] が選択されています。 次に、リージョンで作成した OOS テンプレートを表示します。

ステップ 2: OOS テンプレートを更新する
このステップでは、前のステップで作成した OOS テンプレートの説明を使用して、OOS テンプレートを更新する方法について説明します。
main.tfファイルの alicloud_oos_template リソースのテンプレート記述を修正します。 "Description": "Update Describe instances of given status" を "Description": "Update Describe instances of given status,test update oos" に変更します。サンプルコード:resource "alicloud_oos_template" "example" { tags = { "Created" = "TF", "For" = "template Test" } content = <<EOF { "FormatVersion": "OOS-2019-06-01", "Description": "Update Describe instances of given status,test update oos", "Parameters":{ "Status":{ "Type": "String", "Description": "(Required) The status of the Ecs instance." } }, "Tasks": [ { "Properties" :{ "Parameters":{ "Status": "{{ Status }}" }, "API": "DescribeInstances", "Service": "Ecs" }, "Name": "foo", "Action": "ACS::ExecuteApi" }] } EOF template_name = "terraform-test-${random_integer.default.result}" }実行計画を作成し、変更をプレビューします。
terraform plan次のコマンドを実行して、計画を実行します。
terraform apply実行中に、プロンプトが表示されたら
yesと入力し、[enter] キーを押します。コマンドが実行されるまで待ちます。 次の情報が表示された場合、コードの実行は成功です。Apply complete! Resources: 0 added, 1 changed, 0 destroyed.結果を確認します。
terraform show コマンドを実行する
作業ディレクトリで、次のコマンドを実行して、Terraform によって管理されているリソースの詳細をクエリできます。
terraform show
OOS コンソールにログインする
OOS コンソールにログインします。 左側のペインで、 を選択します。 [カスタムテンプレート] ページで、上部のナビゲーションバーでリージョンを選択します。 この例では、[中国 (北京)] が選択されています。 次に、リージョンで作成した OOS テンプレートの [操作] 列の [詳細] をクリックして、テンプレートの詳細を表示します。

ステップ 3: OOS テンプレートを実行する
次のコードを
main.tfファイルに追加します。resource "alicloud_oos_execution" "exampleExecution" { template_name = alicloud_oos_template.example.template_name description = "From TF Test" parameters = <<EOF {"Status":"Running"} EOF }実行計画を作成し、変更をプレビューします。
terraform plan次のコマンドを実行して、OOS テンプレートを実行します。
terraform apply実行中に、プロンプトが表示されたら
yesと入力し、[enter] キーを押します。コマンドが実行されるまで待ちます。 次の情報が表示された場合、OOS テンプレートは正常に実行されています。Apply complete! Resources: 1 added, 0 changed, 0 destroyed.結果を確認します。
terraform show コマンドを実行する
作業ディレクトリで、次のコマンドを実行して、Terraform によって作成されたリソースの詳細をクエリできます。
terraform show
OOS コンソールにログインする
OOS コンソールにログインします。 左側のペインで、 を選択します。 [タスク実行管理] ページで、上部のナビゲーションバーでリージョンを選択します。 この例では、[中国 (北京)] が選択されています。 次に、タスク実行の詳細情報を表示します。

リソースクリーンアップ
Terraform を使用して作成または管理された上記のリソースが不要になった場合は、次のコマンドを実行してリソースを解放します。 terraform destroy コマンドの詳細については、「共通コマンド」をご参照ください。
terraform destroyサンプルコード
サンプルコード
製品のサンプルコードを表示するには、サンプルコード ページの関連製品フォルダに移動します。