This topic describes how to use Terraform to create and execute a CloudOps Orchestration Service (OOS) template.
You can run the sample code in this topic with a few clicks.
Prerequisites
To reduce information security risks, we recommend that you grant permissions to a RAM user based on the principle of least privilege and use the RAM user to perform the operations in this topic. For more information, see Create a RAM user and Grant permissions to a RAM user. Sample policy:
{ "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": "*" } ] }The runtime environment for Terraform is prepared by using one of the following methods:
Use Terraform in Terraform Explorer: Alibaba Cloud provides an online runtime environment for Terraform. You can log on to the Terraform Explorer environment to use Terraform without the need to install Terraform. This method is suitable for scenarios where you need to use and debug Terraform in a zero-cost, efficient, and convenient manner.
Use Terraform in Cloud Shell: Cloud Shell is preinstalled with Terraform and configured with your identity credentials. You can run Terraform commands in Cloud Shell. This method is suitable for scenarios where you need to use and access Terraform in a low-cost, efficient, and convenient manner.
Install and configure Terraform on your on-premises machine: This method is suitable for scenarios where network connections are unstable or a custom development environment is required.
Required resources
alicloud_oos_template: creates an OOS template.
alicloud_oos_execution: executes an OOS template.
Step 1: Create an OOS template
Create a working directory and a file named main.tf under the directory. Then, copy the following content to the main.tf file.
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}" }Run the following command to initialize the Terraform runtime environment:
terraform initIf the following information is returned, Terraform is initialized:
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 following command to create an OOS template:
terraform applyDuring the execution, enter
yesand press Enter. Wait until the command execution is completed. If the following information is returned, the OOS template is created: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.Verify the result.
NoteYou can update the resources created by using Terraform in the OOS console. You can also modify the configuration file of the resources and run terraform apply command to update the resources. For more information about the terraform apply command, see Common commands.
Run the terraform show command
In the working directory, run the following command to query the details of resources created by using Terraform:
terraform show
Log on to the OOS console
Log on to the OOS console. In the left-side pane, choose . On the Custom Template page, select a region in the top navigation bar. In this example, China (Beijing) is selected. Then, view the OOS template that you created in the region.

Step 2: Update the OOS template
This step instructs you to update an OOS template by using the description of the OOS template that you created in the preceding step.
Modify the template description of the alicloud_oos_template resource in the
main.tffile. Change "Description": "Update Describe instances of given status" to "Description": "Update Describe instances of given status,test update oos". Sample code: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}" }Create an execution plan and preview the changes.
terraform planRun the following command to execute the plan:
terraform applyDuring the execution, enter
yesas prompted and press the Enter key. Wait until the command is executed. If the following information appears, the code execution is successful:Apply complete! Resources: 0 added, 1 changed, 0 destroyed.Verify the result.
Run the terraform show command
In the working directory, you can run the following command to query the details of resources managed by Terraform:
terraform show
Log on to the OOS console
Log on to the OOS console. In the left-side pane, choose . On the Custom Template page, select a region in the top navigation bar. In this example, China (Beijing) is selected. Then, click Details in the Actions column of the OOS template that you created in the region to view the template details.
Step 3: Execute the OOS template
Add the following code to the
main.tffile.resource "alicloud_oos_execution" "exampleExecution" { template_name = alicloud_oos_template.example.template_name description = "From TF Test" parameters = <<EOF {"Status":"Running"} EOF }Create an execution plan and preview the changes.
terraform planRun the following command to execute the OOS template:
terraform applyDuring the execution, enter
yesas prompted and press the Enter key. Wait until the command is executed. If the following information appears, the OOS template is successfully executed:Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Verify the result.
Run the terraform show command
In the working directory, you can run the following command to query the details of resources created by Terraform:
terraform show
Log on to the OOS console
Log on to the OOS console. In the left-side pane, choose . On the Task Execution Management page, select a region in the top navigation bar. In this example, China (Beijing) is selected. Then, view the detailed information about the task execution.
Resource cleanup
If you no longer require the preceding resources that are created or managed by using Terraform, run the following command to release the resources. For more information about the terraform destroy command, see Common commands.
terraform destroySample code
You can run the sample code in this section with a few clicks.
Sample code
To view the sample code of a product, go to the related product folder on the sample code page.