Skip to content

Commit c0f5a32

Browse files
committed
issue-658
1 parent 84dfbfd commit c0f5a32

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

main.tf

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ locals {
1919
s3_key = var.s3_existing_package != null ? try(var.s3_existing_package.key, null) : (var.store_on_s3 ? var.s3_prefix != null ? format("%s%s", var.s3_prefix, replace(local.archive_filename_string, "/^.*//", "")) : replace(local.archive_filename_string, "/^\\.//", "") : null)
2020
s3_object_version = var.s3_existing_package != null ? try(var.s3_existing_package.version_id, null) : (var.store_on_s3 ? try(aws_s3_object.lambda_package[0].version_id, null) : null)
2121

22+
# s3_signing
23+
s3_signing_enabled = local.create && var.store_on_s3 && var.create_package && var.enable_code_signing && var.lambda_code_signing_profile_name != null
24+
s3_signing_bucket = var.s3_signing_bucket != null && local.s3_signing_enabled ? var.s3_signing_bucket : local.s3_bucket
25+
s3_signing_prefix = var.s3_signing_prefix != null && local.s3_signing_enabled ? var.s3_signing_prefix : (var.s3_prefix != null ? var.s3_prefix : "")
26+
27+
lambda_s3_bucket = local.s3_signing_enabled ? aws_signer_signing_job.lambda_code_signing[0].signed_object[0].s3[0].bucket : local.s3_bucket
28+
lambda_s3_key = local.s3_signing_enabled ? aws_signer_signing_job.lambda_code_signing[0].signed_object[0].s3[0].key : local.s3_key
29+
lambda_s3_version = local.s3_signing_enabled ? null : local.s3_object_version # aws_signer_signing_job does not return a version id
2230
}
2331

2432
resource "aws_lambda_function" "this" {
@@ -55,9 +63,9 @@ resource "aws_lambda_function" "this" {
5563
filename = local.filename
5664
source_code_hash = var.ignore_source_code_hash ? null : (local.filename == null ? false : fileexists(local.filename)) && !local.was_missing ? filebase64sha256(local.filename) : null
5765

58-
s3_bucket = local.s3_bucket
59-
s3_key = local.s3_key
60-
s3_object_version = local.s3_object_version
66+
s3_bucket = local.lambda_s3_bucket
67+
s3_key = local.lambda_s3_key
68+
s3_object_version = local.lambda_s3_version
6169

6270
dynamic "image_config" {
6371
for_each = length(var.image_config_entry_point) > 0 || length(var.image_config_command) > 0 || var.image_config_working_directory != null ? [true] : []
@@ -181,9 +189,9 @@ resource "aws_lambda_layer_version" "this" {
181189
filename = local.filename
182190
source_code_hash = var.ignore_source_code_hash ? null : (local.filename == null ? false : fileexists(local.filename)) && !local.was_missing ? filebase64sha256(local.filename) : null
183191

184-
s3_bucket = local.s3_bucket
185-
s3_key = local.s3_key
186-
s3_object_version = local.s3_object_version
192+
s3_bucket = local.lambda_s3_bucket
193+
s3_key = local.lambda_s3_key
194+
s3_object_version = local.lambda_s3_version
187195

188196
depends_on = [null_resource.archive, aws_s3_object.lambda_package]
189197
}
@@ -215,6 +223,27 @@ resource "aws_s3_object" "lambda_package" {
215223
depends_on = [null_resource.archive]
216224
}
217225

226+
resource "aws_signer_signing_job" "lambda_code_signing" {
227+
count = local.s3_signing_enabled ? 1 : 0
228+
profile_name = var.lambda_code_signing_profile_name
229+
230+
source {
231+
s3 {
232+
bucket = local.s3_bucket
233+
key = local.s3_key
234+
version = local.s3_object_version
235+
}
236+
}
237+
238+
destination {
239+
s3 {
240+
bucket = local.s3_signing_bucket
241+
prefix = local.s3_signing_prefix
242+
}
243+
}
244+
ignore_signing_job_failure = var.ignore_signing_job_failure
245+
}
246+
218247
data "aws_cloudwatch_log_group" "lambda" {
219248
count = local.create && var.create_function && !var.create_layer && var.use_existing_cloudwatch_log_group ? 1 : 0
220249

variables.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,3 +843,37 @@ variable "recursive_loop" {
843843
type = string
844844
default = null
845845
}
846+
847+
###############
848+
# Code Signing
849+
###############
850+
851+
variable "enable_code_signing" {
852+
description = "Must be used with a lambda storing code on s3. Set this to true for triggering a signing job creating a signed copy of the lambda zip. https://docs.aws.amazon.com/lambda/latest/dg/configuration-codesigning.html"
853+
type = bool
854+
default = false
855+
}
856+
857+
variable "lambda_code_signing_profile_name" {
858+
description = "Lambda code signing profile name https://console.aws.amazon.com/lambda/home#/code-signing-configurations"
859+
type = string
860+
default = null
861+
}
862+
863+
variable "s3_signing_bucket" {
864+
description = "Bucket where to upload the signed s3 file. If omitted default to var.s3_bucket"
865+
type = string
866+
default = null
867+
}
868+
869+
variable "s3_signing_prefix" {
870+
description = "Prefix for the generated signed object. If omitted default to var.s3_prefix"
871+
type = string
872+
default = null
873+
}
874+
875+
variable "ignore_signing_job_failure" {
876+
description = "Set this argument to true to ignore signing job failures and retrieve failed status and reason"
877+
type = bool
878+
default = false
879+
}

0 commit comments

Comments
 (0)