본문 바로가기
  • 콩's 코딩노트
백엔드

Lambda 에 라이브러리 사용할 때 Layer로 만들어 사용하기 (1)

by Cong_S 2022. 6. 29.

1. PuTTY로  EC2 서버에 접속

2. 파이썬이 없으면 아나콘다 설치(링크 주소 복사해서 wget 으로 다운)

https://www.anaconda.com/products/distribution

 

Anaconda | Anaconda Distribution

Anaconda's open-source Distribution is the easiest way to perform Python/R data science and machine learning on a single machine.

www.anaconda.com

 

3. Lambda의 환경이 파이썬 3.8 이므로 가상환경도 파이썬 3.8로 맞춰준다.

$ conda create -n lambda python=3.8

 

 

4. 가상환경으로 이동

$ conda activate lambda

 

 

5. 디렉토리 생성, 이름은 python

$ mkdir python

 

 

6. python 디렉토리 이동

$ cd python

 

 

7. 판다스 라이브러리를 이 디렉토리에 설치 (판다스의 경우에만 12번까지 진행)

# 판다스
$ pip install -t . pandas

# 다른 일반 라이브러리는 바로 13번
$ pip install -t . mysql-connector-python flask-restful psycopg2-binary passlib email-validator Flask-JWT-Extended requests

 

 

8. 필요없는 파일 삭제

$ rm -rf *.dist-info __pycache__ pandas numpy

 

9. 아마존의 리눅스에 설치될 판다스와 넘파이 파일을 다운로드 받는다.

https://pypi.org/project/pandas/#files
https://pypi.org/project/numpy/#files

 

pandas

Powerful data structures for data analysis, time series, and statistics

pypi.org

 

numpy

NumPy is the fundamental package for array computing with Python.

pypi.org

 

10. 파이썬 3.8 이므로 Downliad Files 에서

Ctrl + F 로 38 검색해서 manylinux.... x86_64.whl 파일을 링크 주소복사해

EC2에서 wget 으로 파일을 다운받는다. 

$ wget https://files.pythonhosted.org/packages/d1/55/18b00a5426ad8a89944ab93b6b29773a556dc06af8b53a29031f861009e3/pandas-1.4.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

 

11. 다운로드 받은 파일 2개 파일명을 변경한다. 

$ mv ...#넘파이 파일명#... 한칸 띄고 numpy.zip

$ mv ...#판다스 파일명#... 한칸 띄고 pandas.zip

 

12. 변경된 파일의 압축을 푼다.

$ unzip numpy.zip

$ unzip pandas.zip

 

13. 필요없는 파일 삭제

$ rm -rf *.zip *.dist-info

 

 

14. 상위 경로로 이동한다.

$ cd ..

 

 

15. python 폴더를 압축한다.

$ zip -r pandas_layer.zip python

 

16. PC의 웹브라우저로 AWS 콘솔에 접속 (리전 확인 꼭 할 것!!!!!)

https://ap-northeast-2.console.aws.amazon.com/

 

https://ap-northeast-2.console.aws.amazon.com/console/home

 

ap-northeast-2.console.aws.amazon.com

 

17. S3에서 알아보기 쉽게 "자신의 아이디-lambda-layers" 라는 버킷을 만든다.

건드릴 것은 없다.

 

18. 버킷이 만들어지면, 해당 버킷에서 먼저 만들어진 pandas_layers.zip 파일을 업로드한다.

 

19. 업로드하기 위해 FTP를 이용해 EC2로부터 다운받아야한다. (파일질라 이용 : https://filezilla-project.org/)

 

FileZilla - The free FTP solution

Overview Welcome to the homepage of FileZilla®, the free FTP solution. The FileZilla Client not only supports FTP, but also FTP over TLS (FTPS) and SFTP. It is open source software distributed free of charge under the terms of the GNU General Public Licen

filezilla-project.org

 

 

20. 17에서 만든 버킷에 pandas_layers.zip 을 업로드한다.

 

21. 업로드가 완료되면, 파일명을 눌러, 속성 부분의 '객체 URL'을 링크 주소 복사한다.

 

22. AWS 콘솔에서 Lambda 로 이동.

 

23. 왼쪽의 계층 메뉴 선택 

 

26.  계층 이름 (보통 알아보기 쉽게 layer이름 적음) 적고, S3에서 복사한 링크 넣고

x86_64, python 3.8로 계층 생성

댓글