読者です 読者をやめる 読者になる 読者になる

AWS Lambdaで外部のURLをたたく

lambchopができたので、AWS Lambdaでいろいろ試行錯誤中。

以下のようなスクリプトを立ち上げて、echo '{}' | lambchop-cat testでイベントおくってやると、外部のURLをたたけた。

#!/usr/bin/env lambchop
/*
function_name: test
role: arn:aws:iam::822997939312:role/lambda_exec_role
handler: test.handler
*/
var http = require('http');

exports.handler = function(event, context) {
  http.get('http://example.com/', function(res) {
    res.setEncoding('utf8');
    res.on('data', function(str) {
      console.log(str);
      context.done();
    });
  });
};
~/work$ ./test.js
Function was uploaded:
{
  "function_name": "test",
  "function_arn": "arn:aws:lambda:us-east-1:NNNNNNNNNNNN:function:test",
  "configuration_id": "8a3ce7a6-a545-4b61-9bcd-c97fe87507bc",
  "runtime": "nodejs",
  "role": "arn:aws:iam::NNNNNNNNNNNN:role/lambda_exec_role",
  "handler": "test.handler",
  "mode": "event",
  "code_size": 74641,
  "description": "",
  "timeout": 3,
  "memory_size": 128,
  "last_modified": "2014-11-23 21:11:52 +0900"
}
START RequestId: e8ce956d-7309-11e4-a426-6b0834bf3dac
2014-11-23T12:12:00.374Z  e8ce956d-7309-11e4-a426-6b0834bf3dac   <!doctype html>
<html>
<head>
    <title>Example Domain</title>
    ...
</body>
</html>

END RequestId: e8ce956d-7309-11e4-a426-6b0834bf3dac
REPORT RequestId: e8ce956d-7309-11e4-a426-6b0834bf3dac Duration: 297.82 ms Billed Duration: 300 ms    Memory Size: 128 MB    Max Memory Used: 10 MB

LambdaというよりNodeの勉強だな…