AWS Lambda中的HTTP请求
我是Lambdas的新手,所以也许还有一些我还没有注意到,但是我写了一个简单的Lambda函数来向外部站点发出HTTP请求。 出于某种原因,无论我使用Node的http
还是https
模块,我都会得到一个ECONNREFUSED
。
这是我的Lambda:
var http = require('http'); exports.handler = function (event, context) { http.get('www.google.com', function (result) { console.log('Success, with: ' + result.statusCode); context.done(null); }).on('error', function (err) { console.log('Error, with: ' + err.message); context.done("Failed"); }); };
这里是日志输出:
START RequestId: request hash 2015-08-04T14:57:56.744Z request hash Error, with: connect ECONNREFUSED 2015-08-04T14:57:56.744Z request hash {"errorMessage":"Failed"} END RequestId: request hash
有没有我需要做的HTTP请求的angular色权限? Lambda甚至允许普通的旧HTTP请求? 有我需要设置特殊的标题?
任何指导表示赞赏。
我解决了我自己的问题。
很明显,如果您决定将URL作为第一个参数提供给.get()
,则必须在URL的前面包含http://
,例如http://www.google.com
。
var http = require('http'); exports.handler = function (event, context) { http.get('http://www.google.com', function (result) { console.log('Success, with: ' + result.statusCode); context.done(null); }).on('error', function (err) { console.log('Error, with: ' + err.message); context.done("Failed"); }); };
或者,您可以将第一个参数指定为选项的散列 ,其中hostname
可以是URL的简单forms。 例:
var http = require('http'); exports.handler = function (event, context) { var getConfig = { hostname: 'www.google.com' }; http.get(getConfig, function (result) { console.log('Success, with: ' + result.statusCode); context.done(null); }).on('error', function (err) { console.log('Error, with: ' + err.message); context.done("Failed"); }); };