1
0

initial commit

This commit is contained in:
xpk
2026-02-13 15:44:24 +08:00
parent 66be8224f4
commit 09ce4c881a
570 changed files with 61807 additions and 0 deletions
@@ -0,0 +1,57 @@
<!-- This readme file is generated with terraform-docs -->
# LambdaZipBuilder
Download pip packages and create ./lambda\_layer.zip
Optionally upload archive to s3 bucket
## Requirements
| Name | Version |
|------|---------|
| archive | >= 2.7.1 |
| aws | ~> 5.100.0 |
| null | >= 3.2.4 |
## Providers
| Name | Version |
|------|---------|
| archive | >= 2.7.1 |
| aws | ~> 5.100.0 |
| null | >= 3.2.4 |
## Modules
No modules.
## Resources
| Name | Type |
|------|------|
| [aws_s3_object.object](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_object) | resource |
| [null_resource.pip_download](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.remove_temp_downloads](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [archive_file.layer1](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| pip\_packages | List of pip packages, separated by space | `string` | n/a | yes |
| pip\_path | Path to pip. Defaults to /usr/bin/pip3 | `string` | `"/usr/bin/pip3"` | no |
| s3\_bucket\_name | Name of s3 bucket for storing lambda layer archive | `string` | `null` | no |
| upload\_archive\_to\_s3 | Whether to upload layer archive to s3. Use this for zipped size >50M or unzipped size >250M. Limit is imposed by AWS API | `bool` | `false` | no |
## Outputs
| Name | Description |
|------|-------------|
| archive\_checksum | Hash of archive |
| archive\_path | Full path to archive file |
| archive\_size | Size of archive |
| s3\_object\_hash | S3 object hash, useful for triggering lambda layer update |
| s3\_object\_key | S3 object key |
---
## Authorship
This module was developed by xpk.
@@ -0,0 +1,11 @@
module "lambda-zip" {
source = "../"
pip_packages = "pandas numpy"
}
output "zip_archive" {
value = {
checksum : module.lambda-zip.archive_checksum
size : module.lambda-zip.archive_size
}
}
+45
View File
@@ -0,0 +1,45 @@
/**
* # LambdaZipBuilder
*
* Download pip packages and create ./lambda_layer.zip
* Optionally upload archive to s3 bucket
*
*/
resource "null_resource" "pip_download" {
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
command = "${var.pip_path} install --only-binary=all --target /tmp/LambdaZipBuilder/python ${var.pip_packages}"
}
triggers = {
date = timestamp()
}
}
resource "null_resource" "remove_temp_downloads" {
depends_on = [data.archive_file.layer1]
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
command = "rm -rf /tmp/LambdaZipBuilder || true"
}
triggers = {
cleanup = data.archive_file.layer1.output_base64sha256
}
}
data "archive_file" "layer1" {
depends_on = [null_resource.pip_download]
type = "zip"
source_dir = "/tmp/LambdaZipBuilder"
output_path = "lambda_layer.zip"
}
resource "aws_s3_object" "object" {
count = var.upload_archive_to_s3 ? 1 : 0
bucket = var.s3_bucket_name
key = "LambdaZipBuilder-${sha256(var.pip_packages)}.zip"
source = data.archive_file.layer1.output_path
checksum_algorithm = "SHA256"
source_hash = data.archive_file.layer1.output_base64sha256
}
@@ -0,0 +1,24 @@
output "archive_path" {
description = "Full path to archive file"
value = data.archive_file.layer1.output_path
}
output "archive_checksum" {
description = "Hash of archive"
value = data.archive_file.layer1.output_base64sha256
}
output "archive_size" {
description = "Size of archive"
value = data.archive_file.layer1.output_size
}
output "s3_object_key" {
description = "S3 object key"
value = try(aws_s3_object.object[0].key, null)
}
output "s3_object_hash" {
description = "S3 object hash, useful for triggering lambda layer update"
value = try(aws_s3_object.object[0].checksum_sha256, null)
}
@@ -0,0 +1,16 @@
terraform {
required_providers {
null = {
source = "hashicorp/null"
version = ">= 3.2.4"
}
archive = {
source = "hashicorp/archive"
version = ">= 2.7.1"
}
aws = {
source = "hashicorp/aws"
version = "~> 5.100.0"
}
}
}
@@ -0,0 +1,22 @@
variable "pip_packages" {
type = string
description = "List of pip packages, separated by space"
}
variable "pip_path" {
type = string
description = "Path to pip. Defaults to /usr/bin/pip3"
default = "/usr/bin/pip3"
}
variable "upload_archive_to_s3" {
type = bool
description = "Whether to upload layer archive to s3. Use this for zipped size >50M or unzipped size >250M. Limit is imposed by AWS API"
default = false
}
variable "s3_bucket_name" {
type = string
description = "Name of s3 bucket for storing lambda layer archive"
default = null
}