All Products
Search
Document Center

:Use Terraform to manage OOS

Last Updated:Dec 27, 2023

Terraform is an open source tool that provides a secure and efficient method to preview, configure, and manage cloud infrastructure and resources. Terraform also allows you to build, change, and manage infrastructure with efficiency. You can use the tool to manage existing and mainstream service providers and custom internal solutions. You can also use this tool to manage some Alibaba Cloud API operations.

CloudOps Orchestration Service (OOS) allows you to use templates to manage API operations. You can use OOS to manage Alibaba Cloud resources that you purchased and perform operations with ease. For example, you can use OOS to automatically scale out resources. Before you proceed, make sure that you are familiar with the basic operations of OOS. This topic describes a tutorial that shows how to use Terraform to manage OOS.

Install and configure Terraform

  1. Download the software package that is suitable for your operating system from the official Terraform website. In this example, a Linux operating system is used. If you do not have a Linux operating system, purchase an Alibaba Cloud Elastic Compute Service (ECS) instance. For more information, see Quick start for Linux instances.

  2. Decompress the package to the /usr/local/bin directory. If you want to decompress the package to another directory, define a global path for the package by using one of the following methods:

3. Run the terraform command to verify the path.

terraform
Usage: terraform [-version] [-help] <command> [args]

4. Run the following command to install Go. Before you install other modules, you must install Go.

yum install golang

5. Run the following command to install terraform-provider-alicloud. For more information about the instructions, see Terraform Provider For Alibaba Cloud.

yum install git
go get golang.org/x/tools/cmd/goimports
mkdir -p $GOPATH/src/github.com/aliyun
cd $GOPATH/src/github.com/aliyun
git clone https://github.com/aliyun/terraform-provider-alicloud
cd $GOPATH/src/github.com/aliyun/terraform-provider-alicloud
make build
Important

Configure the following environment variables.

export ALICLOUD_ACCESS_KEY=xxx

export ALICLOUD_SECRET_KEY=xxx

export ALICLOUD_REGION=xxx

OOS resource modules that are supported by Terraform

Resource type

Module

Scenarios

date

alicloud_oos_templates

Query templates.

alicloud_oos_executions

Query executions.

resource

alicloud_oos_template

Create or update templates.

alicloud_oos_execution

Create executions.

Use Terraform to create, update, query, or delete OOS templates

1. Create a template

1.1 Run the following commands to create the testTerraform directory, go to the directory, initialize Terraform, create the createTemplate.tf file, and then enter the edit mode of this file:

mkdir testTerraform
cd testTerraform
terraform init
vi createTemplate.tf

1.2 In edit mode, copy the following code to the createTemplate.tf file:

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

1.3 Save and exit the file.

1.4 Run the following commands to use Terraform to create an OOS template:

terraform plan
terraform apply

1.5 Log on to the OOS console and check whether the template is created. After the preceding steps are performed and custom parameters are configured, the template is created, as shown in the following figure.

image

2. Update a template

2.1 After the template is created, find the createTemplate.tf file and open this file.

vi createTemplate.tf

2.2 Enter the edit mode and replace the file content with the following 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",
        "Parameters":{},
        "Tasks": [
            {
                "Properties" :{
                    "Parameters":{
                        "Status": "Running"
                    },
                    "API": "DescribeInstances",
                    "Service": "Ecs"
                },
                "Name": "foo",
                "Action": "ACS::ExecuteApi"
            }]
        }
        EOF
        template_name="terraform-test"
}

2.3 Save and exit the file.

2.4 Run the following commands to update the template.

terraform plan
terraform apply

3. Query a template

3.1 Perform the operations that are required to query templates. For more information, see alicloud_oos_templates.

3.2 Create the dataTest.tf file and enter the edit mode of this file.

vi dataTest.tf

3.3 Copy the following code to the dataTest.tf file. Then, save and exit the file. The following code is used to view resources.

data "alicloud_oos_templates" "example" {
  ids=["terraform-test"]
}

output "first_template" {
  value = "${data.alicloud_oos_templates.example.templates.0}"
}

3.4 After the preceding operations are performed, run the following commands to query the details of the specified template:

terraform plan
terraform apply

3.5 View the details of the template, as shown in the following figure.

image

4. Delete a template

4.1 After a template is created, run the following commands to delete the template if required:

terraform show
terraform destroy

Use Terraform to manage OOS executions

1. Create an execution

1.1 Create the testExecution.tf file and enter the edit mode of this file. This file is used to create an execution based on the preceding template.

vi createTemplate.tf

1.2 In the edit mode, copy the following code to the testExecution.tf file:

resource "alicloud_oos_execution" "exampleExecution"{
        template_name = "terraform-test"
        description = "From TF Test"
        parameters = <<EOF
          {"Status":"Running"}
        EOF
}

1.3 Save and exit the file.

1.4 Run the following commands to use Terraform to execute the OOS template:

terraform plan
terraform apply

1.5 Log on to the OOS console and check whether the execution is created. The result indicates that the execution is created, as shown in the following figure.

2. View an execution

2.1 Perform the operations that are required to query executions. For more information, see alicloud_oos_executions.

2.2 Create the dataExecutionTest.tf file and enter the edit mode of this file.

vi dataTest.tf

2.3 Copy the following code to the dataExecutionTest.tf file. Then, save and exit the file.

data "alicloud_oos_executions" "example" {
  ids = ["execution_id"]
  template_name = "name"
}

output "first_execution_id" {
  value = "${data.alicloud_oos_executions.example.executions.0.id}"
}

2.4 After the preceding operations are performed, run the following commands to query the details of the specified execution:

terraform plan
terraform apply

2.5 View the details of the execution, as shown in the following figure.

image

3. Delete an execution

3.1 After you use Terraform to create an execution, run the following commands to delete the execution from OOS if required:

terraform show
terraform destroy