Help us understand the problem. What is going on with this article?

CoffeeFormation

More than 5 years have passed since last update.

CoffeeFormation とは?

Coffee Script + Cloud Formation = CoffeeFormation

CoffeScriptの記法でCloudFormationのテンプレートを書こうという物です。

どんな感じで書くの?

Resources.Bucket=
  Type: "AWS::S3::Bucket"
  Properties:
    BucketName: "my-special-bucket"

これが

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "Bucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketName": "my-special-bucket"
      }
    }
  }
}

こうなります。

どう使うの?

# npm経由でインストール
$ npm install -g csfn
/usr/local/bin/csfn -> /usr/local/lib/node_modules/csfn/bin/csfn
csfn@1.0.0 /usr/local/lib/node_modules/csfn
└── coffee-script@1.7.1 (mkdirp@0.3.5)

# 変換して出力してみる
$ cat bucket.coffee
Resources.Bucket=
  Type:"AWS::S3::Bucket"
  Properties:
    BucketName: "my-special-bucket"
$ csfn bucket.coffee
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "Bucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketName": "my-special-bucket"
      }
    }
  }
}

# AWS CLIに渡す
$ aws cloudformation create-stack --stack-name mybucket --template-body "$(csfn bucket.coffee)"
arn:aws:cloudformation:ap-northeast-1:123456789012:stack/mybucket/5de182a0-fe91-11e3-92e7-5088487c4896

何がいいの?

以下のメリットがあります。

  • コメントが書ける
  • JSONの文法エラーがなくなる (Atom等のエディタで随時コンパイルしながら書けばさらに安心)
  • ヒアドキュメントが書ける
commands = '''
#!/bin/sh
yum install -y httpd
service httod start
chkconfig httpd on
'''

:

Resources.Webserver=
  Type: "AWS::EC2::Instance"
  Proprerties:
     :
    UserData: "Fn::Base64":command

  • 繰り返しの定義をコードに出来る
Resources.Master=
  Type: "AWS::RDS::DBInstance"
  Properties:
    AllocatedStorage: 5
    DBInstanceClass: "db.t1.micro"
    Engine: "mysql"
    EngineVersion: "5.6.17"
    MasterUsername: "admin"
    MasterUserPassword: "password"

for i in [1..5]
  Resources["Slave"+i]=
    Type: "AWS::RDS::DBInstance"
    Properties:
      SourceDBInstanceIdentifier: "Ref":"Master"

追記

少し改良。いきなり、"Resources.hoge=" で書き始められるようになりました。
リソースをグルーピングして、別ファイルに分けたい場合とかに便利かもしれない。

追記2

npmでインストールできるようにしてみた。

j3tm0t0
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした