CloudFormation+OpsWorks+Github(Private)勘所

#1. はじめに
* VPCの土台、OpsWorks の Stack と Layer と Instance、Github（Private）から Setup recipes　取得して実行するまでを行う　CloudFormation のテンプレートを作成するときに、つまずいたところを記す。
* テンプレート実行環境は、Win, Mac　両方で動作確認済み。
* 実際のテンプレートは、[こちら](http://qiita.com/tonishy/items/51432640ea46fd08b1c5)

#2. CloudFormationテンプレート要点
* Githubの鍵をCloudFormationにべた書きするのはよろしくないため、AWSマネコン上から入力させる（参考：[本家ドキュメント](http://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-opsworks-stack-source.html/)）

```json:sample.json
    "Parameters": {
        ...
        "GitSSHKey": {
            "Description": "Change SSH key newlines to commas.",
            "Type": "CommaDelimitedList",
            "NoEcho": "true"
        }
　　　　　　　　　　　　　　　　...
    },
```
* Opsworksリソース（参考：[本家ドキュメント](http://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-opsworks-stack-source.html/)）

```json:sample.json
        "OpsWorksStack": {
            "Type": "AWS::OpsWorks::Stack",
            "Properties": {
                ...
                "UseCustomCookbooks": "true",
                "CustomCookbooksSource": {
                    "Type": "git",
                    "Url": "git@github.com:tonishy/awsec2-settings.git",
                    "Revision": "master",
                    "SshKey": {
                        "Fn::Join": [
                            "\n",
                            {
                                "Ref": "GitSSHKey"
                            }
                        ]
                    }
                },
```

#3. CloudFormationテンプレート実行時、AWSマネコン上から入力するGithubのキーは、次のように「newlines」を「comma」へ変換したもの（１行）を入力する
* 従来

```
-----BEGINRSAPRIVATEKEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxx,-----ENDRSAPRIVATEKEY-----
```

#4. 検証の際など、CloudFormationでOpsWorksリソースを何度も作り直す際に、CloudFrontのイベントエラーで次のようなエラーがでて、ロールバックすることがある
```
12:08:30 UTC+0900	ROLLBACK_IN_PROGRESS	AWS::CloudFormation::Stack	stress	The following resource(s) failed to create: [OpsWorksStack]. . Rollback requested by user.
12:08:29 UTC+0900	CREATE_FAILED	AWS::OpsWorks::Stack	OpsWorksStack	Service Role Arn: Credential has not the necessary trust relationship
```
CloudFormationのスタックをすべて削除したあと、３０分くらい待ってから再度CloudFormationを実行すればエラーは出ない。
（もっといい方法はないのかな？）