AWS lamda?
AWS lambda는 서버리스 컴퓨팅 서비스로, 개발자가 서버 관리 없이 코드를 실행할 수 있게 해준다. 사용자는 코드를 업로드하기만 하면, lambda가 자동으로 필요한 컴퓨팅 인프라를 할당하고 관리하기에 매우 편리하다. 이 서비스는 코드 실행에 필요한 시간에 따라 요금이 부과되므로, 사용한 만큼만 비용을 지불하게 장점 또한 가지고 있다.
AWS Lambda is a serverless computing service that enables developers to run code without managing servers. Users simply upload their code, and AWS Lambda automatically allocates and manages the computing infrastructure required. This service charges based on the actual time your code takes to execute, meaning you pay only for what you use.
lambda Layer?
Lambda 계층 작업 - AWS Lambda
이 페이지에 작업이 필요하다는 점을 알려 주셔서 감사합니다. 실망시켜 드려 죄송합니다. 잠깐 시간을 내어 설명서를 향상시킬 수 있는 방법에 대해 말씀해 주십시오.
docs.aws.amazon.com
AWS Lambda에서 Layer(계층)는 Lambda 함수의 코드와 함께 실행되어야 하는 라이브러리, 사용자 지정 런타임, 또는 다른 종속성을 분리하여 관리할 수 있게 해주는 기능이다. Layer를 사용함으로써, 개발자는 공통의 코드와 라이브러리를 여러 Lambda 함수 간에 재사용할 수 있게 되어 코드 관리가 용이해지고, 빌드 패키지의 크기를 줄일 수 있다.
In AWS Lambda, Layer is a feature that allows to separate and manage libraries, custom runtimes, or other dependencies that need to run alongside the code in a Lambda function. By using layers, developers can reuse common code and libraries across multiple Lambda functions, making code management easier and reducing the size of build packages.
개발자 입장에서는 lambda 함수에서 실행하는 코드와 실행을 위한 라이브러리 종속성을 따로 관리하기 때문에 배포의 용이성을 높일 수 있다. 위의 사진처럼 기본적으로 제공되는 Layer도 충분히 활용할 수 있지만, 다른 여러 python 라이브러리를 활용하기 위해서는 따로 로컬 환경에서 ZIP 아카이브 형태 만들어서 업로드를 해야한다.
From a developer's point of view, it is easier to deploy because the code executed by the lambda function and the library dependencies for execution are managed separately. As shown in the photo above, you can fully utilize the built-in Layer, but if you want to utilize other python libraries, you need to create and upload them as ZIP archives from your local environment.
User custom Layer(for python pip)
Lambda 런타임 - AWS Lambda
Lambda는 때때로 해당 런타임이 지원하는 언어 버전의 지원 종료일 이후 제한된 기간 동안 Lambda 런타임의 지원 중단을 지연합니다. 이 기간 동안 Lambda는 런타임 OS에만 보안 패치를 적용합니다. Lam
docs.aws.amazon.com
AWS lambda는 Amazon linux 기반으로 운영되기 때문에 이에 맞춰진 linux기반의 python 라이브러리를 ZIP 아카이브 해야한다. 외부 라이브러리 중에는 특히, C/C++로 컴파일 되어 바이너리 형태로 제공되는 것들이 있기 때문에 운영체제와 아키텍쳐를 고려한 라이브러리 아카이빙이 필요하다.
AWS lambda runs on Amazon linux, so we need to ZIP archive the linux-based python libraries that are customized for it.
Some external libraries, especially those compiled in C/C++, are provided in binary form, so it is necessary to archive libraries considering the operating system and architecture.
(Optional) Virtual environment creation
$ python3 -m venv venv # please set your own python version
$ venv/bin/pip install --upgrade pip
$ . venv/bin/activate
현재 사용하고 있는 python 환경과 별개로 구성하고 싶다면 새로운 python 가상환경을 설정하자.
If you want to configure something separate from your current python environment, you can set up a new python virtual environment.
Install dependencies to target folder
$ pip install \
--platform manylinux2010_x86_64 \
--implementation cp \
--python {your desired python version} \
--only-binary=:all: --upgrade \
--target {path to your target folder} \
{your desired libraries}{or just use requirements"-r requirements.txt"}
linux기반의 운영체제에 맞게 설치해주자.
Let's install it for linux-based operating systems.
- platform: based OS (Linux)
- implementation: cp (C Python)
- python: version of python
- only-binary=:all: : Complied binary file only
- target: target path
ZIP it up
python은 아래와 같이 python이라는 폴더 아래에 종속성 파일들이 위치한 상태에서 zip 파일로 만들어줘야 Layer설정에서 인식할 수 있다.
For python, you need to zip the dependency files under the folder named python as shown below so that it can be recognized by Layer settings.
pillow.zip
│ python/PIL
└ python/Pillow-5.3.0.dist-info
아래의 링크를 통해서 node.js, java, ruby 등의 다른 언어로 구현된 Layer를 구성할 수 있다.
You can configure Layer implementations in other languages such as node.js, java, ruby, and others through the links below.
계층 콘텐츠 패키징 - AWS Lambda
이 페이지에 작업이 필요하다는 점을 알려 주셔서 감사합니다. 실망시켜 드려 죄송합니다. 잠깐 시간을 내어 설명서를 향상시킬 수 있는 방법에 대해 말씀해 주십시오.
docs.aws.amazon.com
Upload the layer
잘 정리한 파일을 lambda layer에 업로드하면 여러 function에서 바로 사용할 수 있다.
Once you have a well-organized file, you can upload it to the lambda layer and use it in multiple functions.
+ mysqlclient library layer
개인적으로 sqlalchemy를 활용하기 위해서 필요한 종속성인 mysqlclient가 제대로 import되지 않아서 골치 아팠는데 친절히도 해당 라이브러리에 대한 layer를 제작해둔 github가 있어서 공유한다.
Personally, I had a problem with mysqlclient not being imported properly, which is a necessary dependency to utilize sqlalchemy, but luckily there is a github that has created a layer for that library.
https://github.com/nonbeing/mysqlclient-python3-aws-lambda/tree/master
GitHub - nonbeing/mysqlclient-python3-aws-lambda: Use mysqlclient seamlessly with AWS Lambda Python3 functions.
Use mysqlclient seamlessly with AWS Lambda Python3 functions. - nonbeing/mysqlclient-python3-aws-lambda
github.com