【问题标题】:How to access Memcache from AWS Lambda?如何从 AWS Lambda 访问 Memcache?
【发布时间】:2021-11-24 15:58:56
【问题描述】:

我很难将我的 Lambda 与 AWS Memcache 连接起来。我正在使用下面的代码 sn-p,我没有看到任何错误日志并且函数超时。你能告诉我出了什么问题吗?

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 内部还是外部? AWS 内存缓存也一样?
  • lambda 和 memcache 都在同一个 VPC 上。
  • 那很好。然后你能提供你的安全组规则吗?
  • 目前我使用的是默认角色,允许所有流量。
  • memcache 和 lambda 都使用同一个组?还能截图规则吗?

标签: node.js amazon-web-services aws-lambda memcached elastic-cache


【解决方案1】:
  1. 通过查看此文档以指导您 https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html 和 CloudFormation 堆栈中的 Memcached URL 作为输出,确保您打开 VPC 的 DNS 主机名,以便您能够将其作为环境变量添加到您的 lambda
  2. “连接”您的 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
  1. 为您正在使用的语言/框架安装 Memaced 库,并使用已传递的 env 变量进行连接

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 2018-04-03
    • 1970-01-01
    • 2017-02-08
    相关资源
    最近更新 更多