Creating AWS Lambda Layer
How to create AWS Lambda Layer (for Python)
From Library
Be sure to work from Python Virtual Environment. Always work from venv. Follow this link. You can also see here.
1. Install Libraries that you want
python -m pip install requests
2. Show it's dependencies (you'll also see it being installed). You'll see Requires line with certifi, urllib3, charset-normalizer, idna
pip show requests
3. Open the folder /Lib/site-packages
4. Copy all the listed Requires packages into a separate directory called python
5. Zip up this folder. Resulting zip and content
locals{ layer_file = "${path.module}/requests_2_26_0.zip" } resource "aws_lambda_layer_version" "requests" { filename = local.layer_file layer_name = "requests" compatible_runtimes = ["python3.8"] source_code_hash = data.archive_file.requests.output_base64sha256 }
From Custom Code
1. Create a new folder called python
2. Drop your python code into this folder
3. Create a zip file where python is at root
4. Terraform Code to use (this code will handle the zipping). Your python code should be located at /layer_code/python/my_code.py
locals{ layer_file = "${path.module}/zips/new_layer.zip" } data "archive_file" "requests" { type = "zip" output_path = local.layer_file source_dir = "${path.module}/layer_code" output_file_mode = "0666" } resource "aws_lambda_layer_version" "new_layer" { filename = local.layer_file layer_name = "new_layer" compatible_runtimes = ["python3.8"] source_code_hash = data.archive_file.requests.output_base64sha256 }
No comments:
Post a Comment