Skip to content

Commit

Permalink
refactor(lambda): make package_s3 variable an object
Browse files Browse the repository at this point in the history
  • Loading branch information
mskrajnowski committed Mar 7, 2021
1 parent 746c740 commit 76c00b4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
7 changes: 5 additions & 2 deletions lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ Creates an AWS Lambda function

Path to the zip that contains the Lambda's source. Either `package_path` or `package_s3` is required.

* `package_s3` (`string`, default: `null`)
* `package_s3` (`object({
bucket = string
key = string
})`, default: `null`)

S3 object path to a zip that contains the Lambda's source in a `{bucket}/{key}` format. Either `package_path` or `package_s3` is required.
S3 zip object that contains the Lambda's source. Either `package_path` or `package_s3` is required.

* `package_s3_version` (`string`, default: `null`)

Expand Down
7 changes: 5 additions & 2 deletions lambda/layer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ Creates an AWS Lambda Layer that can be attached to a AWS Lambda Function

Path to the zip that contains the Lambda layer's source. Either `package_path`, `package_s3` or `image` is required.

* `package_s3` (`string`, default: `null`)
* `package_s3` (`object({
bucket = string
key = string
})`, default: `null`)

S3 object path to a zip that contains the Lambda layer's source in a `{bucket}/{key}` format. Either `package_path`, `package_s3` or `image` is required.
S3 zip object that contains the Lambda layer's source. Either `package_path` or `package_s3` is required.

* `package_s3_version` (`string`, default: `null`)

Expand Down
14 changes: 5 additions & 9 deletions lambda/layer/main.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
locals {
package_s3_match = var.package_s3 != null ? regex(
"(?P<bucket>[^/]+)/(?P<key>.+)", var.package_s3
) : { bucket = null, key = null }
package_s3_bucket = local.package_s3_match.bucket
package_s3_key = local.package_s3_match.key
create_package = var.create && (var.files != null || var.files_dir != null)
}

module "package" {
source = "./../../zip"
create = var.create && (var.files != null || var.files_dir != null)
create = local.create_package

files = var.files
directory = var.files_dir
Expand All @@ -24,9 +20,9 @@ resource "aws_lambda_layer_version" "layer" {
layer_name = var.name
compatible_runtimes = var.runtimes

filename = coalesce(var.package_path, module.package.output_path)
s3_bucket = local.package_s3_bucket
s3_key = local.package_s3_key
filename = local.create_package ? module.package.output_path : var.package_path
s3_bucket = var.package_s3 != null ? var.package_s3.bucket : null
s3_key = var.package_s3 != null ? var.package_s3.key : null
s3_object_version = var.package_s3_version
}

Expand Down
9 changes: 6 additions & 3 deletions lambda/layer/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ variable "package_path" {
}

variable "package_s3" {
description = "S3 object path to a zip that contains the Lambda layer's source in a `{bucket}/{key}` format. Either `package_path`, `package_s3` or `image` is required."
type = string
default = null
description = "S3 zip object that contains the Lambda layer's source. Either `package_path` or `package_s3` is required."
type = object({
bucket = string
key = string
})
default = null
}

variable "package_s3_version" {
Expand Down
14 changes: 5 additions & 9 deletions lambda/main.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
locals {
package_s3_match = var.package_s3 != null ? regex(
"(?P<bucket>[^/]+)/(?P<key>.+)", var.package_s3
) : { bucket = null, key = null }
package_s3_bucket = local.package_s3_match.bucket
package_s3_key = local.package_s3_match.key
create_package = var.create && (var.files != null || var.files_dir != null)
}

module "package" {
source = "./../zip"
create = var.create && (var.files != null || var.files_dir != null)
create = local.create_package

files = var.files
directory = var.files_dir
Expand Down Expand Up @@ -81,9 +77,9 @@ resource "aws_lambda_function" "lambda" {

function_name = var.name

filename = coalesce(var.package_path, module.package.output_path)
s3_bucket = local.package_s3_bucket
s3_key = local.package_s3_key
filename = local.create_package ? module.package.output_path : var.package_path
s3_bucket = var.package_s3 != null ? var.package_s3.bucket : null
s3_key = var.package_s3 != null ? var.package_s3.key : null
s3_object_version = var.package_s3_version

layers = var.layer_qualified_arns
Expand Down
9 changes: 6 additions & 3 deletions lambda/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ variable "package_path" {
}

variable "package_s3" {
description = "S3 object path to a zip that contains the Lambda's source in a `{bucket}/{key}` format. Either `package_path` or `package_s3` is required."
type = string
default = null
description = "S3 zip object that contains the Lambda's source. Either `package_path` or `package_s3` is required."
type = object({
bucket = string
key = string
})
default = null
}

variable "package_s3_version" {
Expand Down

0 comments on commit 76c00b4

Please sign in to comment.