Create and execute CloudOps Orchestration Service (OOS) templates using Terraform.
The sample code in this topic can be run with a single click. Run with one click
Prerequisites
-
For enhanced security, use a RAM user with minimum permissions. Create a RAM user and Grant permissions to a RAM user. Minimum required 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": "*" } ] } -
Set up a Terraform environment using one of the following methods:
-
Terraform Explorer: An online Terraform environment with no installation required. Ideal for trying and debugging at no cost.
-
Use Terraform to quickly create resources: Cloud Shell comes pre-installed with Terraform and configured with your credentials. Ideal for quick, low-cost access.
-
Install and configure Terraform on your local machine: Suitable for limited-connectivity environments or custom development needs.
-
Resources used
-
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 configuration file named main.tf. Add the following code to 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}" } -
Run the following command to initialize the Terraform environment.
terraform initExpected output on success:
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 the OOS template.
terraform applyWhen prompted, enter
yesand press Enter. Expected output on success: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.
NoteTo update Terraform-managed resources, modify them in the CloudOps Orchestration Service (OOS) console or update the configuration and run terraform apply. Common commands.
CLI
Run the following command to view resource details:
terraform show
Console
Log on to the CloudOps Orchestration Service (OOS) console. In the left-side navigation pane, choose . In the top-left corner, select the region. In this example, select China (Beijing) to view the created OOS template.

Step 2: Update the OOS template
Update the OOS template by modifying its description.
-
In the
main.tffile, change theDescriptionin the alicloud_oos_template resource from Update Describe instances of given status to 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}" } -
Create an execution plan to preview the changes.
terraform plan -
Run the following command to apply the changes.
terraform applyWhen prompted, enter
yesand press Enter. Expected output on success:Apply complete! Resources: 0 added, 1 changed, 0 destroyed. -
Verify the result.
CLI
Run the following command to view resource details:
terraform show
Console
Log on to the CloudOps Orchestration Service (OOS) console. In the left-side navigation pane, choose . In the top-left corner, select the region. In this example, select China (Beijing). In the Actions column of the template, click 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 to preview the changes.
terraform plan -
Run the following command to apply the changes and execute the OOS template.
terraform applyWhen prompted, enter
yesand press Enter. Expected output on success:Apply complete! Resources: 1 added, 0 changed, 0 destroyed. -
Verify the result.
CLI
Run the following command to view resource details:
terraform show
Console
Log on to the CloudOps Orchestration Service (OOS) console. In the left-side navigation pane, choose . In the top-left corner, select the region. In this example, select China (Beijing) to view the task execution details.

Resource cleanup
When you no longer need these resources, release them with the following command. Common Commands.
terraform destroy
Complete example
The sample code in this topic can be run with a single click. Run with one click
Sample code
More examples are available in the Examples repository.