如何从 AWS Lambda 函数中排除文件?

How can I exclude files from AWS Lambda function?

我已经使用 CDK 创建了一个 lambda 函数,我正在使用 Code.fromAsset@aws-cdk/aws-lambda 获取代码。一切正常,但我想知道是否有办法排除添加 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 文件?

谢谢!

我同意之前的评论,除非您达到 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)
        });