如何从 AWS Lambda 访问 Memcache?

How to access Memcache from AWS Lambda?

我在将 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);
    }
}
  1. 确保通过查看此文档来指导您为您的 VPC 打开 DNS 主机名 https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html 并将 CloudFormation 堆栈中的 Memcached URL 作为输出,以便您能够添加它作为你的 lambda
  2. 的环境变量
  3. 'Connect' 您对 VPC 子网和安全组的 lambda。这就是我使用无服务器框架的方式,在使用 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
  1. 为您正在使用的 language/framework 安装 Memached 库,并使用已传递的环境变量进行连接