API Gateway, Lambda로 배포하기

API Gateway, Lambda로 배포하기

ML 모델을 서비스로 배포하려면 어떻게 하는게 좋을지 고민하던 중, AWS에 Amazon SageMaker를 이용하여 배포하기로 결정했다.

Developer Guide의 Get Started를 읽으며 따라해보았다.

ML inference를 서비스로 만드려면?

AWS Docs를 따라 진행해보겠다.

가상환경 생성

1
2
3
4
5
6
7
8
% python3 -m venv aws
% python3 --version
Python 3.12.3
% source ./aws/bin/activate

또는
$ python3 -m venv venv37
$ . venv37/bin/activate

관련 라이브러리 설치

1
2
3
4
% python3 -m pip install chalice
% chalice new-project gemma-inference-server

# 이후 app.py 수정

directory로 들어가보면 아래와같이 여러 파일들이 생성되어있다.

1
2
3
4
5
6
7
8
9
10
11
% ls -la
total 24
drwxr-xr-x 9 leehamin staff 288 Jun 15 16:55 .
drwxr-xr-x 63 leehamin staff 2016 Jun 15 16:50 ..
drwxr-xr-x 3 leehamin staff 96 Jun 15 16:50 .chalice
-rw-r--r-- 1 leehamin staff 37 Jun 15 16:50 .gitignore
drwxr-xr-x 9 leehamin staff 288 Jun 15 17:01 .idea
drwxr-xr-x 6 leehamin staff 192 Jun 15 16:51 .venv
drwxr-xr-x 3 leehamin staff 96 Jun 15 16:55 __pycache__
-rw-r--r-- 1 leehamin staff 1427 Jun 15 16:52 app.py
-rw-r--r-- 1 leehamin staff 31 Jun 15 16:54 requirements.txt

지금은 .chalice 디렉터리를 무시해도 된다. 우리가 집중할 두 가지 주요 파일은 app.py와 요구사항.txt입니다.

Chalice 로컬 환경 테스트

1
2
3
% chalice local

# localhost로 접근 시 기본 샘플 동작하는 것 확인

AWS CLI 설정

configure sso

1
2
3
4
5
6
7
8
9
10
11
12
aws configure sso

# SSO session name: (임의로 입력해도 되는 것으로 추정)
# SSO start URL: (Access Keys에 있는 값 복사)
# SSO Region: (Access Keys에 있는 값 복사)
# SSO registration scopes: (기본 값이 있는 것 같아 Enter 누르고 넘김)

# 이후 code를 입력하는 링크가 주어지는데, AWS 콘솔에 로그인 한 환경에서 해당 링크에 접속하여 해당 code 입력

To use this profile, specify the profile name using --profile, as shown:

aws s3 ls --profile [profile명]

Access key 입력

1
2
3
4
5
6
7
# Access keys에 있는 값 복사하여 입력 (어느정도 시간이 지나면 키나 토큰값이 달라지는 것 같으므로 주의)
export AWS_ACCESS_KEY_ID=""
export AWS_SECRET_ACCESS_KEY=""
export AWS_SESSION_TOKEN=""

# 가이드엔 없는데, deploy시 에러 발생하여 추가로 실행
export AWS_DEFAULT_REGION="ap-northeast-2"

배포

1
chalice deploy
Author

hamin

Posted on

2024-06-13

Updated on

2024-06-22

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.
You need to set client_id and slot_id to show this AD unit. Please set it in _config.yml.