AWS Lambda (Layer)에서 regex._regex ImportError

kimeasyn·2025년 10월 16일

최근에 aws Lambda함수를 만들면서(3.13버젼)
Lambda Layer에 transformers를 올리면서 이런 에러가 발생했다.

"errorMessage": "Unable to import module 'lambda_function': No module named 'regex._regex'"

처음엔 단순히 패키지 누락인가 싶었는데, 알고 보니 원인은 플랫폼 차이였다.
로컬(macOS)에서 설치한 regex 패키지는 mac 전용 wheel이라
AWS Lambda(amazon 리눅스 기반)에서 그대로 쓸 수 없었던 것.

문제 원인 요약

Lambda의 실행 환경은 Amazon Linux 2 (x86_64) 기반이다.
즉, macOS나 Windows에서 설치한 패키지를 그대로 올리면
C 확장 모듈(.so)이 호환되지 않아 ImportError가 난다.

해결 방법

amazon linux 기반 Docker를 쓰는 대신, pip에서 제공하는 --platform 옵션을 활용하면
로컬에서도 Lambda용(리눅스용) wheel을 직접 받을 수 있다.

아래 명령어 한 줄로 해결된다 👇

pip install regex transformers \
  --platform=manylinux2014_x86_64 \
  --only-binary=:all: \
  --implementation cp \
  --python-version 313 \
  --target ./python

--platform=manylinux2014_x86_64
→ AWS Lambda와 동일한 리눅스 플랫폼

--only-binary=:all:
→ wheel(바이너리) 파일만 설치, 소스 빌드는 제외

--python-version 313
→ Lambda 런타임 버전에 맞춰 설정 (예: python3.13)

--target ./python
→ 실제 Layer에 포함될 디렉토리 지정

설치가 끝나면 python/ 폴더를 zip으로 묶어서 Layer로 업로드하면 끝이다.

zip -r python.zip python

profile
들썩들썩 떠들썩

0개의 댓글