Skip to content

Commit 8d7b207

Browse files
CCM-14833: Force Code Deploy for Container Based Lambdas
1 parent 97b3624 commit 8d7b207

5 files changed

Lines changed: 21 additions & 3 deletions

File tree

infrastructure/terraform/modules/lambda/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
| <a name="input_enable_lambda_insights"></a> [enable\_lambda\_insights](#input\_enable\_lambda\_insights) | Enable the lambda insights layer, this must be disabled for lambda@edge usage | `bool` | `true` | no |
2121
| <a name="input_environment"></a> [environment](#input\_environment) | The name of the tfscaffold environment | `string` | n/a | yes |
2222
| <a name="input_filter_pattern"></a> [filter\_pattern](#input\_filter\_pattern) | Filter pattern to use for the log subscription filter | `string` | `""` | no |
23-
| <a name="input_force_lambda_code_deploy"></a> [force\_lambda\_code\_deploy](#input\_force\_lambda\_code\_deploy) | If the lambda package in s3 has the same commit id tag as the terraform build branch, the lambda will not update automatically. Set to True if making changes to Lambda code from on the same commit for example during development | `bool` | `false` | no |
23+
| <a name="input_force_lambda_code_deploy"></a> [force\_lambda\_code\_deploy](#input\_force\_lambda\_code\_deploy) | If true, force code deploy checks for Lambda packages. For Zip, this enables source hash tracking of the package archive. For Image, tagged ECR image URIs are resolved to their current digest so Lambda updates when the tag is repointed. | `bool` | `false` | no |
2424
| <a name="input_function_code_base_path"></a> [function\_code\_base\_path](#input\_function\_code\_base\_path) | The base path to the sourcecode directories needed for this lambda | `string` | `"./"` | no |
2525
| <a name="input_function_code_dir"></a> [function\_code\_dir](#input\_function\_code\_dir) | The directory for this lambda | `string` | `null` | no |
2626
| <a name="input_function_include_common"></a> [function\_include\_common](#input\_function\_include\_common) | Include the 'common' lambda module with this lambda | `bool` | `true` | no |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
data "aws_ecr_image" "lambda" {
2+
count = local.resolve_image_to_digest ? 1 : 0
3+
4+
repository_name = local.image_repository_name
5+
image_tag = local.image_tag
6+
}

infrastructure/terraform/modules/lambda/lambda_function.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ resource "aws_lambda_function" "main" {
1313
s3_key = local.package_type == "zip" ? aws_s3_object.lambda[0].key : null
1414
s3_object_version = local.package_type == "zip" ? aws_s3_object.lambda[0].version_id : null
1515

16-
image_uri = local.package_type == "image" ? var.image_uri : null
16+
image_uri = local.package_type == "image" ? local.effective_image_uri : null
1717

1818
dynamic "image_config" {
1919
for_each = local.package_type == "image" && var.image_config != null ? [1] : []

infrastructure/terraform/modules/lambda/locals.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ locals {
33

44
package_type = lower(var.package_type)
55

6+
# For Image package types, optionally resolve tag-based URIs to digests
7+
# when force_lambda_code_deploy is enabled.
8+
image_uri_parts = split(":", var.image_uri != null ? var.image_uri : "")
9+
image_uri_has_digest = var.image_uri != null ? length(split("@", var.image_uri)) > 1 : false
10+
image_uri_has_tag = var.image_uri != null ? length(local.image_uri_parts) == 2 : false
11+
image_repository_uri = local.image_uri_has_tag ? local.image_uri_parts[0] : null
12+
image_tag = local.image_uri_has_tag ? local.image_uri_parts[1] : null
13+
image_repository_parts = local.image_repository_uri != null ? split("/", local.image_repository_uri) : []
14+
image_repository_name = local.image_repository_uri != null ? join("/", slice(local.image_repository_parts, 1, length(local.image_repository_parts))) : null
15+
resolve_image_to_digest = local.package_type == "image" && var.force_lambda_code_deploy && !local.image_uri_has_digest && local.image_uri_has_tag
16+
effective_image_uri = local.resolve_image_to_digest ? "${local.image_repository_uri}@${data.aws_ecr_image.lambda[0].image_digest}" : var.image_uri
17+
618
# Compound Scope Identifier
719
csi = replace(
820
format(

infrastructure/terraform/modules/lambda/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ variable "function_include_common" {
198198

199199
variable "force_lambda_code_deploy" {
200200
type = bool
201-
description = "If the lambda package in s3 has the same commit id tag as the terraform build branch, the lambda will not update automatically. Set to True if making changes to Lambda code from on the same commit for example during development"
201+
description = "If true, force code deploy checks for Lambda packages. For Zip, this enables source hash tracking of the package archive. For Image, tagged ECR image URIs are resolved to their current digest so Lambda updates when the tag is repointed."
202202
default = false
203203
}
204204

0 commit comments

Comments
 (0)