lambchopとGuard

lambchopでLambdaの開発をちまちまとやっているんですが、「js更新」→「lambdaアップロード」→「イベント投げる」の流れがめんどくさいので、Guard使ってみました。

https://github.com/winebarrel/my-lambda

index.jsはこんな感じ。

#!/usr/bin/env lambchop -d
/*
function_name: hello
handler: index.handler
timeout: 60
role: arn:aws:iam::123456789012:role/lambda_basic_execution
*/
console.log('Loading function');

exports.handler = function(event, context) {
  //console.log('Received event:', JSON.stringify(event, null, 2));
  console.log('value1 =', event.key1);
  console.log('value2 =', event.key2);
  console.log('value3 =', event.key3);
  context.succeed(event.key1);  // Echo back the first key value
  // context.fail('Something went wrong');
};

Guardfileはこんな感じ。

guard :shell do
  watch(/index.js/) do |m|
    `lambchop -d #{m[0]}` +
    `echo '{"key1":100,"key2":200,"key3":300}' | lambchop-cat hello -l tail`
  end
end

コンソールでGuardを立ち上げて

$ bundle exec guard
16:11:26 - INFO - Guard is now watching at '/Users/sugawara/Projects/my-lambda'
[1] guard(main)>

適当にindex.jsを編集すると、index.jsがアップロードされてイベントが投げられます。

Function was uploaded:
{
  "function_name": "hello",
  "function_arn": "arn:aws:lambda:ap-northeast-1:822997939312:function:hello",
  "runtime": "nodejs",
  "role": "arn:aws:iam::123456789012:role/lambda_basic_execution",
  "handler": "index.handler",
  "code_size": 336,
  "description": "A starter AWS Lambda function.",
  "timeout": 60,
  "memory_size": 128,
  "last_modified": "2015-08-02 16:12:44 +0900"
}
Node modules:
[

]
---
status_code: 200
function_error:
payload: '100'
log_result: |-
  START RequestId: dfa6e0bf-38e5-11e5-94a2-9d06e7dbbcdc
  2015-08-02T07:12:45.779Z    dfa6e0bf-38e5-11e5-94a2-9d06e7dbbcdc    value1 = 100
  2015-08-02T07:12:45.779Z    dfa6e0bf-38e5-11e5-94a2-9d06e7dbbcdc    value2 = 200
  2015-08-02T07:12:45.779Z    dfa6e0bf-38e5-11e5-94a2-9d06e7dbbcdc    value3 = 300
  2015-08-02T07:12:45.779Z    dfa6e0bf-38e5-11e5-94a2-9d06e7dbbcdc    Zzz.. Zzz..
  END RequestId: dfa6e0bf-38e5-11e5-94a2-9d06e7dbbcdc
  REPORT RequestId: dfa6e0bf-38e5-11e5-94a2-9d06e7dbbcdc    Duration: 1.44 ms    Billed Duration: 100 ms     Memory Size: 128 MB    Max Memory Used: 9 MB
[1] guard(main)>