
Serverless Framework 및 AWS Lambda with Python 환경에 pip 설치
2022-10-06 last update
5 minutes reading 람다 파이썬 ServerlessFramework AWSpip 설치가 필요한 AWS Lambda Python 스크립트를 Serverless Framework에 배포하는 방법입니다.
gem 설치가 필요한 AWS Lambda의 Ruby 스크립트는 지난번 문서에서 썼습니다.
절차 개요
나머지는 보통 배포하면 마음대로 여러가지 해준다
절차 상세
(Ruby 버전) Serverless Framework 및 AWS Lambda with Ruby 환경에 gem 설치
gem 설치가 필요한 AWS Lambda의 Ruby 스크립트는 지난번 문서에서 썼습니다.
절차 개요
플러그인을 넣으면 쉽게 할 수 있습니다.
serverless plugin install -n serverless-python-requirements
requirements.txt
생성 절차 상세
Serverless Framework 서비스 만들기
$ serverless create --template aws-python3
Serverless: Generating boilerplate...
_______ __
| _ .-----.----.--.--.-----.----| .-----.-----.-----.
| |___| -__| _| | | -__| _| | -__|__ --|__ --|
|____ |_____|__| \___/|_____|__| |__|_____|_____|_____|
| | | The Serverless Application Framework
| | serverless.com, v2.16.1
-------'
Serverless: Successfully generated boilerplate for template: "aws-python3"
Serverless: NOTE: Please update the "service" property in serverless.yml with your service name
세 개의 파일이 생성됩니다.
.gitignore
handler.py
serverless.yml
플러그인 설치
serverless-python-requirements
라는 플러그인을 설치합니다.
$ serverless plugin install -n serverless-python-requirements
다음 파일과 디렉토리가 증가합니다.
node_modules
package.json
package-lock.json
Serverless Framework가 Node.js로 구현되어 있기 때문에 파이썬 프로젝트이지만 node_modules
와 package.json
가 존재하는 것 같습니다.
소스 코드
serverless.yml
serverless.yml
는 다음과 같이 합니다. plugins
의 기술은 플러그인을 설치하면 마음대로 추기되어 있습니다.
serverless.yml
service: sample
frameworkVersion: '2'
provider:
name: aws
runtime: python3.8
region: ap-northeast-1
functions:
hello:
handler: handler.hello
plugins:
- serverless-python-requirements
requirements.txt
샘플 라이브러리로 jpholiday
라는 라이브러리를 사용해 보겠습니다. 일본의 공휴일을 판정하는 라이브러리입니다.
requirements.txt
를 작성해, 이하의 내용으로 합니다. 라이브러리 이름 한 줄만의 파일입니다.
jpholiday
jpholiday
라는 라이브러리를 샘플로 사용해 보겠습니다. 일본의 공휴일을 판정하는 라이브러리입니다.
파이썬 소스 코드
handler.py
import datetime
import jpholiday
def hello(event, context):
holidayName = jpholiday.is_holiday_name(datetime.date(2021, 8, 8))
print(holidayName) # CloudWatch に "山の日" と書き出される
배포
지금까지 만든 다음 serverless
명령으로 배포하면 Lambda 본체뿐만 아니라 serverless
명령이 라이브러리를 자동으로 설치한 이미지를 생성하여 AWS Lambda의 Layer로 업로드합니다.
$ serverless deploy -v
실행
배포 후 AWS Management Console에서 Lambda를 보면 다음과 같이 보입니다.

Ruby와 달리 Layer가 아닌 Lambda에 라이브러리가 직접 저장되는 것 같습니다.
2021/01/29 추가serverless.yml
에 설정을 추가하면 Layer가 되었습니다. 다음 기사에 썼습니다.
AWS Lambda + Python + Serverless Framework의 Layer에 pip 설치
링크
$ serverless create --template aws-python3
Serverless: Generating boilerplate...
_______ __
| _ .-----.----.--.--.-----.----| .-----.-----.-----.
| |___| -__| _| | | -__| _| | -__|__ --|__ --|
|____ |_____|__| \___/|_____|__| |__|_____|_____|_____|
| | | The Serverless Application Framework
| | serverless.com, v2.16.1
-------'
Serverless: Successfully generated boilerplate for template: "aws-python3"
Serverless: NOTE: Please update the "service" property in serverless.yml with your service name
.gitignore
handler.py
serverless.yml
$ serverless plugin install -n serverless-python-requirements
node_modules
package.json
package-lock.json
service: sample
frameworkVersion: '2'
provider:
name: aws
runtime: python3.8
region: ap-northeast-1
functions:
hello:
handler: handler.hello
plugins:
- serverless-python-requirements
jpholiday
import datetime
import jpholiday
def hello(event, context):
holidayName = jpholiday.is_holiday_name(datetime.date(2021, 8, 8))
print(holidayName) # CloudWatch に "山の日" と書き出される
$ serverless deploy -v