如何将其他文件与“NodejsFunction”捆绑在一起?

Une*_*loy 7 node.js typescript aws-lambda aws-cdk

我想将额外的 Html 文件上传到代码源,如下所示。 在此输入图像描述

这是我的代码:

const mailerFunction = new aws_lambda_nodejs.NodejsFunction(this, 'ApiNotificationHandler', {
    runtime: lambda.Runtime.NODEJS_14_X,
    memorySize: 1024,
    timeout: cdk.Duration.seconds(3),
    handler: 'main',
    entry: path.join(__dirname, '../../src/mailer/index.ts'),
    environment: {
        SES_REGION,
        SES_EMAIL_FROM,
        SES_EMAIL_TO,
    }
});
Run Code Online (Sandbox Code Playgroud)

我使用的是CDK 2.58.1版本。如何使用 cdk lambda 将额外的 html 文件上传到代码源?

fed*_*nev 7

通过在prop 中.html定义commandHook来复制文件bundling

new NodejsFunction(this, "ApiNotificationHandler", {
  bundling: {
    commandHooks: {
      afterBundling: (inputDir: string, outputDir: string): string[] => [
        `cp ${inputDir}/path/from/root/to/email-template.html ${outputDir}`,
      ],
      beforeBundling: (inputDir: string, outputDir: string): string[] => [],
      beforeInstall: (inputDir: string, outputDir: string): string[] => [],
    },
  },
  // ...
});
Run Code Online (Sandbox Code Playgroud)

该接口需要定义所有三个钩子。选择其中之一来实现复制。返回一个空数组作为其他两个数组的无操作。 inputDir将是项目根目录。


Exc*_*ion 2

您可以尝试使用命令 hooks。在您的示例中,它可能看起来像这样(调整命令inputDir):

const mailerFunction = new aws_lambda_nodejs.NodejsFunction(this, 'ApiNotificationHandler', {
    bundling: {
      commandHooks: {
        beforeBundling(inputDir: string, outputDir: string): string[] {
          return [`cp -r ${inputDir} ${outputDir}`] //adjust here
        },
        afterBundling(inputDir: string, outputDir: string): string[] {
          return []
        },
        beforeInstall(inputDir: string, outputDir: string): string[] {
          return []
        },
      },
    },
    runtime: lambda.Runtime.NODEJS_14_X,
    memorySize: 1024,
    timeout: cdk.Duration.seconds(3),
    handler: 'main',
    entry: path.join(__dirname, '../../src/mailer/index.ts'),
    environment: {
        SES_REGION,
        SES_EMAIL_FROM,
        SES_EMAIL_TO,
    }
});
Run Code Online (Sandbox Code Playgroud)