-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create new layer_nlp lambda layer
- Loading branch information
Showing
10 changed files
with
148 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#------------------------------------------------------------------------------ | ||
# written by: Lawrence McDaniel | ||
# https://lawrencemcdaniel.com/ | ||
# | ||
# date: sep-2023 | ||
# | ||
# usage: implement a Python Lambda layer with a virtual environment | ||
# that includes the packages required by OpenAI API and LangChain. | ||
#------------------------------------------------------------------------------ | ||
locals { | ||
nlp_layer_slug = "nlp" | ||
nlp_layer_name = "layer_${local.nlp_layer_slug}" | ||
nlp_layer_parent_directory = "${path.module}/python" | ||
nlp_layer_source_directory = "${local.nlp_layer_parent_directory}/${local.nlp_layer_name}" | ||
nlp_layer_packaging_script = "${local.nlp_layer_source_directory}/create_container.sh" | ||
nlp_layer_package_folder = local.nlp_layer_slug | ||
nlp_layer_dist_build_path = "${path.module}/build/" | ||
nlp_layer_dist_package_name = "${local.nlp_layer_name}_dst.zip" | ||
} | ||
|
||
############################################################################### | ||
# Python package | ||
# https://alek-cora-glez.medium.com/deploying-aws-lambda-function-with-terraform-custom-dependencies-7874407cd4fc | ||
############################################################################### | ||
resource "null_resource" "package_nlp_layer" { | ||
triggers = { | ||
redeployment = sha1(jsonencode([ | ||
"${path.module}/lambda_layer.tf", | ||
file("${local.nlp_layer_packaging_script}"), | ||
file("${local.nlp_layer_source_directory}/Dockerfile"), | ||
file("${local.nlp_layer_source_directory}/create_container.sh"), | ||
file("${local.nlp_layer_source_directory}/requirements.txt"), | ||
fileexists("${local.nlp_layer_source_directory}/${local.nlp_layer_dist_package_name}") ? filebase64("${local.nlp_layer_source_directory}/${local.nlp_layer_dist_package_name}") : null | ||
])) | ||
} | ||
|
||
provisioner "local-exec" { | ||
interpreter = ["/bin/bash"] | ||
command = local.nlp_layer_packaging_script | ||
|
||
environment = { | ||
SOURCE_CODE_PATH = local.nlp_layer_source_directory | ||
RUNTIME = var.lambda_python_runtime | ||
CONTAINER_NAME = local.nlp_layer_name | ||
PACKAGE_NAME = local.nlp_layer_dist_package_name | ||
} | ||
} | ||
} | ||
|
||
resource "aws_lambda_layer_version" "nlp" { | ||
filename = "${local.nlp_layer_source_directory}/${local.nlp_layer_dist_package_name}" | ||
source_code_hash = fileexists("${local.nlp_layer_source_directory}/${local.nlp_layer_dist_package_name}") ? filebase64sha256("${local.nlp_layer_source_directory}/${local.nlp_layer_dist_package_name}") : null | ||
layer_name = local.nlp_layer_slug | ||
compatible_architectures = var.compatible_architectures | ||
compatible_runtimes = [var.lambda_python_runtime] | ||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
depends_on = [ | ||
null_resource.package_nlp_layer | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.zip | ||
venv | ||
archive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Use an AWS Lambda Python runtime as the base image | ||
# https://hub.docker.com/r/amazon/aws-lambda-python | ||
# ------------------------------------------------------ | ||
|
||
# Stage 1: Build dependencies for x86_64 | ||
FROM --platform=linux/amd64 public.ecr.aws/lambda/python:3.11 | ||
|
||
WORKDIR /var/task | ||
|
||
COPY requirements.txt . | ||
|
||
ENV PYTHONPATH "${PYTHONPATH}:python/lib/python3.11/site-packages" | ||
|
||
RUN yum install -y zip | ||
RUN pip install -r requirements.txt --target python/lib/python3.11/site-packages | ||
RUN python3 -m spacy download en_core_web_sm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# AWS Lambda Layer for OpenAI/Langchain Lambdas | ||
|
||
This layer contains the combined pip requirements for Natural Language Processing features. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/bin/bash | ||
#------------------------------------------------------------------------------ | ||
# written by: Lawrence McDaniel | ||
# https://lawrencemcdaniel.com/ | ||
# | ||
# date: nov-2023 | ||
# | ||
# usage: Lambda Python packaging tool. | ||
# Called by Terraform "null_resource". Copies python | ||
# module(s) plus any requirements to a dedicated folder so that | ||
# it can be archived to a zip file for upload to | ||
# AWS Lambda by Terraform. | ||
#------------------------------------------------------------------------------ | ||
cd $SOURCE_CODE_PATH | ||
|
||
|
||
docker build -t $CONTAINER_NAME . | ||
docker rm $CONTAINER_NAME | ||
docker run --name $CONTAINER_NAME --entrypoint /bin/bash $CONTAINER_NAME -c "zip -r $PACKAGE_NAME ." | ||
|
||
# Delete the distribution package if it exists | ||
if [ -f $PACKAGE_NAME ]; then | ||
rm $PACKAGE_NAME | ||
fi | ||
|
||
docker cp $CONTAINER_NAME:/var/task/$PACKAGE_NAME . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# ----------------------------------------------------------------------------- | ||
# written by: Lawrence McDaniel | ||
# https://lawrencemcdaniel.com | ||
# | ||
# usage: Shared Python requirements for AWS Lambda functions. | ||
# Create a virtual environment in the root of this repository | ||
# named `venv`. Terraform modules will look for and include these | ||
# requirements in the zip package for this layer. | ||
# ----------------------------------------------------------------------------- | ||
|
||
# NLP requirements | ||
# -------------------------- | ||
python-Levenshtein==0.23.0 | ||
spacy==3.7.2 | ||
pyyaml |