1
0

feat: use new module to build python package archive

This commit is contained in:
xpk
2026-02-13 17:44:42 +08:00
parent ec0aebcacb
commit fa698d0939
6 changed files with 54 additions and 77 deletions
+35 -44
View File
@@ -1,51 +1,42 @@
resource "aws_lambda_layer_version" "libraries" {
description = "Python3 requests library"
depends_on = [data.archive_file.layer1]
filename = "lambda-layer1.zip"
layer_name = "Python3RequestsLibrary"
/**
* # lambda-layers
*
* Download python packages and create lambda layer
*
* ## Notes
* Packages need to be placed under a python/ subdirectory.
* See https://docs.aws.amazon.com/lambda/latest/dg/packaging-layers.html
*/
compatible_runtimes = ["python3.12"]
# build python package zip file
module "lambda_archive" {
source = "../modules/compute/LambdaZipBuilder"
pip_packages = "pandas numpy pytz openpyxl"
upload_archive_to_s3 = true
s3_bucket_name = module.s3.bucket_name
pip_path = "/my/work/xpk-git/venv314/bin/pip3"
}
data "archive_file" "layer1" {
source_dir = "lambda-layer1"
type = "zip"
output_path = "lambda-layer1.zip"
# create lambda layer
resource "aws_lambda_layer_version" "pandas" {
description = "Python packages pandas numpy pytz openpyxl"
s3_bucket = module.s3.bucket_name
s3_key = module.lambda_archive.s3_object_key
source_code_hash = module.lambda_archive.s3_object_hash
layer_name = "py_packages"
compatible_runtimes = ["python3.13"]
}
data "archive_file" "function1" {
source_file = "function.py"
type = "zip"
output_path = "function1.zip"
# s3 bucket required for uploading python package zip
module "s3" {
source = "../modules/storage/s3_bucket_2023"
bucket_name = "lab-lambdalayer-${random_uuid.this.result}"
bucket_force_destroy = true
enable_bucket_logging = false
enable_bucket_lifecycle = false
enable_versioning = false
enable_encryption = true
}
resource "aws_lambda_function" "myFunction" {
description = "Lambda layer test"
filename = data.archive_file.function1.output_path
source_code_hash = data.archive_file.function1.output_base64sha256
function_name = "TestFunction"
role = aws_iam_role.lambda-role1.arn
handler = "function.lambda_handler"
runtime = "python3.12"
architectures = ["arm64"]
layers = [aws_lambda_layer_version.libraries.arn]
}
resource "aws_iam_role" "lambda-role1" {
name = "TestFunctionRole"
assume_role_policy = jsonencode(
{
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {
"Service" : "lambda.amazonaws.com"
},
"Action" : "sts:AssumeRole"
}
]
}
)
managed_policy_arns = ["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"]
}
resource "random_uuid" "this" {}