All Products
Search
Document Center

Resource Orchestration Service:Create a Terraform template

Last Updated:Jun 02, 2026

When you need to provision resources across Alibaba Cloud, Amazon Web Services (AWS), and Microsoft Azure from a single workflow, use Resource Orchestration Service (ROS) with Terraform templates. Define all resources and their dependency relationships in one template and manage your multi-cloud infrastructure without switching between providers.

Scenario

If you want to create and manage resources of third-party cloud platforms such as AWS and Microsoft Azure on Alibaba Cloud, you can define the third-party resources and configure dependency relationships for the resources in a Terraform template. This helps you manage resources across cloud platforms in a convenient manner.

Terraform template structure

Every Terraform workspace has configuration files in its root directory — Terraform treats this as the root module. You can organize related resources into child modules stored in subdirectories (for example, modules/vpc/). The root module calls child modules using module blocks, and child modules expose values back to the root module through output blocks.

For more information about the structure of Terraform templates, see Structure of Terraform templates.

Create a Terraform template

The following procedure creates a Terraform template in ROS. The example provisions a vSwitch in a virtual private cloud (VPC), using a child module to separate the VPC definition from the root configuration.

  1. Log on to the ROS console.

  2. In the left-side navigation pane, choose Templates > My Templates.

  3. On the My Templates page, click Create Template.

  4. Set the Template Type parameter to Terraform Template.

  5. Create the Terraform template files.

    The example below creates a vSwitch in a VPC. The child module (modules/vpc/main.tf) defines the VPC, and the root main.tf calls that module and creates the vSwitch using the VPC ID passed through an output.

    The final directory structure:

    .
    ├── main.tf           (root module — creates the vSwitch)
    └── modules/
        └── vpc/
            └── main.tf   (child module — creates the VPC)
    1. Create modules/vpc/main.tf to define the VPC.

      1. In the upper-right corner of the Directory section, click the + icon and select Create Folder.

      2. In the Create Folder dialog box, enter modules and click OK. A folder named modules is created in the Directory section.

      3. Hover over the modules folder, click the + icon on the right, and select Create Folder.

      4. In the Create Folder dialog box, enter vpc and click OK. A folder named vpc is created inside modules.

      5. Hover over the vpc folder, click the + icon on the right, and select Create File.

      6. In the Create File dialog box, enter main.tf and click OK. A file named main.tf is created in the vpc folder.

      7. Click main.tf and enter the following code in the code editor:

        modules/vpc/main.tf

        # Create a VPC
        resource "alicloud_vpc" "vpc" {
          name       = "tf_test"
          cidr_block = "172.16.0.0/12"
        }
        
        # Expose the VPC ID so the root module can reference it
        output "vpc_id" {
          value = "${alicloud_vpc.vpc.id}"
        }

        The resource block creates the VPC. The output block passes the VPC ID to any module that calls this one.

    2. Edit the root main.tf to call the VPC module and create the vSwitch.

      1. Click the main.tf file in the root directory.

      2. Enter the following code in the code editor:

        main.tf

        # Call the VPC child module
        module "my_vpc" {
          source = "./modules/vpc"
        }
        
        # Create a vSwitch in the VPC using the output from the child module
        resource "alicloud_vswitch" "vsw" {
          vpc_id            = "${module.my_vpc.vpc_id}"
          cidr_block        = "172.16.0.0/21"
          availability_zone = "cn-shanghai-b"
        }
        
        # Expose the vSwitch ID for use by other resources
        output "vsw_id" {
          value = "${alicloud_vswitch.vsw.id}"
        }

        The module block calls the VPC child module and makes its outputs available as module.my_vpc.*. The resource block for the vSwitch references module.my_vpc.vpc_id to use the VPC created by the child module — this is how dependency relationships are expressed between modules.

  6. In the lower-left corner of the Create Template page, choose Save Template > Save as My Template.

  7. In the Save as My Template dialog box, configure the Template Name, Template Description, Resource Group, and Tag parameters.

  8. Click OK.

References