All Products
Search
Document Center

CloudOps Orchestration Service:Use Terraform to manage OOS

Last Updated:Dec 19, 2024

This topic describes how to use Terraform to create and execute a CloudOps Orchestration Service (OOS) template.

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

Step 1: Create an OOS template

  1. 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}"
    }
  2. Run the following command to initialize the Terraform runtime environment:

    terraform init

    If 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.
  3. Run the following command to create an OOS template:

    terraform apply

    During the execution, enter yes and 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.
  4. Verify the result.

    Note

    You 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

    image

    Log on to the OOS console

    Log on to the OOS console. In the left-side pane, choose Automated Task > Custom Template. 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.

    image

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.

  1. Modify the template description of the alicloud_oos_template resource in the main.tf file. 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}"
    }
  2. Create an execution plan and preview the changes.

    terraform plan
  3. Run the following command to execute the plan:

    terraform apply

    During the execution, enter yes as 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.
  4. 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

    image

    Log on to the OOS console

    Log on to the OOS console. In the left-side pane, choose Automated Task > Custom Template. 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.image

Step 3: Execute the OOS template

  1. Add the following code to the main.tf file.

    resource "alicloud_oos_execution" "exampleExecution" {
      template_name = alicloud_oos_template.example.template_name
      description   = "From TF Test"
      parameters    = <<EOF
              {"Status":"Running"}
            EOF
    }
  2. Create an execution plan and preview the changes.

    terraform plan
  3. Run the following command to execute the OOS template:

    terraform apply

    During the execution, enter yes as 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.
  4. 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

    image

    Log on to the OOS console

    Log on to the OOS console. In the left-side pane, choose Automated Task > Task Execution Management. 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.image

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 destroy

Sample code

Sample code

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}"
}

resource "alicloud_oos_execution" "exampleExecution" {
  template_name = alicloud_oos_template.example.template_name
  description   = "From TF Test"
  parameters    = <<EOF
          {"Status":"Running"}
        EOF
}

To view the sample code of a product, go to the related product folder on the sample code page.