项目作者: drrb

项目描述 :
CloudFormation master
高级语言: Ruby
项目地址: git://github.com/drrb/cfoo.git
创建时间: 2013-04-25T10:54:02Z
项目社区:https://github.com/drrb/cfoo

开源协议:GNU General Public License v3.0

下载


Cfoo

Build Status
Coverage Status
Code Climate

Gem Version
Dependency Status

Cfoo (pronounced “sifu”) lets you write your CloudFormation
templates in YAML, and makes it easier with some helpers.

Installation

Cfoo can be installed as a Ruby Gem

  1. $ gem install cfoo

Usage

  1. Write your CloudFormation templates using Cfoo YAML

  2. Turn your Cfoo templates into normal CloudFormation templates

    1. $ cfoo web-server.template.yml database.template.yml > web-stack.template.json
  3. Create your stack with CloudFormation

    1. $ cfn-create-stack --stack-name WebStack -f web-stack.template.json

Templates

Comparison with standard CloudFormation templates

Snippet from a CloudFormation template (based on this example):

  1. "Properties": {
  2. "ImageId" : { "Fn::FindInMap" : [ "AWSRegion2AMI", { "Ref" : "AWS::Region" }, "AMI" ] },
  3. "InstanceType" : { "Ref" : "InstanceType" },
  4. "SecurityGroups" : [ {"Ref" : "FrontendGroup"} ],
  5. "KeyName" : { "Ref" : "KeyName" },
  6. "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
  7. "#!/bin/bash -v\n",
  8. "yum update -y aws-cfn-bootstrap\n",
  9. "function error_exit\n",
  10. "{\n",
  11. " /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '", { "Ref" : "WaitHandle" }, "'\n",
  12. " exit 1\n",
  13. "}\n",
  14. "/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackId" }, " -r WebServer ",
  15. " --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n",
  16. "/opt/aws/bin/cfn-signal -e 0 -r \"cfn-init complete\" '", { "Ref" : "WaitHandle" }, "'\n"
  17. ]]}}
  18. }

Equivalent Cfoo template snippet:

  1. Properties:
  2. ImageId : AWSRegion2AMI[$(AWS::Region)][AMI]
  3. InstanceType: $(InstanceType)
  4. SecurityGroups:
  5. - $(FrontendGroup)
  6. KeyName: $(KeyName)
  7. UserData: !Base64 |
  8. #!/bin/bash -v
  9. yum update -y aws-cfn-bootstrap
  10. function error_exit
  11. {
  12. /opt/aws/bin/cfn-signal -e 1 -r "\$1" '$(WaitHandle)'
  13. exit 1
  14. }
  15. /opt/aws/bin/cfn-init -s $(AWS::StackId) -r WebServer --region $(AWS::Region) || error_exit 'Failed to run cfn-init'
  16. /opt/aws/bin/cfn-signal -e 0 -r "cfn-init completed" '$(WaitHandle)'

Projects

Using Cfoo, it is possible to split your templates up into logical components that will
combined to form your CloudFormation template.

First, create a directory in your project directory called modules. For each module,
create some Cfoo templates defining the different parts of your app. Your project
structure will look like this:

  1. my-web-app
  2. └── modules
  3. ├── application
  4. ├── app_servers.yml
  5. ├── database.yml
  6. └── public_load_balancer.yml
  7. ├── instances
  8. └── instance_mappings.yml
  9. └── network
  10. ├── bastion.yml
  11. ├── cfn_user.yml
  12. ├── dns.yml
  13. ├── nat.yml
  14. ├── private_subnet.yml
  15. ├── public_subnet.yml
  16. └── vpc.yml

Use Cfoo to generate your project’s CloudFormation template

  1. $ cfoo > web-app.template.json

Shortcuts

Cfoo allows you to simplify CloudFormation intrinsic function
references using its own shorthand

Reference

CloudFormation: { "Ref" : "InstanceType" }

Cfoo Shortcut: $(InstanceType)

Mapping Reference

CloudFormation: { "FindInMap" : [ "SubnetConfig", "VPC", "CIDR" ] }

Cfoo Shortcut: $(SubnetConfig[VPC][CIDR])

Attribute Reference

CloudFormation: { "Fn::GetAtt" : [ "Ec2Instance", "PublicIp" ] }

Cfoo Shortcut: $(Ec2Instance[PublicIp])

Embedded Reference

CloudFormation: { "Fn::Join" : [ "", [{"Ref" : "HostedZone"}, "." ]]}

Cfoo Shortcut: $(HostedZone).

YAML Types

Cfoo gives you the option of using YAML custom data-types where it helps to make your templates easier to read.

Reference

CloudFormation:

  1. { "Ref" : "InstanceType" }

YAML Type:

  1. !Ref InstanceType
Mapping Reference

CloudFormation:

  1. { "FindInMap" : [ "SubnetConfig", "VPC", "CIDR" ] }

YAML Type:

  1. !FindInMap [ SubnetConfig, VPC, CIDR ]
Attribute Reference

CloudFormation:

  1. { "Fn::GetAtt" : [ "Ec2Instance", "PublicIp" ] }

YAML Type:

  1. !GetAtt [ Ec2Instance, PublicIp ]
Base64 String

CloudFormation:

  1. { "Fn::Base64" : "#!/bin/bash\necho 'Running script...'" }

YAML Type:

  1. !Base64 "#!/bin/bash\necho 'running script...'"

Alternative YAML Type:

  1. !Base64 |
  2. #!/bin/bash
  3. echo 'running script...'
Condition Function

CloudFormation:

  1. { "Fn::Equals": [ "sg-mysggroup", {"Ref": "ASecurityGroup"} ] },

YAML Type:

  1. !Equals [ "sg-mysggroup", $(ASecurityGroup) ]

Goals

Primary Goals

Cfoo aims to let developers simplify CloudFormation templates by:

  • allowing them to write templates in YAML
  • providing an expression language to simplify CF Ref/Attr/etc expressions
  • allowing templates to be split up into logical components (to simplify and share)

Secondary Goals

Cfoo also aims (subject to Primary Goals) to:

  • allow inclusion existing JSON templates (so you don’t have to switch all at once)

Non-goals

Cfoo does not (yet) aim to:

  • provide commandline utilities for interacting directly with CloudFormation (it just generates the templates for now)
  • resolve/validate references (the CloudFormation API already does this)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Make your changes (with tests please)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request