[英]HTTP Requests in an AWS Lambda
I'm new to Lambdas so perhaps there is something I've not caught on to just yet, but I've written a simple Lambda function to do an HTTP request to an external site. 我是Lambdas的新手所以也许还有一些我还没有抓到的东西,但是我已经编写了一个简单的Lambda函数来向外部网站发出HTTP请求。 For some reason, whether I use Node's
http
or https
modules, I get an ECONNREFUSED
. 出于某种原因,无论我使用Node的
http
还是https
模块,我都会获得ECONNREFUSED
。
Here's my Lambda: 这是我的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");
});
};
Here's the log output: 这是日志输出:
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
Is there a role permission I need to have to do HTTP requests? 是否需要执行HTTP请求的角色权限? Does Lambda even permit plain old HTTP requests?
Lambda甚至允许普通的旧HTTP请求吗? Are there special headers I need to set?
我需要设置特殊标头吗?
Any guidance is appreciated. 任何指导表示赞赏。
I solved my own problem. 我解决了自己的问题。
Apparently, if you decide to feed the URL in as the first parameter to .get()
, you must include the http://
up front of the URL, eg, http://www.google.com
. 显然,如果您决定将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");
});
};
Alternatively, you can specify the first parameter as a hash of options , where hostname
can be the simple form of the URL. 或者,您可以将第一个参数指定为选项的哈希值 ,其中
hostname
可以是URL的简单形式。 Example: 例:
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");
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.