README.md in sparkle_formation-0.1.6 vs README.md in sparkle_formation-0.2.0
- old
+ new
@@ -18,65 +18,60 @@
Lets use one of the example CF templates that creates an EC2 instance. First
we can just convert it into a single file (ec2_example.rb):
```ruby
SparkleFormation.new('ec2_example') do
- description "AWS CloudFormation Sample Template EC2InstanceSample: Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example uses the default security group, so to SSH to the new instance using the KeyPair you enter, you will need to have port 22 open in your default security group. **WARNING** This template an Amazon EC2 instances. You will be billed for the AWS resources used if you create a stack from this template."
+ description "AWS CloudFormation Sample Template ..."
- parameters do
- key_name do
- description 'Name of an existing EC2 KeyPair to enable SSH access to the instance'
- type 'String'
- end
+ parameters.keyname do
+ description 'Name of EC2 key pair'
+ type 'string'
end
mappings.region_map do
- _set('us-east-1', :ami => 'ami-7f418316')
- _set('us-east-1', :ami => 'ami-7f418316')
- _set('us-west-1', :ami => 'ami-951945d0')
- _set('us-west-2', :ami => 'ami-16fd7026')
- _set('eu-west-1', :ami => 'ami-24506250')
- _set('sa-east-1', :ami => 'ami-3e3be423')
- _set('ap-southeast-1', :ami => 'ami-74dda626')
- _set('ap-northeast-1', :ami => 'ami-dcfa4edd')
+ set!('us-east-1', :ami => 'ami-7f418316')
+ set!('us-east-1', :ami => 'ami-7f418316')
+ set!('us-west-1', :ami => 'ami-951945d0')
+ set!('us-west-2', :ami => 'ami-16fd7026')
+ set!('eu-west-1', :ami => 'ami-24506250')
+ set!('sa-east-1', :ami => 'ami-3e3be423')
+ set!('ap-southeast-1', :ami => 'ami-74dda626')
+ set!('ap-northeast-1', :ami => 'ami-dcfa4edd')
end
- resources do
- my_instance do
- type 'AWS::EC2::Instance'
- properties do
- key_name _cf_ref(:key_name)
- image_id _cf_map(:region_map, 'AWS::Region', :ami)
- user_data _cf_base64('80')
- end
+ dynamic!(:ec2_instance, :foobar) do
+ properties do
+ key_name ref!(:key_name)
+ image_id map!(:region_map, 'AWS::Region', :ami)
+ user_data base64!('80')
end
end
outputs do
instance_id do
description 'InstanceId of the newly created EC2 instance'
- value _cf_ref(:my_instance)
+ value ref!(:foobar_ec2_instance)
end
az do
description 'Availability Zone of the newly created EC2 instance'
- value _cf_attr(:my_instance, :availability_zone)
+ value attr!(:foobar_ec2_instance, :availability_zone)
end
public_ip do
description 'Public IP address of the newly created EC2 instance'
- value _cf_attr(:my_instance, :public_ip)
+ value attr!(:foobar_ec2_instance, :public_ip)
end
private_ip do
description 'Private IP address of the newly created EC2 instance'
- value _cf_attr(:my_instance, :private_ip)
+ value attr!(:foobar_ec2_instance, :private_ip)
end
public_dns do
description 'Public DNSName of the newly created EC2 instance'
- value _cf_attr(:my_instance, :public_dns_name)
+ value attr!(:foobar_ec2_instance, :public_dns_name)
end
private_dns do
description 'Private DNSName of the newly created EC2 instance'
- value _cf_attr(:my_instance, :private_dns_name)
+ value attr!(:foobar_ec2_instance, :private_dns_name)
end
end
end
```
@@ -108,74 +103,70 @@
templates.
First, create the component (components/ami.rb):
```ruby
-SparkleFormation.build do
+SparkleFormation.build(:ami) do
- parameters do
- key_name do
- description 'Name of an existing EC2 KeyPair to enable SSH access to the instance'
- type 'String'
- end
- end
-
mappings.region_map do
- _set('us-east-1', :ami => 'ami-7f418316')
- _set('us-east-1', :ami => 'ami-7f418316')
- _set('us-west-1', :ami => 'ami-951945d0')
- _set('us-west-2', :ami => 'ami-16fd7026')
- _set('eu-west-1', :ami => 'ami-24506250')
- _set('sa-east-1', :ami => 'ami-3e3be423')
- _set('ap-southeast-1', :ami => 'ami-74dda626')
- _set('ap-northeast-1', :ami => 'ami-dcfa4edd')
+ set!('us-east-1', :ami => 'ami-7f418316')
+ set!('us-east-1', :ami => 'ami-7f418316')
+ set!('us-west-1', :ami => 'ami-951945d0')
+ set!('us-west-2', :ami => 'ami-16fd7026')
+ set!('eu-west-1', :ami => 'ami-24506250')
+ set!('sa-east-1', :ami => 'ami-3e3be423')
+ set!('ap-southeast-1', :ami => 'ami-74dda626')
+ set!('ap-northeast-1', :ami => 'ami-dcfa4edd')
end
+
end
```
Now, we can modify our initial example to use this component (ec2_example.rb):
```ruby
-SparkleFormation.new('ec2_example').load(:ami).overrides do
+SparkleFormation.new('ec2_example').load(:ami) do
- description "AWS CloudFormation Sample Template EC2InstanceSample: Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example uses the default security group, so to SSH to the new instance using the KeyPair you enter, you will need to have port 22 open in your default security group. **WARNING** This template an Amazon EC2 instances. You will be billed for the AWS resources used if you create a stack from this template."
+ description "AWS CloudFormation Sample Template ..."
- resources do
- my_instance do
- type 'AWS::EC2::Instance'
- properties do
- key_name _cf_ref(:key_name)
- image_id _cf_map(:region_map, 'AWS::Region', :ami)
- user_data _cf_base64('80')
- end
+ parameters.keyname do
+ description 'Name of EC2 key pair'
+ type 'string'
+ end
+
+ dynamic!(:ec2_instance, :foobar) do
+ properties do
+ key_name ref!(:key_name)
+ image_id map!(:region_map, 'AWS::Region', :ami)
+ user_data base64!('80')
end
end
outputs do
instance_id do
description 'InstanceId of the newly created EC2 instance'
- value _cf_ref(:my_instance)
+ value ref!(:foobar_ec2_instance)
end
az do
description 'Availability Zone of the newly created EC2 instance'
- value _cf_attr(:my_instance, :availability_zone)
+ value attr!(:foobar_ec2_instance, :availability_zone)
end
public_ip do
description 'Public IP address of the newly created EC2 instance'
- value _cf_attr(:my_instance, :public_ip)
+ value attr!(:foobar_ec2_instance, :public_ip)
end
private_ip do
description 'Private IP address of the newly created EC2 instance'
- value _cf_attr(:my_instance, :private_ip)
+ value attr!(:foobar_ec2_instance, :private_ip)
end
public_dns do
description 'Public DNSName of the newly created EC2 instance'
- value _cf_attr(:my_instance, :public_dns_name)
+ value attr!(:foobar_ec2_instance, :public_dns_name)
end
private_dns do
description 'Private DNSName of the newly created EC2 instance'
- value _cf_attr(:my_instance, :private_dns_name)
+ value attr!(:foobar_ec2_instance, :private_dns_name)
end
end
end
```
@@ -194,61 +185,78 @@
parts to multiple templates. So what do we use?
Enter `dynamics`. These are much like components, except that instead of simply
being merged, they allow passing of arguments which makes them reusable to create
unique resources. So, from our last example, lets move the ec2 related items
-into a dynamic (dynamics/ec2.rb):
+into a dynamic (dynamics/node.rb):
```ruby
-SparkleFormation.dynamic(:ec2) do |_name|
- resources("#{_name}_instance".to_sym)
- type 'AWS::EC2::Instance'
+SparkleFormation.dynamic(:node,
+ :parameters => {
+ :key_name => {
+ :type => 'String',
+ :description => 'Optionally make keypair static'
+ }
+ }
+) do |_name, _config|
+
+ if(_config[:key_name])
+ parameters.keyname do
+ description 'Name of EC2 key pair'
+ type 'string'
+ end
+ end
+
+ dynamic!(:ec2_instance, _name) do
properties do
- key_name _cf_ref(:key_name)
- image_id _cf_map(:region_map, 'AWS::Region', :ami)
- user_data _cf_base64('80')
+ key_name _config.fetch(:key_name, ref!(:key_name))
+ image_id map!(:region_map, 'AWS::Region', :ami)
+ user_data baes64!('80')
end
end
outputs("#{_name}_instance_id".to_sym) do
description 'InstanceId of the newly created EC2 instance'
- value _cf_ref("#{_name}_instance".to_sym)
+ value ref!("#{_name}_ec2_instance".to_sym)
end
outputs("#{_name}_az".to_sym) do
description 'Availability Zone of the newly created EC2 instance'
- value _cf_attr("#{_name}_instance".to_sym, :availability_zone)
+ value attr!("#{_name}_ec2_instance".to_sym, :availability_zone)
end
outputs("#{_name}_public_ip".to_sym) do
description 'Public IP address of the newly created EC2 instance'
- value _cf_attr("#{_name}_instance".to_sym, :public_ip)
+ value attr!("#{_name}_ec2_instance".to_sym, :public_ip)
end
outputs("#{_name}_private_ip".to_sym) do
description 'Private IP address of the newly created EC2 instance'
- value _cf_attr("#{_name}_instance".to_sym, :private_ip)
+ value attr!("#{_name}_ec2_instance".to_sym, :private_ip)
end
outputs("#{_name}_public_dns".to_sym) do
description 'Public DNSName of the newly created EC2 instance'
- value _cf_attr("#{_name}_instance".to_sym, :public_dns_name)
+ value attr!("#{_name}_ec2_instance".to_sym, :public_dns_name)
end
outputs("#{_name}_private_dns".to_sym) do
description 'Private DNSName of the newly created EC2 instance'
- value _cf_attr("#{_name}_instance".to_sym, :private_dns_name)
+ value attr!("#{_name}_ec2_instance".to_sym, :private_dns_name)
end
end
```
Now we can put all of these together, and create multiple ec2 instance
resource easily:
```ruby
SparkleFormation.new('ec2_example').load(:ami).overrides do
- description "AWS CloudFormation Sample Template EC2InstanceSample: Create an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example uses the default security group, so to SSH to the new instance using the KeyPair you enter, you will need to have port 22 open in your default security group. **WARNING** This template an Amazon EC2 instances. You will be billed for the AWS resources used if you create a stack from this template."
+ description "AWS CloudFormation Sample Template ..."
- [:node1, :node2, :node3].each do |_node_name|
- SparkleFormation.insert(:ec2, self, _node_name)
+ %w(node1 node2 node3).each do |_node_name|
+ dynamic!(:node, _node_name)
end
+ # and include one with predefined keypair
+
+ dynamic!(:node, 'snowflake', :key_pair => 'snowkeys')
end
```
## TODO
* Add information about symbol importance