From d40a79c6ddfa02c338a6ba12f793c601be14f555 Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Mon, 12 Aug 2024 16:17:08 +0100 Subject: [PATCH 1/5] allow default sg by default --- main.tf | 12 +++++++++++- variables.tf | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/main.tf b/main.tf index d6f0442..e12bb16 100644 --- a/main.tf +++ b/main.tf @@ -1,3 +1,13 @@ +locals { + security_group_ids = var.security_group_ids != null ? var.security_group_ids : [aws_security_group.default[0].id] +} + +data "aws_security_group" "default" { + count = var.security_group_ids != null ? 0 : 1 + name = "${terraform.workspace}-default-lambda-sg" +} + + resource "aws_lambda_function" "lambda_function" { image_uri = var.image_uri s3_bucket = var.s3_bucket @@ -25,7 +35,7 @@ resource "aws_lambda_function" "lambda_function" { vpc_config { subnet_ids = var.subnet_ids - security_group_ids = var.security_group_ids + security_group_ids = local.security_group_ids } environment { diff --git a/variables.tf b/variables.tf index d2fe7b9..ada7dbb 100644 --- a/variables.tf +++ b/variables.tf @@ -60,7 +60,7 @@ variable "subnet_ids" { variable "security_group_ids" { type = list(string) description = "The VPC security groups assigned to the Lambda." - default = [] + default = null } variable "datadog_log_subscription_arn" { From edb53f3602a97a211f65f2b9422d6e419ff78f32 Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 15 Aug 2024 10:47:20 +0100 Subject: [PATCH 2/5] add use_default_sg variable --- main.tf | 2 +- variables.tf | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/main.tf b/main.tf index e12bb16..8ebf7ee 100644 --- a/main.tf +++ b/main.tf @@ -3,7 +3,7 @@ locals { } data "aws_security_group" "default" { - count = var.security_group_ids != null ? 0 : 1 + count = var.use_default_sg == true ? 1 : 0 name = "${terraform.workspace}-default-lambda-sg" } diff --git a/variables.tf b/variables.tf index ada7dbb..620a78e 100644 --- a/variables.tf +++ b/variables.tf @@ -136,4 +136,10 @@ variable "architectures" { type = list(string) description = "Lambda architectures to support." default = ["x86_64"] +} + +variable "use_default_sg" { + type = bool + description = "Use default security group" + default = false } \ No newline at end of file From 9fdde33158c666571db6e99ccb5b20c3be0970a3 Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 15 Aug 2024 11:17:15 +0100 Subject: [PATCH 3/5] renamed use_default_security_group variabel --- main.tf | 4 ++-- variables.tf | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main.tf b/main.tf index 8ebf7ee..ec0dc85 100644 --- a/main.tf +++ b/main.tf @@ -1,9 +1,9 @@ locals { - security_group_ids = var.security_group_ids != null ? var.security_group_ids : [aws_security_group.default[0].id] + security_group_ids = var.use_default_security_group == false ? var.security_group_ids : [data.aws_security_group.default[0].id] } data "aws_security_group" "default" { - count = var.use_default_sg == true ? 1 : 0 + count = var.use_default_security_group == true ? 1 : 0 name = "${terraform.workspace}-default-lambda-sg" } diff --git a/variables.tf b/variables.tf index 620a78e..6e29356 100644 --- a/variables.tf +++ b/variables.tf @@ -138,7 +138,7 @@ variable "architectures" { default = ["x86_64"] } -variable "use_default_sg" { +variable "use_default_security_group" { type = bool description = "Use default security group" default = false From e26c56740facd188f669c4f02930f91087d3aa9e Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 15 Aug 2024 11:25:26 +0100 Subject: [PATCH 4/5] added vpc_id variable --- main.tf | 1 + variables.tf | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/main.tf b/main.tf index ec0dc85..69e1433 100644 --- a/main.tf +++ b/main.tf @@ -5,6 +5,7 @@ locals { data "aws_security_group" "default" { count = var.use_default_security_group == true ? 1 : 0 name = "${terraform.workspace}-default-lambda-sg" + vpc_id = var.vpc_id } diff --git a/variables.tf b/variables.tf index 6e29356..09e9edd 100644 --- a/variables.tf +++ b/variables.tf @@ -142,4 +142,10 @@ variable "use_default_security_group" { type = bool description = "Use default security group" default = false -} \ No newline at end of file +} + +variable "vpc_id" { + type = string + description = "The VPC ID in which the Lambda runs." + default = null +} From 5c9b6061883aae236b28e9f88b5fc983c12dfd2d Mon Sep 17 00:00:00 2001 From: Marcio Goda Date: Thu, 15 Aug 2024 11:52:36 +0100 Subject: [PATCH 5/5] add dynamic block --- main.tf | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/main.tf b/main.tf index 69e1433..92e4127 100644 --- a/main.tf +++ b/main.tf @@ -34,9 +34,12 @@ resource "aws_lambda_function" "lambda_function" { } } - vpc_config { - subnet_ids = var.subnet_ids - security_group_ids = local.security_group_ids + dynamic vpc_config { + for_each = local.security_group_ids != null ? [1] : [] + content { + subnet_ids = var.subnet_ids + security_group_ids = local.security_group_ids + } } environment {