36
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CoffeeFormation

Last updated at Posted at 2014-06-28

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でインストールできるようにしてみた。

36
36
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?