52
51

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.

Lambdaからhttpリクエスト

Last updated at Posted at 2014-11-30

#Lambdaからhttpリクエスト
この記事はAWS Lambda Advent Calendar 2014へのエントリー記事です。

Lambdaでは、node.js標準のモジュールが利用可能です。これを利用してhttpリクエスト[[1]][link-1]を実行してみました。

http://aws.amazon.com/lambda/faqs/
Q: What languages does AWS Lambda support?
At launch AWS Lambda supports code written in Node.js (JavaScript). Your code can include existing Node.js libraries, even native ones.

Function

var http = require ('http');
exports.handler = function(event, context) {
    console.log('value1 = ' + event.key1);
    http.get("http://www.google.com/index.html", function(res) {
        console.log("Got response: " + res.statusCode);
        
        res.on("data", function(chunk) {
            context.done(null, chunk);
        });
    }).on('error', function(e) {
        context.done('error', e);
    });
};

出力

Logs
----
START RequestId: 8e2c889c-74a0-11e4-b6d0-73063f569a63
2014-11-25T12:42:57.815Z	8e2c889c-74a0-11e4-b6d0-73063f569a63	value1 = value1
2014-11-25T12:42:57.968Z	8e2c889c-74a0-11e4-b6d0-73063f569a63	Got response: 200
END RequestId: 8e2c889c-74a0-11e4-b6d0-73063f569a63
REPORT RequestId: 8e2c889c-74a0-11e4-b6d0-73063f569a63	Duration: 221.39 ms	Billed Duration: 300 ms 	Memory Size: 128 MB	Max Memory Used: 10 MB	

Message
-------
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly 
...

終わりに

コールバックに初挑戦しました。動くものができるまで、様々な紆余曲折がありました。

参考文献

[1] http://nodejs.org/api/http.html#http_http_get_options_callback
[link-1]: http://nodejs.org/api/http.html#http_http_get_options_callback

52
51
0

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
52
51

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?