简体   繁体   English

独立部署 AWS Lambda 与在应用程序堆栈中部署的不同行为

[英]Different Behavior Deploying AWS Lambda Standalone vs within an Application Stack

Hi everybody and thanks for taking time to look at my issue/question.大家好,感谢您花时间查看我的问题。

I am getting different results when deploying my AWS Lambda stand-alone versus within an Application Stack.独立部署 AWS Lambda 与在应用程序堆栈中部署时,我得到不同的结果。

I'm trying to connect to AWS Elasticache Redis from within my Lambda. I have.Net Core 3.1 Lambdas (using StackExchange.Redis) which can connect.我正在尝试从我的 Lambda 连接到 AWS Elasticache Redis。我有可以连接的 .Net Core 3.1 Lambdas(使用 StackExchange.Redis)。 But I also need to be able to connect from my Node.js Lambdas.但我还需要能够从我的 Node.js Lambda 进行连接。

For the Node.js Lambdas, I'm using "node-redis" and "async-redis".对于 Node.js Lambda,我使用的是“node-redis”和“async-redis”。 I have two Lambdas which are essentially identical except that one is deployed in an Application Stack and the other is deployed as a stand-alone Lambda. Both Lambdas reference the same Lambda Layer (ie same "node_modules"), have the same VPC settings, the same Execution Role, and essentially the same code.我有两个基本上相同的 Lambda,除了一个部署在应用程序堆栈中,另一个部署为独立的 Lambda。两个 Lambda 引用相同的 Lambda 层(即相同的“node_modules”),具有相同的 VPC 设置,相同的执行角色,本质上是相同的代码。 So they've pushed it up to another group.所以他们把它推给了另一个小组。

The stand-alone Lambda connects to Redis without issue.独立的 Lambda 连接到 Redis 没有问题。 The Application Stack Lambda does not and exits processing before completing but without raising any error.应用程序堆栈 Lambda 没有并在完成之前退出处理,但没有引发任何错误。

At first I thought I might just need to configure my Application Stack but I cannot find any information indicating we even can configure Application Stacks.起初我以为我可能只需要配置我的应用程序堆栈,但我找不到任何信息表明我们甚至可以配置应用程序堆栈。 So I'm at a loss.所以我很茫然。

The stand-alone Lambda:单机Lambda:

 exports.handler = async (event) => {
        const asyncRedis = require("async-redis");
        const redisOptions = 
        {
            host: "XXXXXXXXX.XXXXX.XXXX.use2.cache.amazonaws.com",
            port: 6379
        }
    
        console.log('A');
        const client = asyncRedis.createClient(redisOptions);
        console.log(client);
    
        console.log('B');
        const value = await client.get("Key");
    
        console.log('C');
        console.log(value);
    
        console.log('D');
        console.log(client);
    };

The output of this function is essentially:这个 function 的 output 本质上是:

A
{RedisClient} --> the "client" object --> Shows connected = false
B
C
{ Correct Data From Redis }
D
{RedisClient} --> the "client" object --> Shows connected = true

The Application Stack Lambda:应用程序堆栈 Lambda:

async function testRedis2(event, context) {
    console.log('In TestRedis2');
    const asyncRedis = require("async-redis");
    const redisOptions = 
    {
        host: "XXXXXXXXX.XXXXX.XXXX.use2.cache.amazonaws.com",
        port: 6379
    }
        console.log('A');
        const client = asyncRedis.createClient(redisOptions);
        console.log(client);
    
        console.log('B');
        var value = await client.get("Key");

        console.log('C');
        console.log(value);
        
        console.log('D');
        console.log(client);
    }

module.exports = {
    testRedis2
};

The output of this function is essentially:这个 function 的 output 本质上是:

In TestRedis2
A
{RedisClient} --> the "client" object --> Shows connected = false
B

I don't understand why these don't perform identically.我不明白为什么它们的表现不一样。 And I don't get why I don't see further entries in the output.我不明白为什么我在 output 中看不到更多条目。

Has anyone else experienced issues connecting to VPC resources from within an Application Stack?其他人是否遇到过从应用程序堆栈连接到 VPC 资源的问题?

Thanks谢谢

I stumbled across the answer through extensive trial and error.我通过大量的反复试验偶然发现了答案。 It may be obvious to Node.js developers but, just in case another Javascript/Node newbie has the same issue, I'll post the answer here.对于 Node.js 开发人员来说,这可能是显而易见的,但为了防止其他 Javascript/Node 新手遇到同样的问题,我将在此处发布答案。

The import/require and creation of the client must be at the top of the module.客户端的导入/请求和创建必须在模块的顶部。 Not in the function itself.不在 function 本身。

So, the following does work in my application stack:因此,以下内容确实适用于我的应用程序堆栈:

const asyncRedis = require("async-redis");

const redisOptions = {
    host: "XXXXXXXXX.XXXXX.XXXX.use2.cache.amazonaws.com",
    port: 6379
};

const client = asyncRedis.createClient(redisOptions);

async function redisGet(key: string){
  // console.log('In redisGet');
  return  await client.get(key);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从不同的账户部署 AWS Lambda - Deploying an AWS Lambda from a different account 将 python 应用程序部署为带有子文件夹的 aws lambda 中的图像时缺少父目录 - MissingParentDirectory when Deploying python application as an image in aws lambda with subfolders 使用 Rest API 和 Lambda Function 集成部署 CDK 堆栈时出错 (AWS CDK 2 Python API) - Error deploying a CDK stack with a Rest API with a Lambda Function Integration (AWS CDK 2 Python API) 将 Go 项目部署到 AWS Lambda 时出现“PathError” - "PathError" when deploying Go project to AWS Lambda AWS Lambda 应用层 - AWS Lambda Application Layer 通过无服务器框架部署到 AWS Lambda 时,将 Package 文件放入应用程序包的特定文件夹 - Package files into specific folder of application bundle when deploying to AWS Lambda via Serverless Framework 在 AWS CodeBuild 中运行 Terraform - 部署 lambda - Running Terraform in AWS CodeBuild - deploying lambda 在 AWS 中部署 Python Flask Web 服务 Lambda - Deploying Python Flask Web service in AWS Lambda 使用无服务器框架将 Python package 部署到 AWS lambda 时出错 - Error deploying Python package to AWS lambda using Serverless framework 使用 AWS CDK python 部署 node.js lambda - Deploying node.js lambda with AWS CDK python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM