Deploy WebChat Lambda
Prerequisites
- For our WebChat to work, we would need to setup few resources on AWS. To do this, we must have Terraform CLI installed and configured with a private token.
- One of the services we would deploy would be an AWS Lambda, so we need Python and a package manager installed in our machine, preferably Poetry.
Configuring Poetry
At the time of writing, Poetry's latest stable version is v1.1. But we need at least v1.2 to make the whole setup process easier. To update you poetry version to the required version, run the following commands:
poetry self update --preview
poetry plugin add git+https://github.com/python-poetry/poetry-bundle-plugin#main
This is a one-time setup.
note
If you get an error about Poetry not being installed with the official installer, uninstall Poetry first and use the official installer this time. Once installed, run the above commands again.
Setting up the code for Lambda
Create a new Poetry project by using the command:
poetry new my-web-chat-lambda
cd my-web-chat-lambda
Now follow these instructions to setup private Neev repository for Poetry. Once added, Poetry would look for Neev packages from this repository and the remaining packages from PyPi.
Let's install web-chat-lambda
:
poetry add web-chat-lambda
This package includes a helpful lambda function, which we will use directly afterwards to avoid having to write any code.
Zipping our code
Our Python code and its dependencies must be zipped in order to use with the Terraform template. Because we are not writing any code, this just means that only our dependencies must be zipped.
To create a zip, follow these instructions:
- macOS or Linux
- Powershell
poetry bundle venv bundle-folder
# replace `python3.9` with the folder you have on your machine
cd bundle-folder/lib/python3.9/site-packages
Now we have to zip all the contents of site-packages/
. Let's name it lambda-code.zip
:
# while being inside `site-packages/`, zip the contents of this folder
zip -qr lambda-code.zip *
# and move the zip to the project root
mv lambda-code ../../../../
poetry bundle venv bundle-folder
cd bundle-folder/Lib/site-packages
We need to zip all the contents of site-packages/
. You can use Windows Explorer or the zip tool you prefer to create a lambda-code.zip
from the contents. Make sure you select all folders and files under site-packages/
and zip them, not zip the site-packages/
folder itself. Once done, cut and paste this zip to your project root for easy access.
Deploying AWS resources using Terraform
Follow the instructions in this repository to leverage a Terraform (TF) template. Configure the file along with changing these attributes:
module "lambda_module" {
# ...
lambda_function_handler = "web_chat_lambda.lambda_function.lambda_handler"
lambda_source = "../path/to/lambda-code.zip"
# ...
}
Given that you have configured Terraform CLI with a private token to retrieve the TF template (if not done, see prerequisites), you can use terraform init
followed by terraform apply
to create the required resource.
Setting up an API Gateway
You would have add the deployed Lambda to an API Gateway manually so that the WebChat frontend could use it. This Lambda would be receiving a POST request.