gpt4 book ai didi

node.js - AWS Lambda 和 Redis

转载 作者:IT王子 更新时间:2023-10-29 06:09:44 24 4
gpt4 key购买 nike

我正在尝试编写一个使用 redis(在 amazon elasticcache 上)的 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 中安装 Redis。检查您的安全组设置。然后,如果您有 EC2 访问权限,请安装 redis-cli 并尝试连接 Redis。如果连接成功,您的 Lambda Redis 也将连接。如前所述,您需要将 lambda 放在同一个 VPC 中。以下是演示连接到 Lambda 的样板代码:

console.log('before client initialization')
const redisOptions = {
host: 'xxxx.xxx.xxx.xxx.xxx.amazonaws.com',
port: 6379,
}

var client = redis.createClient(redisOptions);
console.log('after client initialization');

client.on('connect', function(result) {
console.log('connected');
}

关于node.js - AWS Lambda 和 Redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39595689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com