This topic describes how to convert a Terraform template to a Resource Orchestration Service (ROS) template. In this example, the template that you use to create a virtual private cloud (VPC) and a vSwitch is used.

Background information

For more information about Terraform templates, see Overview.

Step 1: Modify the Terraform template file

A Terraform template file consists of main.tf and output.tf. The following sample code shows the content of main.tf and output.tf:

  • main.tf
    # Configure the AliCloud Provider
    provider "alicloud" {}
    
    # Create VPC and VSwitch
    resource "alicloud_vpc" "myvpc" {
      cidr_block = "172.16.0.0/12"
      name       = "myvpc"
    }
    
    resource "alicloud_vswitch" "myvswitch" {
      vpc_id            = alicloud_vpc.myvpc.id
      cidr_block        = "172.16.0.0/21"
      availability_zone = "cn-beijing-g"
      name              = "myvswitch"
    }
  • output.tf
    output "vpc_id" {
      value = alicloud_vpc.myvpc.id
    }
    
    output "vswitch_id" {
      value = alicloud_vswitch.myvswitch.id
    }

Step 2: Convert the template

  • Method 1: Run the following command to convert the Terraform template to an ROS template and generate a JSON ROS template file named template.json in the directory in which the Terraform template file is stored:
    rostran transform templates/terraform/alicloud/main.tf --target-format json
  • Method 2: Run the following command to convert the Terraform template to an ROS template. In the command, --source-format terraform specifies that the template is a Terraform template. The template is named main.tf.
    rostran transform templates/terraform/alicloud --source-format terraform

Step 3: View the ROS template

Open the template.json file and view the ROS template that is generated. The following sample code shows the content of the ROS template:

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Resources": {
    "alicloud_vpc.myvpc": {
      "Properties": {
        "CidrBlock": "172.16.0.0/12",
        "VpcName": "myvpc"
      },
      "Type": "ALIYUN::ECS::VPC"
    },
    "alicloud_vswitch.myvswitch": {
      "Properties": {
        "CidrBlock": "172.16.0.0/21",
        "VSwitchName": "myvswitch",
        "VpcId": {
          "Fn::GetAtt": [
            "alicloud_vpc.myvpc",
            "VpcId"
          ]
        },
        "ZoneId": "cn-beijing-g"
      },
      "Type": "ALIYUN::ECS::VSwitch"
    }
  },
  "Outputs": {
    "vpc_id": {
      "Value": {
        "Fn::GetAtt": [
          "alicloud_vpc.myvpc",
          "VpcId"
        ]
      }
    },
    "vswitch_id": {
      "Value": {
        "Fn::GetAtt": [
          "alicloud_vswitch.myvswitch",
          "VSwitchId"
        ]
      }
    }
  }
}