Skip to content

Commit 9d671a1

Browse files
author
Bjorn Olsen
committed
feat: Make archiving optional
1 parent 436941f commit 9d671a1

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ Check [examples](./examples) for non-python examples.
7070
|------|-------------|------|:--------:|
7171
| <a name="input_function_name"></a> [function\_name](#input\_function\_name) | A unique name for your Lambda Function. | `string` | yes |
7272
| <a name="input_handler"></a> [handler](#input\_handler) | The function entrypoint in your code. | `string` | yes |
73-
| <a name="input_output_path"></a> [output\_path](#input\_output\_path) | A path to which the source directory is archived before uploading to AWS. | `string` | yes |
73+
| <a name="input_output_path"></a> [output\_path](#input\_output\_path) | A path to the archive file which will be uploaded to AWS. If `source_dir` is not `null`, then a file is created at `output_path` containing the archived contents of `source_dir`. | `string` | yes |
7474
| <a name="input_runtime"></a> [runtime](#input\_runtime) | The identifier of the function's runtime. | `string` | yes |
75-
| <a name="input_source_dir"></a> [source\_dir](#input\_source\_dir) | A path to the directory which contains source files. | `string` | yes |
75+
| <a name="input_source_dir"></a> [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 |
7676
| <a name="input_allowed_services"></a> [allowed\_services](#input\_allowed\_services) | A list of AWS Services that are allowed to access this lambda. | `list(string)` | no |
7777
| <a name="input_build_command"></a> [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 |
7878
| <a name="input_build_triggers"></a> [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 |

build.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ resource "null_resource" "build" {
1212
}
1313

1414
data "archive_file" "source" {
15+
count = var.source_dir != null ? 1 : 0
1516
type = "zip"
1617
source_dir = var.source_dir
1718
excludes = var.exclude_files

main.tf

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,21 @@ resource "aws_cloudwatch_log_group" "this" {
7676
# Lambda function
7777
#---------------------------------------------------------------------------------------------------
7878

79+
locals {
80+
lambda_filename = try(
81+
data.archive_file.source[0].output_path,
82+
var.output_path
83+
)
84+
lambda_source_code_hash = try(
85+
data.archive_file.source[0].output_base64sha256,
86+
filebase64sha256(var.output_path)
87+
)
88+
}
89+
7990
resource "aws_lambda_function" "this" {
80-
filename = data.archive_file.source.output_path
91+
filename = local.lambda_filename
8192
role = aws_iam_role.this.arn
82-
source_code_hash = data.archive_file.source.output_base64sha256
93+
source_code_hash = local.lambda_source_code_hash
8394

8495
runtime = var.runtime
8596
handler = var.handler

variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ variable "build_triggers" {
2727
}
2828

2929
variable "source_dir" {
30-
description = "A path to the directory which contains source files."
30+
description = "A path to the directory which contains source files to be archived. If set to `null`, then no archive file is created."
3131
type = string
3232
}
3333

3434
variable "output_path" {
35-
description = "A path to which the source directory is archived before uploading to AWS."
35+
description = "A path to the archive file which will be uploaded to AWS. If `source_dir` is not `null`, then a file is created at `output_path` containing the archived contents of `source_dir`."
3636
type = string
3737
}
3838

0 commit comments

Comments
 (0)