【问题标题】:How can I exclude files from AWS Lambda function?如何从 AWS Lambda 函数中排除文件?
【发布时间】:2023-03-06 17:15:01
【问题描述】:

我已经使用 CDK 创建了一个 lambda 函数,并且我正在使用来自 @aws-cdk/aws-lambdaCode.fromAsset 获取代码。一切正常,但我想知道是否有办法排除 TypeScript 文件被添加,而只添加编译后的 JavaScript。看起来您可以传递 excludes 参数,但现在似乎已弃用。

这是我的堆栈当前的样子:

LambdaFunctionStack.ts

import { Rule, Schedule } from '@aws-cdk/aws-events';
import { LambdaFunction } from '@aws-cdk/aws-events-targets';
import { Function as AwsFunction, Runtime, Code } from '@aws-cdk/aws-lambda';
import { Stack, Construct, Duration, StackProps } from '@aws-cdk/core';

interface FunctionStackProps extends StackProps {
  functionName: string;
}

export class FunctionStack extends Stack {
  constructor(scope: Construct, id: string, props: FunctionStackProps) {
    super(scope, id, props);

    this.lambdaFunction = new AwsFunction(this, props.functionName, {
      functionName: props.functionName,
      handler: 'src/index.handler',
      runtime: Runtime.NODEJS_12_X,
      code: Code.fromAsset('./app', { exclude: ['*.ts'] }),
      memorySize: 512,
      timeout: Duration.seconds(30),
    });

    const rule: Rule = new Rule(this, 'Rule', {
      schedule: Schedule.rate(Duration.hours(2))
    });

    rule.addTarget(new LambdaFunction(this.lambdaFunction));
  }
}

但不幸的是,这不起作用。

有没有办法让我轻松排除 TypeScript 文件?

谢谢!

【问题讨论】:

  • 您使用哪个版本的 aws-sdk ?我想我可能与github.com/aws/aws-cdk/issues/7669 有关
  • 您可以将要添加的所有文件移动到另一个文件夹并添加新文件夹。我认为这不值得,除非 lambda 大小实际上对您来说是个问题。 KISS 是你的朋友
  • @Traycho 我正在使用 cdk 的 1.58.0 版本,显然这在 1.38.0 中已修复,这很奇怪。谢谢!
  • @Jonny lambda 大小不是问题,我只是想尽可能地整理它,但我同意这不值得。谢谢!

标签: typescript aws-lambda aws-cdk


【解决方案1】:

我同意之前的 cmets,除非您使用 lambda 函数大小达到某种限制,否则不值得付出努力。

话虽如此,下面是我用于上传单个 python 脚本的示例。它排除了custom_resources 目录中的所有内容,除了create_deployment_group.py

        // Custom resource to create the deployment group
        const createDeploymentGroupLambda = new lambda.Function(this, 'createDeploymentGroupLambda', {
            code: lambda.Code.fromAsset(
                path.join(__dirname, 'custom_resources'),
                {
                    exclude: ["**", "!create_deployment_group.py"]
                }),
            runtime: lambda.Runtime.PYTHON_3_8,
            handler: 'create_deployment_group.handler',
            role: customLambdaServiceRole,
            description: "Custom resource to create deployment group",
            memorySize: 128,
            timeout: cdk.Duration.seconds(60)
        });

【讨论】:

  • 感谢您的回答!我同意,lambda 真的很小,所以除了试图整理它之外没有理由这样做。看起来不错,但我仍然很难理解为什么我的不工作,因为它们非常相似。您使用的是什么版本的 CDK?
  • 我使用的是 1.54 版本。是否有任何构建错误?
  • 嗯,那我试试那个版本。不,似乎认为一切都很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 2021-11-12
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
相关资源
最近更新 更多