○ AWS Lambda(람다) 외부 패키지 적용 방법
Lambda를 사용할 때 외부 패키지를 사용해야 하는 경우가 생길 수 있다.
예를 들어 DB 연동, axios의 경우 외부 패키지를 사용해야만 한다.
Node.js 기준으로 외부 패키지 적용 방법을 정리하고자 한다.
1. 패키지 zip 파일 생성
Linux & MacOS
mkdir nodejs
cd nodejs
npm i axios
rm -rf package-lock.json
cd ..
zip -r axios.zip nodejs
Windows
- cmd 창에서 zip 명령어를 사용하기 위해 zip 파일 설치 (windows는 zip 명령어가 기본적으로 지원하지 않음)
- 다운로드 받은 파일을 c:\windows\system32 경로로 복사한다.
http://stahlworks.com/dev/?tool=zipunzip
zip and unzip for the windows command line
How to get zip and unzip on the windows command line for quick creation and extraction of zip compressed files. Download the free Windows executables: (for Linux, see below) to extract zip files on the command line, download unzip.exe here. this file is a
stahlworks.com
mkdir nodejs
cd nodejs
npm i axios
del /f package-lock.json
cd ..
zip -r axios.zip nodejs
2. Lambda(람다) 계층 생성
- 계층 메뉴 -> 계층 생성 버튼 클릭
- 업로드 버튼 클릭
- 생성한 zip 파일 선택
- 생성 버튼 클릭
3. 계층 적용
1. 생성된 계층의 ARN을 복사한다.
2. 계층을 적용할 함수로 이동하려 Layers 항목을 클릭한다.
3. [Add a layer] 버튼 클릭
4. ARN 지정 선택 후 복사해 놓은 계층 ARN 값을 입력하여 확인 버튼 클릭 후 추가 버튼 클릭
4. 계층 사용
- Layers에 추가 되었는지 확인
- 코드 소스 상에 패키지를 import 하여 사용
Reference
https://stackoverflow.com/questions/48356841/how-can-i-use-axios-in-lambda - StackOverflow
https://lee-mandu.tistory.com/530 - Windows cmd zip 명렁어
'AWS' 카테고리의 다른 글
AWS - API Gateway JWT 인증(with Firebase auth) (0) | 2023.04.17 |
---|---|
AWS - RDS 생성, DBeaver 연결 방법 (0) | 2023.04.05 |