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 --> <!-- This readme file is generated with terraform-docs -->
# Notice # lambda-layers
This layer is obsolete. A replacement will be pushed when ready.
Download python packages and create lambda layer
## Prepare lambda-layer1 with the following command. ## Notes
The path is hard-required by AWS. See https://docs.aws.amazon.com/lambda/latest/dg/packaging-layers.html Packages need to be placed under a python/ subdirectory.
See https://docs.aws.amazon.com/lambda/latest/dg/packaging-layers.html
```bash
pip install requests -t python/lib/python3.12/site-packages/
```
## Requirements ## Requirements
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| terraform | >= 1.3.0 | | terraform | >= 1.1.0 |
| aws | >= 4.40 | | aws | >= 6.0 |
## Providers ## Providers
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| archive | 2.5.0 | | aws | 5.100.0 |
| aws | 5.64.0 | | random | 3.8.1 |
## Modules ## Modules
No modules. | Name | Source | Version |
|------|--------|---------|
| lambda\_archive | ../modules/compute/LambdaZipBuilder | n/a |
| s3 | ../modules/storage/s3_bucket_2023 | n/a |
## Resources ## Resources
| Name | Type | | Name | Type |
|------|------| |------|------|
| [aws_iam_role.lambda-role1](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource | | [aws_lambda_layer_version.pandas](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_layer_version) | resource |
| [aws_lambda_function.myFunction](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource | | [random_uuid.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/uuid) | 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 |
## Inputs ## 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" * # lambda-layers
depends_on = [data.archive_file.layer1] *
filename = "lambda-layer1.zip" * Download python packages and create lambda layer
layer_name = "Python3RequestsLibrary" *
* ## 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" { # create lambda layer
source_dir = "lambda-layer1" resource "aws_lambda_layer_version" "pandas" {
type = "zip" description = "Python packages pandas numpy pytz openpyxl"
output_path = "lambda-layer1.zip" 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" { # s3 bucket required for uploading python package zip
source_file = "function.py" module "s3" {
type = "zip" source = "../modules/storage/s3_bucket_2023"
output_path = "function1.zip" 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" { resource "random_uuid" "this" {}
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"]
}
+3 -4
View File
@@ -2,18 +2,17 @@ provider "aws" {
region = "ap-east-1" region = "ap-east-1"
default_tags { default_tags {
tags = { tags = {
TerraformMode = "managed" TerraformDir = join("/", reverse(slice(reverse(split("/", path.cwd)), 0, 2)))
TerraformDir = join("/", reverse(slice(reverse(split("/", path.cwd)), 0, 2)))
} }
} }
} }
terraform { terraform {
required_version = ">= 1.3.0" required_version = ">= 1.1.0"
required_providers { required_providers {
aws = { aws = {
source = "hashicorp/aws" source = "hashicorp/aws"
version = ">= 4.40" version = ">= 6.0"
} }
} }
} }