From d195edf821adb72fe5e30a7a6b4346a4b332472f Mon Sep 17 00:00:00 2001 From: Bjorn Olsen Date: Fri, 9 Dec 2022 08:45:30 +0200 Subject: [PATCH] feat: Make archiving optional --- README.md | 4 ++-- build.tf | 1 + main.tf | 15 +++++++++++++-- variables.tf | 4 ++-- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d9c81a1..b116eea 100644 --- a/README.md +++ b/README.md @@ -70,9 +70,9 @@ Check [examples](./examples) for non-python examples. |------|-------------|------|:--------:| | [function\_name](#input\_function\_name) | A unique name for your Lambda Function. | `string` | yes | | [handler](#input\_handler) | The function entrypoint in your code. | `string` | yes | -| [output\_path](#input\_output\_path) | A path to which the source directory is archived before uploading to AWS. | `string` | yes | +| [output\_path](#input\_output\_path) | A path to the archive file which will be uploaded to AWS. If `source_dir` is not `null`, then this file is created with the archived contents of `source_dir`. | `string` | yes | | [runtime](#input\_runtime) | The identifier of the function's runtime. | `string` | yes | -| [source\_dir](#input\_source\_dir) | A path to the directory which contains source files. | `string` | yes | +| [source\_dir](#input\_source\_dir) | A path to the directory which contains source files to be archived. If set to `null`, then no archive file is created. | `string` | yes | | [allowed\_services](#input\_allowed\_services) | A list of AWS Services that are allowed to access this lambda. | `list(string)` | no | | [build\_command](#input\_build\_command) | This is the build command to execute. It can be provided as a relative path to the current working directory or as an absolute path. It is evaluated in a shell, and can use environment variables or Terraform variables. | `string` | no | | [build\_triggers](#input\_build\_triggers) | A map of values which should cause the build command to re-run. Values are meant to be interpolated references to variables or attributes of other resources. | `map(string)` | no | diff --git a/build.tf b/build.tf index 9242df6..77b785e 100644 --- a/build.tf +++ b/build.tf @@ -12,6 +12,7 @@ resource "null_resource" "build" { } data "archive_file" "source" { + count = var.source_dir != null ? 1 : 0 type = "zip" source_dir = var.source_dir excludes = var.exclude_files diff --git a/main.tf b/main.tf index 48fba36..f70e6f0 100644 --- a/main.tf +++ b/main.tf @@ -76,10 +76,21 @@ resource "aws_cloudwatch_log_group" "this" { # Lambda function #--------------------------------------------------------------------------------------------------- +locals { + lambda_filename = try( + data.archive_file.source[0].output_path, + var.output_path + ) + lambda_source_code_hash = try( + data.archive_file.source[0].output_base64sha256, + filebase64sha256(var.output_path) + ) +} + resource "aws_lambda_function" "this" { - filename = data.archive_file.source.output_path + filename = local.lambda_filename role = aws_iam_role.this.arn - source_code_hash = data.archive_file.source.output_base64sha256 + source_code_hash = local.lambda_source_code_hash runtime = var.runtime handler = var.handler diff --git a/variables.tf b/variables.tf index 49e5527..1af5a55 100644 --- a/variables.tf +++ b/variables.tf @@ -27,12 +27,12 @@ variable "build_triggers" { } variable "source_dir" { - description = "A path to the directory which contains source files." + description = "A path to the directory which contains source files to be archived. If set to `null`, then no archive file is created." type = string } variable "output_path" { - description = "A path to which the source directory is archived before uploading to AWS." + description = "A path to the archive file which will be uploaded to AWS. If `source_dir` is not `null`, then this file is created with the archived contents of `source_dir`." type = string }