0

I am new to Terraform, I am having difficulty in configuring vpc_config in Lambda function.

main.tf

resource "aws_lambda_function" "lambda" {
  function_name = var.function_name
  s3_bucket = var.s3_bucket_name
  s3_key = var.s3_key
    vpc_config {
      security_group_ids = ["${var.lambda_security_group_id}"]
      subnet_ids         = ["${split(",", var.lambda_subnet_id)}"]
    }
  #source_code_hash = data.archive_file.zip.output_base64sha256
  role    = aws_iam_role.iam_for_lambda.arn
  handler = "welcome.lambda_handler"
  runtime = "python3.9"
  timeout = var.timeout
  memory_size = var.lambda_mem_size
}

variables.tf

variable "lambda_security_group_id" {
  type = list(string)
}

variable "lambda_subnet_id" {
  type = list(string)
}

terraform.tfvars

lambda_security_group_id = ["sg-0aabcef7795c7e092", "sg-0f218ddc9fb47341d"]
lambda_subnet_id = ["subnet-0d786711ca50ab0f7", "subnet-06341798f99bc9849"]

Please guide me from here.

Yefet
  • 2,010
  • 1
  • 10
  • 19
Sandeep SK
  • 11
  • 2

1 Answers1

1

I think you need something like this, since the variables are already a list of strings. It could be a good idea to rename your variables to the plural form since they are lists: lambda_security_group_ids and lambda_subnet_ids.

  vpc_config {
      security_group_ids = var.lambda_security_group_id
      subnet_ids         = var.lambda_subnet_id
  }

More info can be found here.

lennart
  • 121
  • 5
  • HI lennart, this is not working either. I get below error message – Sandeep SK Sep 08 '22 at 12:00
  • Below is the error message HI lennart, this is not working either. I get below error message, ``` │ Error: Incorrect attribute value type │ on main.tf line 77, in resource "aws_lambda_function" "lambda": │ 77: security_group_ids = [var.lambda_security_group_ids] │ ├──────────────── │ │ var.lambda_security_group_ids is a list of string, known only after apply │ Inappropriate value for attribute "security_group_ids": element 0: string required. │``` – Sandeep SK Sep 08 '22 at 12:07
  • You are still using the square brackets based on the error you are getting. So you need to drop them, as @lennart has written in his answer. – Marko E Sep 08 '22 at 12:57