Skip to content

Commit 2c845c3

Browse files
authored
fix: Strip leading ./ in S3 key (#191)
1 parent 883ec89 commit 2c845c3

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ No modules.
732732
| <a name="input_s3_existing_package"></a> [s3\_existing\_package](#input\_s3\_existing\_package) | The S3 bucket object with keys bucket, key, version pointing to an existing zip-file to use | `map(string)` | `null` | no |
733733
| <a name="input_s3_object_storage_class"></a> [s3\_object\_storage\_class](#input\_s3\_object\_storage\_class) | Specifies the desired Storage Class for the artifact uploaded to S3. Can be either STANDARD, REDUCED\_REDUNDANCY, ONEZONE\_IA, INTELLIGENT\_TIERING, or STANDARD\_IA. | `string` | `"ONEZONE_IA"` | no |
734734
| <a name="input_s3_object_tags"></a> [s3\_object\_tags](#input\_s3\_object\_tags) | A map of tags to assign to S3 bucket object. | `map(string)` | `{}` | no |
735+
| <a name="input_s3_prefix"></a> [s3\_prefix](#input\_s3\_prefix) | Directory name where artifacts should be stored in the S3 bucket. If unset, the path from `artifacts_dir` is used | `string` | `null` | no |
735736
| <a name="input_s3_server_side_encryption"></a> [s3\_server\_side\_encryption](#input\_s3\_server\_side\_encryption) | Specifies server-side encryption of the object in S3. Valid values are "AES256" and "aws:kms". | `string` | `null` | no |
736737
| <a name="input_source_path"></a> [source\_path](#input\_source\_path) | The absolute path to a local file or directory containing your Lambda source code | `any` | `null` | no |
737738
| <a name="input_store_on_s3"></a> [store\_on\_s3](#input\_store\_on\_s3) | Whether to store produced artifacts on S3 or locally. | `bool` | `false` | no |

examples/complete/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ module "lambda_function" {
2727

2828
store_on_s3 = true
2929
s3_bucket = module.s3_bucket.s3_bucket_id
30+
s3_prefix = "lambda-builds/"
31+
32+
artifacts_dir = "${path.root}/.terraform/lambda-builds/"
3033

3134
layers = [
3235
module.lambda_layer_local.lambda_layer_arn,

main.tf

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
locals {
2+
archive_filename = element(concat(data.external.archive_prepare.*.result.filename, [null]), 0)
3+
archive_was_missing = element(concat(data.external.archive_prepare.*.result.was_missing, [false]), 0)
4+
25
# Use a generated filename to determine when the source code has changed.
36
# filename - to get package from local
4-
filename = var.local_existing_package != null ? var.local_existing_package : (var.store_on_s3 ? null : element(concat(data.external.archive_prepare.*.result.filename, [null]), 0))
5-
was_missing = var.local_existing_package != null ? !fileexists(var.local_existing_package) : element(concat(data.external.archive_prepare.*.result.was_missing, [false]), 0)
7+
filename = var.local_existing_package != null ? var.local_existing_package : (var.store_on_s3 ? null : local.archive_filename)
8+
was_missing = var.local_existing_package != null ? !fileexists(var.local_existing_package) : local.archive_was_missing
69

710
# s3_* - to get package from S3
811
s3_bucket = var.s3_existing_package != null ? lookup(var.s3_existing_package, "bucket", null) : (var.store_on_s3 ? var.s3_bucket : null)
9-
s3_key = var.s3_existing_package != null ? lookup(var.s3_existing_package, "key", null) : (var.store_on_s3 ? element(concat(data.external.archive_prepare.*.result.filename, [null]), 0) : null)
12+
s3_key = var.s3_existing_package != null ? lookup(var.s3_existing_package, "key", null) : (var.store_on_s3 ? var.s3_prefix != null ? format("%s%s", var.s3_prefix, replace(local.archive_filename, "/^.*//", "")) : replace(local.archive_filename, "/^\\.//", "") : null)
1013
s3_object_version = var.s3_existing_package != null ? lookup(var.s3_existing_package, "version_id", null) : (var.store_on_s3 ? element(concat(aws_s3_bucket_object.lambda_package.*.version_id, [null]), 0) : null)
1114

1215
}
@@ -114,7 +117,7 @@ resource "aws_s3_bucket_object" "lambda_package" {
114117

115118
bucket = var.s3_bucket
116119
acl = var.s3_acl
117-
key = data.external.archive_prepare[0].result.filename
120+
key = local.s3_key
118121
source = data.external.archive_prepare[0].result.filename
119122
storage_class = var.s3_object_storage_class
120123

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,12 @@ variable "artifacts_dir" {
493493
default = "builds"
494494
}
495495

496+
variable "s3_prefix" {
497+
description = "Directory name where artifacts should be stored in the S3 bucket. If unset, the path from `artifacts_dir` is used"
498+
type = string
499+
default = null
500+
}
501+
496502
variable "ignore_source_code_hash" {
497503
description = "Whether to ignore changes to the function's source code hash. Set to true if you manage infrastructure and code deployments separately."
498504
type = bool

0 commit comments

Comments
 (0)