Skip to content

Commit

Permalink
Fixes the SG already exists error when recreating with create_before_…
Browse files Browse the repository at this point in the history
…destroy by introducing a sg_name_prefix variable that can be used to override the default behaviour where the Lambda's name is used.

Signed-off-by: Stefan Wessels Beljaars <[email protected]>
  • Loading branch information
stefanwb committed Oct 24, 2023
1 parent 6882b88 commit 5212ad3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ locals {
create_policy = var.create_policy != null ? var.create_policy : var.role_arn == null
dead_letter_config = var.dead_letter_target_arn != null ? { create : true } : {}
environment = var.environment != null ? { create : true } : {}
ephemeral_storage = var.ephemeral_storage_size != null ? { create : true } : {}
execution_type = var.subnet_ids == null ? "Basic" : "VPCAccess"
filename = var.filename != null ? var.filename : data.archive_file.dummy.output_path
source_code_hash = var.source_code_hash != null ? var.source_code_hash : var.filename != null ? filebase64sha256(var.filename) : null
tracing_config = var.tracing_config_mode != null ? { create : true } : {}
ephemeral_storage = var.ephemeral_storage_size != null ? { create : true } : {}
vpc_config = var.subnet_ids != null ? { create : true } : {}
}

Expand Down Expand Up @@ -74,7 +74,8 @@ resource "aws_security_group" "default" {
#checkov:skip=CKV2_AWS_5: False positive finding, the security group is attached.
count = var.subnet_ids != null ? 1 : 0

name = var.name
name = var.sg_name_prefix == null ? var.name : null
name_prefix = var.sg_name_prefix != null ? var.sg_name_prefix : null
description = "Security group for lambda ${var.name}"
vpc_id = data.aws_subnet.selected[0].vpc_id
tags = var.tags
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ variable "security_group_egress_rules" {
}
}

variable "sg_name_prefix" {
type = string
default = null
description = "An optional prefix that will be used to create an unique name of the security group. If not provided `var.name` will be used"
}

variable "source_code_hash" {
type = string
default = null
Expand Down

0 comments on commit 5212ad3

Please sign in to comment.