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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

HI lennart, this is not working either. I get below error message
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. │```
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.