0
votes

I am trying to generate AWS cloudformation YAML from ruby hash. But I am not able to figure out on how to represent the aws internal function in ruby hash. e.g what would be the equivalent ruby object for the following snippet ?

Resources:
  AppNode:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.large
      ImageId: ami-0def3275
      KeyName: mykey
      SecurityGroups:
        - !Ref AppNodeSG
  AppNodeSG:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: for the app nodes that allow ssh port
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: '22'
        ToPort: '22'
        CidrIp: 0.0.0.0/0
1

1 Answers

0
votes

This would be:

{ 'Resources' => {
  'AppNode' => {
    'Type' => 'AWS::EC2::Instance',
    'Properties' => {
      'InstanceType' => 't2.large',
      'ImageId' => 'ami-0def3275',
      'KeyName' => 'mykey',
      'SecurityGroups' => ['AppNodeSG']
    }
  },
  'AppNodeSG' => {
    'Type' => 'AWS::EC2::SecurityGroup',
    'Properties' => {
      'GroupDescription' => 'for the app nodes that allow ssh port',
      'SecurityGroupIngress' => [{ 'IpProtocol' => 'tcp', 'FromPort' => '22', 'ToPort' => '22', 'CidrIp' => '0.0.0.0/0' }]
    }
  }
} }

Ruby has YAML built in which allows you to parse YAML into a hash or convert a hash into YAML.