带有依赖项的AWS Lambda打包 [英] AWS Lambda packaging with dependencies

查看:97
本文介绍了带有依赖项的AWS Lambda打包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进一步概述是在NodeJS和Monorepo(基于Lerna)的背景下进行的.

Further outlining is in the context of NodeJS and Monorepo (based on Lerna).

我有一个AWS堆栈,其中通过AWS CloudFormation部署了多个AWS Lambda.一些lambda很简单(单个小模块),可以内联:

I have AWS stack with several AWS Lambda inside deployed by means of AWS CloudFormation. Some of the lambdas are simple (the single small module) and could be inlined:

有些没有依赖项,并且打包如下:

Some have no dependencies and packaged as follows:

const someLambda = new Function(this, 'some-lambda', {
  code: Code.fromAsset(<relative path to folder with lambda>),
  handler: 'index.handler',
  runtime: Runtime.NODEJS_12_X
});

但是,据我所知,在具有依赖项的lambda相对较大的情况下,我们唯一的打包方式(由API提出)是 @ aws-cdk/aws-lambda-nodejs :

But in case of relatively huge lambdas with dependencies, as I understand, we only way to package (proposed by API) is @aws-cdk/aws-lambda-nodejs:

import * as lambdaNJS from "@aws-cdk/aws-lambda-nodejs";

export function createNodeJSFunction(
  scope: cdk.Construct, id: string, nodejsFunctionProps: Partial<NodejsFunctionProps>
) {
  const params: NodejsFunctionProps = Object.assign({
    parcelEnvironment: { NODE_ENV: 'production' },
  }, nodejsFunctionProps);

  return new lambdaNJS.NodejsFunction(scope, id, params);
}

对于独立的软件包,它可以很好地工作,但是在monorepo的情况下,它仅挂在堆栈的 synth 上.我只是在寻找替代方法,因为我认为捆绑( parcel )BE来源不是一个好主意.

For standalone packages, it works well, but in case of the monorepo it just hangs on synth of the stack. I just looking for alternatives, cause I believe it is not a good idea to bundle (parcel) BE sources.

推荐答案

我已经创建了以下原始库,用于 zip 仅需 node_modules ,尽管软件包是 hoisting.

I've created the following primitive library to zip only required node_modules despite packages hoisting.

https://github.com/redneckz/slice-node-modules

用法(从monorepo根开始):

Usage (from monorepo root):

$ npx @redneckz/slice-node-modules \
  -e packages/some-lambda/lib/index.js \
  --exclude 'aws-*' \
  --zip some-lambda.zip

-排除"aws-*" -默认情况下包含AWS运行时,因此无需打包.

--exclude 'aws-*' - AWS runtime is included by default, so no need to package it.

这篇关于带有依赖项的AWS Lambda打包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆