diff --git a/lambda-layers/README.md b/lambda-layers/README.md index ee17ee0..667dbe8 100644 --- a/lambda-layers/README.md +++ b/lambda-layers/README.md @@ -1,42 +1,39 @@ -# 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 @@ -48,4 +45,4 @@ No outputs. --- ## Authorship -This module was developed by xpk. +This module was developed by xpk. \ No newline at end of file diff --git a/lambda-layers/function.py b/lambda-layers/function.py deleted file mode 100644 index 62dbd4b..0000000 --- a/lambda-layers/function.py +++ /dev/null @@ -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 - } - - diff --git a/lambda-layers/function1.zip b/lambda-layers/function1.zip deleted file mode 100644 index a53da18..0000000 Binary files a/lambda-layers/function1.zip and /dev/null differ diff --git a/lambda-layers/lambda-layer1.zip b/lambda-layers/lambda-layer1.zip deleted file mode 100644 index 2fb3df5..0000000 Binary files a/lambda-layers/lambda-layer1.zip and /dev/null differ diff --git a/lambda-layers/main.tf b/lambda-layers/main.tf index da88c98..b180259 100644 --- a/lambda-layers/main.tf +++ b/lambda-layers/main.tf @@ -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"] -} \ No newline at end of file +resource "random_uuid" "this" {} \ No newline at end of file diff --git a/lambda-layers/provider.tf b/lambda-layers/provider.tf index d1a6851..c9ed12f 100644 --- a/lambda-layers/provider.tf +++ b/lambda-layers/provider.tf @@ -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))) + 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" } } } \ No newline at end of file