Transform a Terraform template into an ROS template

更新时间:
复制 MD 格式

Use the rostran CLI tool to convert a Terraform template into a Resource Orchestration Service (ROS) template. This tutorial walks through a working example that provisions an Alibaba Cloud VPC and vSwitch.

How it works

rostran reads your Terraform HCL files and outputs an equivalent ROS JSON template. This lets you manage Alibaba Cloud infrastructure through native ROS tooling without rewriting your configuration from scratch.

For an overview of Terraform templates and how they relate to ROS, see

Overview.

Step 1: Edit the source template files

A Terraform template consists of two files: main.tf and output.tf. Create these files with the following content.

  • main.tf

    Defines the provider and two resources: a VPC and a vSwitch that belongs to the VPC.

    # 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

    Declares the VPC ID and vSwitch ID as outputs so they can be referenced after deployment.

    output "vpc_id" {
      value = alicloud_vpc.myvpc.id
    }
    
    output "vswitch_id" {
      value = alicloud_vswitch.myvswitch.id
    }

Step 2: Transform the template

Run one of the following commands. Both produce the same template.json output — choose the one that fits your workflow.

  • Method 1 (recommended for single-file input): Specify the entry point file directly and set the output format to JSON.

    rostran transform templates/terraform/alicloud/main.tf --target-format json

    This command converts main.tf (and its companion output.tf) and writes a JSON-formatted ROS template named template.json to the current directory.

  • Method 2 (recommended for directory input): Point to the directory containing all Terraform files and use --source-format terraform to specify the source format.

    rostran transform templates/terraform/alicloud --source-format terraform

    This command scans the specified directory for Terraform files — including main.tf — and writes template.json to the current directory.

Step 3: Verify the transformed ROS template

Open template.json in the current directory to verify the conversion. The file contains the full ROS template in JSON format.

Key differences to note in the output:

  • Terraform resource references (alicloud_vpc.myvpc.id) become Fn::GetAtt intrinsic function calls in ROS.

  • The availability_zone field maps to ZoneId.

  • Terraform output blocks map directly to the ROS Outputs section.

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