kumogata2-plugin-ruby
It is the Ruby plug-in of Kumogata2.
It convert the Ruby DSL to JSON.
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 (" Kumogata Sample Template\n You can use Here document!\n EOS\n\n Parameters do\n InstanceType do\n Default \"t2.micro\"\n Description \"Instance Type\"\n Type \"String\"\n end\n end\n\n Resources do\n myEC2Instance do\n Type \"AWS::EC2::Instance\"\n Properties do\n ImageId \"ami-XXXXXXXX\"\n InstanceType { Ref \"InstanceType\" }\n KeyName \"your_key_name\"\n\n UserData do\n Fn__Base64 (<<-EOS).undent\n #!/bin/bash\n yum install -y httpd\n service httpd start\n EOS\n end\n end\n end\n end\nend\n").undent
::is converted to__Fn::GetAtt=>Fn__GetAtt
_{ ... }is convered to HashSecurityGroups [_{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 (" #!/bin/bash\n /opt/aws/bin/cfn-init -s <%= Ref \"AWS::StackName\" %> -r myEC2Instance --region <%= Ref \"AWS::Region\" %>\n EOS\nend\n").fn_join
"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
