This document describes how to use Terraform to create and execute CloudOps Orchestration Service (OOS) templates.
You can run the sample code in this tutorial with a single click. Run Now
Prerequisites
-
For enhanced security, use a RAM user with least-privilege permissions for this tutorial. For more information, see Create a RAM user and Grant permissions to a RAM user. The following policy contains the minimum required permissions:
{ "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": "*" } ] } -
Prepare a Terraform runtime environment. You can choose one of the following methods:
-
Terraform Explorer: Alibaba Cloud provides a free and convenient online environment for experimenting with and debugging Terraform. No installation is required. This method is ideal for quick, low-cost debugging and testing.
-
Use Terraform to quickly create resources: Cloud Shell comes with Terraform pre-installed and credentials pre-configured, so you can run Terraform commands directly. This method is ideal for quick and easy access to Terraform.
-
Install and configure Terraform on your local machine: This method is suitable for environments with poor network connectivity or for users who require a custom development setup.
-
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 in the directory. Then, copy the following code into 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 runtime environment.
terraform initThe following output indicates that Terraform was initialized successfully.
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. Wait for the command to complete. The following output indicates that the OOS template was created successfully.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 results.
NoteTo update resources created by Terraform, you can modify them in the CloudOps Orchestration Service console, or modify the configuration file and run the
terraform applycommand. For more information about theterraform applycommand, see Common commands.Terraform show
In your working directory, run the following command to view details of the resources managed by Terraform:
terraform showshell@Alicloud:~/oos$ terraform show # alicloud_oos_template.example: resource "alicloud_oos_template" "example" { auto_delete_executions = false content = jsonencode( { Description = "Update Describe instances of given status" FormatVersion = "OOS-2019-06-01" Parameters = { Status = { Description = "(Required) The status of the Ecs instance." Type = "String" } } Tasks = [ { Action = "ACS::ExecuteApi" Name = "foo" Properties = { API = "DescribeInstances" Parameters = { Status = "{{ Status }}" } Service = "Ecs" } }, ] } ) created_by = "xxx" created_date = "2024-10-30T09:41:31Z" description = "Update Describe instances of given status" has_trigger = false id = "terraform-test-xxx" resource_group_id = "xxx" share_type = "Private" tags = { "Created" = "TF" "For" = "template Test" } template_format = "JSON" template_id = "t-e24ada7a7c1940xxx" template_name = "terraform-test-xxx" template_type = "Automation" template_version = "v1" }OOS console
Log on to the CloudOps Orchestration Service console. Go to the tab. In the upper-left corner, select the region. In this example, select China (Beijing). Verify that the OOS template you created appears.
Step 2: Update the OOS template
This section shows how to update an OOS template by modifying its description.
-
In the
main.tffile, modify the template description in thealicloud_oos_templateresource. Change "Description": "Update Describe instances of given status" to "Description": "Update Describe instances of given status,test update oos" as shown in the following 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 plan -
Run the following command to apply the changes.
terraform applyWhen prompted, enter
yesand press Enter. Wait for the command to complete. The following output indicates that the change was successful.Apply complete! Resources: 0 added, 1 changed, 0 destroyed. -
Verify the results.
Terraform show
In your working directory, run the following command to view details of the resources managed by Terraform:
terraform show# alicloud_oos_template.example: resource "alicloud_oos_template" "example" { auto_delete_executions = false content = jsonencode( { Description = "Update Describe instances of given status,test update oos" FormatVersion = "OOS-2019-06-01" Parameters = { Status = { Description = "(Required) The status of the Ecs instance." Type = "String" } } Tasks = [ { Action = "ACS::ExecuteApi" Name = "foo" Properties = { API = "DescribeInstances" Parameters = { Status = "{{ Status }}" } Service = "Ecs" } }, ] } ) created_by = "xxx" created_date = "2024-11-12T02:48:37Z" description = "Update Describe instances of given status,test update oos" has_trigger = false id = "terraform-test-xxx" resource_group_id = "rg-xxx" share_type = "Private" tags = { "Created" = "TF" "For" = "template Test" } template_format = "JSON" template_id = "t-xxx" template_name = "terraform-test-xxx" template_type = "Automation" template_version = "v2" }OOS console
Log on to the CloudOps Orchestration Service console. Go to the tab. In the upper-left corner, select the region. In this example, select China (Beijing). Click Details in the Actions column of the template created in this example. On the Template Information tab, view the following basic information: - Template Name: For example,
terraform-test-xxx- Tag: For example,For: template Test,Created: TF- Resource Group - Template Description: The value isUpdate Describe instances of given status,test update oos, which confirms that the template description was updated. - Version Description and Creation Time The Process Configuration section shows the task steps, such as thefoostep of type Execute Cloud Service API. The section on the right shows the Template Parameters, such as theStatusparameter.
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 plan -
Run the following command to execute the OOS template.
terraform applyWhen prompted, enter
yesand press Enter. Wait for the command to complete. The following output indicates that the OOS template executed successfully.Apply complete! Resources: 1 added, 0 changed, 0 destroyed. -
Verify the results.
Terraform show
In your working directory, run the following command to view details of the resources managed by Terraform:
terraform showshell@Alicloud:~/oos$ terraform show # alicloud_oos_execution.exampleExecution: resource "alicloud_oos_execution" "exampleExecution" { create_date = "2024-10-30T09:47:45Z" description = "From TF Test" executed_by = "xxx" id = "exec-93fd84d8f01xxx" is_parent = false mode = "Automatic" outputs = jsonencode({}) parameters = jsonencode( { Status = "Running" } ) start_date = "2024-10-30T09:47:45Z" status = "Running" template_id = "t-e24ada7a7c194cxxx" template_name = "terraform-test-xxx" template_version = "v1" update_date = "2024-10-30T09:47:45Z" } # alicloud_oos_template.example: resource "alicloud_oos_template" "example" { auto_delete_executions = false content = jsonencode( { Description = "Update Describe instances of given status" FormatVersion = "OOS-2019-06-01" Parameters = { Status = {OOS console
Log on to the CloudOps Orchestration Service console. Go to the tab. In the upper-left corner, select the region. In this example, select China (Beijing). In the task execution list, confirm the execution created by Terraform. The execution ID is
exec-93fd84d8f01f465..., and the description isFrom TF Test. The status is Succeeded, the execution mode is Automatic, and the execution type is Manual.
Clean up resources
When you no longer need the resources, run the following command to remove them. For more information about the terraform destroy command, see Common commands.
terraform destroy
Complete example
You can run the sample code in this tutorial with a single click. Run Now
Sample code
For more examples, see the More complete examples repository and open the folder for the desired product.