Created
February 23, 2015 09:55
-
-
Save knshiro/a8569f7fde60ed301719 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'bundler/setup' | |
require 'cloudformation-ruby-dsl/cfntemplate' | |
require 'cloudformation-ruby-dsl/spotprice' | |
require 'cloudformation-ruby-dsl/table' | |
def tags_to_properties(tags) | |
tags.map { |k,v| {:Key => k, :Value}} | |
end | |
def vpc(name,cidr, enableDnsSupport:nil, enableDnsHostnames:nil, dependsOn:[], tags:{}) | |
properties = { | |
:CidrBlock => cidr, | |
} | |
properties[:EnableDnsSupport] = enableDnsSupport unless enableDnsSupport.nil? | |
properties[:EnableDnsHostnames] = enableDnsHostnames unless enableDnsHostnames.nil? | |
tags['Name'] = name unless tags.contains?('Name') | |
properties[:Tags] = tags_to_properties(tags) | |
options = { | |
:Type => 'AWS::EC2::VPC', | |
:Properties => properties | |
} | |
options[:DependsOn] = dependsOn unless dependsOn.empty? | |
resource name, options | |
end | |
def subnet(name,vpc,cidr, availabilityZone:'', dependsOn:[], tags:{}) | |
properties = { | |
:VpcId => vpc, | |
:CidrBlock => cidr, | |
} | |
properties[:AvailabilityZone] = availabilityZone unless availabilityZone.empty? | |
tags['Name'] = name unless tags.contains?('Name') | |
properties[:Tags] = tags_to_properties(tags) | |
options = { | |
:Type => 'AWS::EC2::Subnet', | |
:Properties => properties | |
} | |
options[:DependsOn] = dependsOn unless dependsOn.empty? | |
resource name, options | |
end | |
def security_group(name,description,securityGroupEgress:[], securityGroupIngress:[],dependsOn:[],tags:{}) | |
properties = { | |
:GroupDescription => description | |
} | |
properties[:SecurityGroupIngress] = securityGroupIngress unless securityGroupIngress.empty? | |
properties[:SecurityGroupEgress] = securityGroupEgress unless securityGroupEgress.empty? | |
tags['Name'] = name unless tags.contains?('Name') | |
properties[:Tags] = tags_to_properties(tags) | |
options = { | |
:Type => 'AWS::EC2::SecurityGroup', | |
:Properties => properties | |
} | |
options[:DependsOn] = dependsOn unless dependsOn.empty? | |
resource name, options | |
end | |
def security_group_vpc(name,description,vpc) | |
properties = { | |
:VpcId => vpc, | |
:GroupDescription => description | |
} | |
properties[:SecurityGroupIngress] = securityGroupIngress unless securityGroupIngress.empty? | |
properties[:SecurityGroupEgress] = securityGroupEgress unless securityGroupEgress.empty? | |
tags['Name'] = name unless tags.contains?('Name') | |
properties[:Tags] = tags_to_properties(tags) | |
options = { | |
:Type => 'AWS::EC2::SecurityGroup', | |
:Properties => properties | |
} | |
options[:DependsOn] = dependsOn unless dependsOn.empty? | |
resource name, options | |
end | |
def network_interface(device_index, options:{}) | |
options[:DeviceIndex] = device_index | |
options | |
end | |
def instance_vpc(name, image_id, subnet, security_groups, dependsOn:[], properties:{}) | |
raise "VPC instance #{name} can not contain NetworkInterfaces and subnet or security_groups" if properties.contains(:NetworkInterfaces) | |
raise "VPC instance #{name} can not contain non VPC SecurityGroups" if properties.contains(:SecurityGroups) | |
properties[:SubnetId] = subnet | |
properties[:SecurityGroupIds] = security_groups | |
options = { | |
:Type => 'AWS::EC2::Instance', | |
:Properties => properties | |
} | |
options[:DependsOn] = dependsOn unless dependsOn.empty? | |
resource name, options | |
end | |
def instance_with_network(name,image_id,network_interfaces, properties:{}) | |
raise "VPC instance #{name} can not contain NetworkInterfaces and subnet or security_groups" if properties.contains(:SubnetId) or properties.contains(:SecurityGroups) or properties.contains(:SecurityGroupIds) | |
properties[:NetworkInterfaces] = network_interface | |
options = { | |
:Type => 'AWS::EC2::Instance', | |
:Properties => properties | |
} | |
options[:DependsOn] = dependsOn unless dependsOn.empty? | |
resource name, options | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment