本文以创建阿里云专有网络(VPC)和交换机(vSwitch)为例,为您介绍如何将Terraform模板转换为ROS模板。

背景信息

关于Terraform模板的更多信息,请参见概览

步骤一:编辑源模板文件

Terraform模板文件分为main.tfoutput.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
    }

步骤二:转换模板

  • 方法一:执行以下命令,将Terraform模板转换为ROS模板,并在当前目录生成JSON格式的ROS模板文件template.json
    rostran transform templates/terraform/alicloud/main.tf --target-format json
  • 方法二:执行以下命令,将Terraform模板转换为ROS模板,并通过--source-format terraform指定源模板文件格式为Terraform,将文件命名为main.tf
    rostran transform templates/terraform/alicloud --source-format terraform

步骤三:查看转换后的ROS模板

打开template.json文件,查看ROS模板。

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