【发布时间】:2017-01-28 11:20:22
【问题描述】:
我正在尝试编写一个使用 redis(在亚马逊弹性缓存上)的 AWS Lambda 函数。问题 - 我无法连接到 redis。我使用这样的代码
'use strict'
function handler (data, context, cb) {
const redis = require("redis")
console.log('before client initialization')
const client = redis.createClient({
url: 'redis://propper-url-cache.some.0001.euw1.cache.amazonaws.com:6379',
retry_strategy: function(options) {
console.log(options)
if (options.total_retry_time > 1000) {
throw new Error('can`t connect to redis')
}
}
})
console.log('after client initialization')
client.on("error", function (err) {
console.log('in error')
cb({error: err})
});
client.get("counter", function (err, counter) {
console.log('counter', counter)
if(_.isNull(counter)) {
counter = 0
}
client.set('counter', counter + 1, function(err) {
console.log(err)
cb(null, {counter: counter})
})
});
}
exports.handler = handler
结果我在日志中看到了这样的内容:
15:33:41
START RequestId: d8024ec2-7f36-11e6-996c-1bfcb60572c6 Version: $LATEST
15:33:42
2016-09-20T13:33:42.632Z d8024ec2-7f36-11e6-996c-1bfcb60572c6 before client initialization
15:33:42
2016-09-20T13:33:42.813Z d8024ec2-7f36-11e6-996c-1bfcb60572c6 after client initialization
15:33:44
END RequestId: d8024ec2-7f36-11e6-996c-1bfcb60572c6
15:33:44
REPORT RequestId: d8024ec2-7f36-11e6-996c-1bfcb60572c6 Duration: 3002.67 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 19 MB
15:33:44
2016-09-20T13:33:44.620Z d8024ec2-7f36-11e6-996c-1bfcb60572c6 Task timed out after 3.00 seconds
当我将 redis url 更改为绝对没有意义的东西时,我有一个额外的行:
2016-09-20T13:29:42.953Z 48fcb071-7f36-11e6-bc52-c5ac58c12843 { attempt: 1, error: { [Error: Redis connection to some-url.euw1.cache.amazonaws.com:6379 failed - getaddrinfo ENOTFOUND some-url.euw1.cache.amazonaws.com some-url.euw1.cache.amazonaws.com:6379] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo', hostna
有什么想法吗?
【问题讨论】:
-
您是否为 Lambda 函数启用了 VPC 访问?
-
是的,问题在于 VPC 访问。谢谢。你知道我为什么会有不同的行为吗?
-
当您尝试访问存在的内容,但您没有网络访问权限(由于不正确的 VPC 配置)时,您会遇到超时。当您尝试访问不存在的内容时,您会收到“未找到”错误。
-
@MarkB 我可以问我的其他问题stackoverflow.com/questions/39598158/… 吗?
标签: node.js amazon-web-services redis aws-lambda amazon-elasticache