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
+15 -18
View File
@@ -1,42 +1,39 @@
<!-- This readme file is generated with terraform-docs -->
# Notice
This layer is obsolete. A replacement will be pushed when ready.
# lambda-layers
Download python packages and create lambda layer
## Prepare lambda-layer1 with the following command.
The path is hard-required by AWS. See https://docs.aws.amazon.com/lambda/latest/dg/packaging-layers.html
```bash
pip install requests -t python/lib/python3.12/site-packages/
```
## Notes
Packages need to be placed under a python/ subdirectory.
See https://docs.aws.amazon.com/lambda/latest/dg/packaging-layers.html
## Requirements
| Name | Version |
|------|---------|
| terraform | >= 1.3.0 |
| aws | >= 4.40 |
| terraform | >= 1.1.0 |
| aws | >= 6.0 |
## Providers
| Name | Version |
|------|---------|
| archive | 2.5.0 |
| aws | 5.64.0 |
| aws | 5.100.0 |
| random | 3.8.1 |
## Modules
No modules.
| Name | Source | Version |
|------|--------|---------|
| lambda\_archive | ../modules/compute/LambdaZipBuilder | n/a |
| s3 | ../modules/storage/s3_bucket_2023 | n/a |
## Resources
| Name | Type |
|------|------|
| [aws_iam_role.lambda-role1](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_lambda_function.myFunction](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
| [aws_lambda_layer_version.libraries](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_layer_version) | resource |
| [archive_file.function1](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
| [archive_file.layer1](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
| [aws_lambda_layer_version.pandas](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_layer_version) | resource |
| [random_uuid.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/uuid) | resource |
## Inputs
-10
View File
@@ -1,10 +0,0 @@
# reference: https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-eventbridge/
import requests
def lambda_handler(event, context):
r = requests.get('https://ipinfo.io/')
return {
"HttpResponseCode": r.status_code
}
Binary file not shown.
Binary file not shown.
+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" {}
+2 -3
View File
@@ -2,18 +2,17 @@ provider "aws" {
region = "ap-east-1"
default_tags {
tags = {
TerraformMode = "managed"
TerraformDir = join("/", reverse(slice(reverse(split("/", path.cwd)), 0, 2)))
}
}
}
terraform {
required_version = ">= 1.3.0"
required_version = ">= 1.1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.40"
version = ">= 6.0"
}
}
}