You can use Resource Orchestration Service (ROS) to host Terraform. You can create Terraform templates to define Alibaba Cloud, AWS, and Azure resources, specify resource parameters, and configure dependencies between resources.

Background information

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

Procedure

  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. On the Create Template page, click the Terraform tab.
  5. Create a Terraform template.

    The following section provides an example on how to create a Terraform template. In this example, a vSwitch is created within a virtual private cloud (VPC).

    1. Create a file named main.tf in the modules/vpc/ directory and edit the file content to create a VPC.
      1. Click the + icon in the upper-right corner of the Directory section 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.
      3. Move the pointer over the modules folder, click the + icon on the right side, and then select Create Folder.
      4. In the Create Folder dialog box, enter vpc and click OK. A folder named vpc is created in the modules folder.
      5. Move the pointer over the vpc folder, click the + icon on the right side, and then select Create File.
      6. In the Create File dialog box, enter main.tf and click OK. A file named main.tf is created under the vpc folder.
      7. Click main.tf and enter the following code in the right-side code editor to create a VPC:
        resource "alicloud_vpc" "vpc" {
          name       = "tf_test"
          cidr_block = "172.16.0.0/12"
        }
        output "vpc_id" {
          value = "${alicloud_vpc.vpc.id}"
        }
        创建vpc
    2. Edit the main.tf file in the root directory to create a vSwitch in the VPC.
      1. Click the main.tf file in the root directory.
      2. Enter the following code in the right-side code editor to create a vSwitch:
        module "my_vpc" {
          source      = "./modules/vpc"
        }
        resource "alicloud_vswitch" "vsw" {
          vpc_id            = "${module.my_vpc.vpc_id}"
          cidr_block        = "172.16.0.0/21"
          availability_zone = "cn-shanghai-b"
        }
        output "vsw_id" {
          value = "${alicloud_vswitch.vsw.id}"
        }
        创建vswitch
  6. In the upper-right corner of the Create Template page, choose Save Template > Save as My Template.
  7. In the Save as My Template dialog box, configure Template Name, Template Description, Resource Group, and Tag.
  8. Click OK.