Repository is archived
No commit activity in last 3 years
No release in over 3 years
It is the Ruby plug-in of Kumogata2. It convert the Ruby DSL to JSON.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 12.3
~> 3.0

Runtime

~> 0.4
~> 0.1
 Project Readme

kumogata2-plugin-ruby

It is the Ruby plug-in of Kumogata2.

It convert the Ruby DSL to JSON.

Gem Version Build Status

Installation

Add this line to your application's Gemfile:

gem 'kumogata2-plugin-ruby'

And then execute:

$ bundle

Or install it yourself as:

$ gem install kumogata2-plugin-ruby

Usage

kumogata2 export my-stack --output-format rb > my-stack.rb
kumogata2 dry-run my-stack.rb my-stack
kumogata2 update my-stack.rb my-stack

Export using braces

EXPORT_RUBY_USE_BRACES=1 kumogata2 export ... --output-format rb

Export old format

EXPORT_RUBY_OLD_FORMAT=1 kumogata2 export ... --output-format rb

Example

template do
  AWSTemplateFormatVersion "2010-09-09"

  Description (<<-EOS).undent
    Kumogata Sample Template
    You can use Here document!
  EOS

  Parameters do
    InstanceType do
      Default "t2.micro"
      Description "Instance Type"
      Type "String"
    end
  end

  Resources do
    myEC2Instance do
      Type "AWS::EC2::Instance"
      Properties do
        ImageId "ami-XXXXXXXX"
        InstanceType { Ref "InstanceType" }
        KeyName "your_key_name"

        UserData do
          Fn__Base64 (<<-EOS).undent
            #!/bin/bash
            yum install -y httpd
            service httpd start
          EOS
        end
      end
    end
  end
end
  • :: is converted to __
    • Fn::GetAtt => Fn__GetAtt
  • _{ ... } is convered to Hash
    • SecurityGroups [_{Ref "WebServerSecurityGroup"}] => {"SecurityGroups": [{"Ref": "WebServerSecurityGroup"}]}
  • _path() creates Hash that has a key of path
    • _path("/etc/passwd-s3fs") { content "..." } => {"/etc/passwd-s3fs": {"content": "..."}}

String#fn_join()

Ruby templates will be converted as follows by String#fn_join():

UserData do
  Fn__Base64 (<<-EOS).fn_join
    #!/bin/bash
    /opt/aws/bin/cfn-init -s <%= Ref "AWS::StackName" %> -r myEC2Instance --region <%= Ref "AWS::Region" %>
  EOS
end
"UserData": {
  "Fn::Base64": {
    "Fn::Join": [
      "",
      [
        "#!/bin/bash\n",
        "/opt/aws/bin/cfn-init -s ",
        {
          "Ref": "AWS::StackName"
        },
        " -r myEC2Instance --region ",
        {
          "Ref": "AWS::Region"
        },
        "\n"
      ]
    ]
  }
}

Split a template file

  • template.rb
template do
  Resources do
    _include 'template2.rb', :ami_id => 'ami-XXXXXXXX'
  end
end
  • template2.rb
myEC2Instance do
  Type "AWS::EC2::Instance"
  Properties do
    ImageId args[:ami_id]
    InstanceType { Ref "InstanceType" }
    KeyName "your_key_name"
  end
end
  • Converted JSON template
{
  "Resources": {
    "myEC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-XXXXXXXX",
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "KeyName": "your_key_name"
      }
    }
  }
}

Post hook

You can run ruby script after building servers using post().

template do
  ...
end

post do |output|
  puts output
  #=> '{"WebsiteURL"=>"http://ec2-XX-XX-XX-XX.ap-northeast-1.compute.amazonaws.com"}'
end