Serverless Framework에서 Webpack multiple entry를 설정하는 방법
Overview
섹션 제목: “Overview”Serverless Framework로 여러 AWS Lambda 함수를 배포할 때는 함수별로 번들 파일을 따로 만들고, 각 함수 handler가 해당 번들을 가리키도록 설정할 수 있다.
How to
섹션 제목: “How to”1) webpack.config.js 설정
섹션 제목: “1) webpack.config.js 설정”entry에 함수별 진입점을 나열하고, output.filename을 [name]-bundle.js처럼 지정한다.
module.exports = (options, webpack) => { return { ...options, entry: { convertImage: './src/lambda/convert-image.ts', backupS3: './src/lambda/backup-s3.ts', statisticsUsers: './src/statistics/statistics-users.ts', }, externals: [], output: { ...options.output, clean: true, libraryTarget: 'commonjs2', filename: '[name]-bundle.js', }, plugins: [ ...options.plugins, new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1, }), ], };};entry- 함수별로 번들링할 소스 파일을 지정한다.
output.filename- entry key를 파일명으로 사용한다.
- 예:
convertImage→convertImage-bundle.js - 기본값을 쓰기보다 여러 entry에 맞게 직접 지정하는 편이 좋다.
- 예:
- entry key를 파일명으로 사용한다.
LimitChunkCountPlugin- 번들 파일이 여러 개로 나뉘지 않도록 chunk 수를 제한한다.
2) NestJS build 확인
섹션 제목: “2) NestJS build 확인”Webpack 설정이 맞는지 먼저 Nest build로 확인한다.
nest build --webpack --webpackPath src/lambda/webpack.config.js번들 결과물은 dist 디렉토리에서 확인할 수 있다.
3) Serverless Framework 배포 설정
섹션 제목: “3) Serverless Framework 배포 설정”각 Lambda 함수의 handler가 생성된 번들을 가리키도록 설정한다.
functions: handleConvertImage: handler: dist/convertImage-bundle.handler package: individually: true include: - dist/convertImage-bundle.js exclude: - '**' timeout: 900 memorySize: 1024
handleBackupS3: handler: dist/backupS3-bundle.backupUserHandler package: individually: true include: - dist/backupS3-bundle.js exclude: - '**' timeout: 900 memorySize: 1024
handleStatisticsUsers: handler: dist/statisticsUsers-bundle.handler package: individually: true include: - dist/statisticsUsers-bundle.js exclude: - '**' timeout: 900 memorySize: 4096handler- 번들 결과물 안에서 실행할 함수를 가리킨다.
package.individually- 함수별로 개별 패키징한다.
include- 해당 함수에 필요한 번들 파일만 포함한다.
excludeinclude로 명시한 파일만 남기고 다른 파일은 제외한다.
4) 배포 명령 연결
섹션 제목: “4) 배포 명령 연결”nest build와 sls deploy를 한 번에 실행하도록 package.json 스크립트를 연결한다.
{ "scripts": { "build": "nest build", "deploy:lambda": "nest build --webpack --webpackPath src/lambda/webpack.config.js && sls deploy -c serverless.yaml" }}Notes
섹션 제목: “Notes”1) handler 이름과 파일명
섹션 제목: “1) handler 이름과 파일명”entrykey는 번들 파일명을 만들 때 사용한다.handler는 그 번들 안에서 실행할 export 함수를 가리킨다.entry는 여러 개일 때 Object 형태(entry: { key: './src/~' })로, 하나일 때는 String 형태(entry: './src/~')로 지정할 수 있다.- 번들 파일명 규칙이 바뀌면 Serverless 설정도 같이 바꿔야 한다.
- 번들 파일명은
.을 이용하여 지정하지 않는다.[name].bundle.js과 같이 지정하면 AWS Lambda 가 실행될 때Runtime.ImportModuleError가 발생한다.
2) chunk 분리 확인
섹션 제목: “2) chunk 분리 확인”LimitChunkCountPlugin을 써도 설정에 따라 chunk가 분리될 수 있다.[name]-bundle.js, 이외에 파일명에 숫자가 붙은 파일들이 생성되는 버그가 있었다.- 예) 1-bundle.js, 2-bundle.js, 10-bundle.js, 11-bundle.js 등
- 이 분리된 bundle 파일들도 아래와 같이 모든 Lambda 함수의
include목록에 추가로 넣어야 한다.- 파일명 길이에 따라
?-bundle.js또는??-bundle.js처럼 와일드카드를 더 길게 써야 할 수 있다.
- 파일명 길이에 따라
-
...functions:handleConvertImage:handler: dist/convertImage-bundle.handlerpackage:individually: trueinclude:- dist/convertImage-bundle.js- dist/?-bundle.js- dist/??-bundle.js...include:- dist/backupS3-bundle.js- dist/?-bundle.js- dist/??-bundle.js...include:- dist/statisticsUsers-bundle.js- dist/?-bundle.js- dist/??-bundle.js