本文以创建阿里云专有网络(VPC)和安全组(SecurityGroup)为例,为您介绍如何将AWS CloudFormation模板转换为ROS模板。

背景信息

关于AWS CloudFormation模板的更多信息,请参见Working with AWS CloudFormation templates

步骤一:编辑源模板文件

AWS CloudFormation模板文件示例如下:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "This template create vpc security group.",
  "Metadata": {
    "AWS::CloudFormation::Interface": {
      "ParameterGroups": {
        "Parameters": [
          "CidrBlock"
        ],
        "Label": {
          "default": "VPC"
        }
      },
      "ParameterLabels": {
        "CidrBlock": {
          "default": "Vpc Cidr Block"
        }
      }
    }
  },
  "Parameters": {
    "CidrBlock": {
      "Type": "String",
      "Default": "10.0.0.0/16"
    }
  },
  "Resources": {
    "MyVpc": {
      "DeletionPolicy": "Retain",
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": {
          "Ref": "CidrBlock"
        },
        "EnableDnsSupport": "false",
        "EnableDnsHostnames": "false",
        "InstanceTenancy": "dedicated",
        "Tags": [
          {
            "Key": "foo",
            "Value": "bar"
          }
        ]
      }
    },
    "MySg": {
      "DependsOn": "MyVpc",
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Create vpc security group",
        "VpcId": {
          "Ref": "MyVpc"
        }
      }
    }
  },
  "Outputs": {
    "VpcId": {
      "Description": "Vpc ID",
      "Value": {
        "Ref": "MyVpc"
      }
    },
    "SgId": {
      "Description": "SecurityGroup ID",
      "Value": {
        "Fn::GetAtt": [
          "MySg",
          "GroupId"
        ]
      }
    }
  }
}

步骤二:转换模板

执行以下命令,将AWS CloudFormation模板转换为ROS模板,并在当前目录生成JSON格式的ROS模板文件template.json

rostran transform templates/cloudformation/vpc_sg.json --target-format json

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

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

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Description": "This template create vpc security group.",
  "Metadata": {
    "ALIYUN::ROS::Interface": {
      "ParameterGroups": {
        "Parameters": [
          "CidrBlock"
        ],
        "Label": {
          "default": "VPC"
        }
      },
      "ParameterLabels": {
        "CidrBlock": {
          "default": "Vpc Cidr Block"
        }
      }
    }
  },
  "Parameters": {
    "CidrBlock": {
      "Type": "String",
      "Default": "10.0.0.0/16",
      "Label": "Vpc Cidr Block"
    }
  },
  "Resources": {
    "MyVpc": {
      "Type": "ALIYUN::ECS::VPC",
      "Properties": {
        "CidrBlock": {
          "Ref": "CidrBlock"
        },
        "Tags": [
          {
            "Key": "foo",
            "Value": "bar"
          }
        ]
      },
      "DeletionPolicy": "Retain"
    },
    "MySg": {
      "Type": "ALIYUN::ECS::SecurityGroup",
      "Properties": {
        "Description": "Create vpc security group",
        "VpcId": {
          "Ref": "MyVpc"
        }
      },
      "DependsOn": "MyVpc"
    }
  },
  "Outputs": {
    "VpcId": {
      "Value": {
        "Ref": "MyVpc"
      }
    },
    "SgId": {
      "Value": {
        "Fn::GetAtt": [
          "MySg",
          "SecurityGroupId"
        ]
      }
    }
  }
}