ズンドコキヨシ with AWS CloudFormation

  • 9
    Like
  • 0
    Comment
More than 1 year has passed since last update.

乗るしかない、このビッグウェーブに!

ズンドコキヨシまとめ

とは言っても、大体の言語は網羅されているような気がするので、AWSのCloudFormationでやってみようかと思います。

必要なもの

AWSのアカウントが必要です。

CloudFormation Lambda-backed カスタムリソース でやってみる

参考:AWS CloudFormation ユーザーガイド

CloudFormationのtemplateの中でLambdaFunctionを作成し、Custom::ZunDoCoリソースから実行しています。

ZunDoCo.json
{

  "AWSTemplateFormatVersion":"2010-09-09",
  "Description":"ZunDoCo Stack",
  "Parameters":{
    "Tries":{
      "Type":"Number",
      "MinValue":1
    }
  },
  "Resources":{
    "ZunDoCo":{
      "Type":"Custom::ZunDoCo",
      "Properties":{
        "ServiceToken":{"Fn::GetAtt":["ZunDoCoLambda","Arn"]},
        "Tries":{"Ref":"Tries"}
      }
    },
    "ZunDoCoLambda":{
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "ZipFile": {"Fn::Join": ["",[
            "var response = require('cfn-response');","\n",
            "exports.handler = function(event, context) {","\n",
            "    if(event.RequestType==='Delete'){","\n",
            "        response.send(event, context, 'SUCCESS');","\n",
            "        return;","\n",
            "    }","\n",
            "    var tries = Number(event.ResourceProperties.Tries);","\n",
            "    var success = false;","\n",
            "    var zunDoCoResult = '';","\n",
            "    var i = 0;","\n",
            "    for( ; i < tries && !success ; i++ ){","\n",
            "        for(var j = 0; j < 5; j++){","\n",
            "            zunDoCoResult += ['Zun', 'DoCo'][Math.random() * 2 | 0];","\n",
            "        }","\n",
            "        if(zunDoCoResult == 'ZunZunZunZunDoCo'){","\n",
            "            success = true;","\n",
            "        }else{","\n",
            "            zunDoCoResult = '';","\n",
            "        }","\n",
            "    }","\n",
            "    if(success){","\n",
            "        zunDoCoResult += ' ! KI-YO-SHI ! ';","\n",
            "        response.send(event, context, 'SUCCESS',{ZunDoCoResult:zunDoCoResult,Count:i + ''},'ZunDoCo');","\n",
            "    }else{","\n",
            "        response.send(event, context, 'FAILED',{Error:'You are not KIYOSHI.'},'ZunDoCo');","\n",
            "        //response.send(event, context, 'SUCCESS',{ZunDoCoResult:'You are not KIYOSHI.',Count:i + ''},'ZunDoCo');","\n",
            "    }","\n",
            "};","\n",
            ""
            ]]}
        },
        "Handler": "index.handler",
        "MemorySize": "128",
        "Runtime": "nodejs",
        "Timeout": "60",
        "Role": {"Fn::GetAtt": ["ZunDoCoLambdaRole","Arn"]}
      }
    },
    "ZunDoCoLambdaRole":{
      "Type": "AWS::IAM::Role",
      "Properties": {
        "Path": "/",
        "ManagedPolicyArns": ["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"],
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Action": "sts:AssumeRole",
              "Principal": {
                "Service": "lambda.amazonaws.com"
              },
              "Effect": "Allow"
            }
          ]
        }
      }
    }
  },
  "Outputs":{
    "ZunDoCoResult":{
      "Value":{"Fn::GetAtt":["ZunDoCo","ZunDoCoResult"]}
    },
    "Count":{
      "Value":{"Fn::GetAtt":["ZunDoCo","Count"]}
    }
  }
}

ParametersTriesの回数挑戦して、その回数の間にZunZunZunZunDoCoを達成すると、StackのOutputにあのフレーズを出力します。

CloudFormation_Management_Console.png

上記の場合、4回目でキヨシを達成しています。

Triesまでにキヨシを達成できなかった場合は、StackのCreateまたはUpdateがエラーになります。

CloudFormation_Management_Console.png

Update Stack

StackのUpdateで再挑戦する場合、同じjsonファイルをアップロードして、ParametersのTriesの数字を変更してください。 何も変更が無いとCloudFormationさんにエラー(何も変更がない)で怒られます。

AWS利用料金について

ParametersのTriesにいくつを指定しても、Lambdaの実行回数は1回なので、大体の人は無料利用枠に収まっていると思います。