我很难将我的Lambda与AWS Memcache连接起来。我使用下面的代码片段,我没有看到任何错误日志和函数超时。你能告诉我哪里出错了吗?
const MemcachePlus = require("memcache-plus");
const client = new MemcachePlus("test_memcached.cfg.use1.cache.amazonaws.com:11211");
exports.index = async (event) => {
try {
await client.set("firstName", "Victor", 10000);
console.log("Successfully set the key firstName");
const firstName = await client.get("firstName");
console.log(`Successfully got the key firstName: ${firstName}`);
} catch (e) {
console.log("error", e);
}
}
- 通过查看本文档来指导您打开VPC的DNS主机名https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html和CloudFormation堆栈中的Memcached URL作为输出,以便您能够将其作为环境变量添加到lambda
- 将您的lambda '连接'到vpc子网& &;安全组。这就是我如何使用无服务器框架,应该看起来非常相似时使用CloudFormation (https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html)
myLambdaFunc:
handler: src/myLambdaFunc.handler
vpc:
securityGroupIds:
- Fn::ImportValue: myapp-${{self:provider.stage}}-PrivateVPCLambdaSGID
subnetIds:
- Fn::ImportValue: myapp-${{self:provider.stage}}-PrivateVPCSubnet1Ref
- Fn::ImportValue: myapp-${{self:provider.stage}}-PrivateVPCSubnet2Ref
environment:
ELASTIC_CACHE_CONNECTION_URL:
Fn::ImportValue: myapp-${{self:provider.stage}}-ECURL
- 为您正在使用的语言/框架安装memcached库,并使用已传递的env变量进行连接