修改 Lambda 函数配置时出错:Lambda 和 VPC 的 ValidationException

error modifying Lambda Function configuration : ValidationException with Lambda and VPC

我正在使用它的 AWS 模块在 terraform 中构建一个 lambda,我的代码如下:

module "lambda_function" {

# * Lambda module configs
  source  = "terraform-aws-modules/lambda/aws"
  version = "3.0.0"

  # * Lambda Configs
  function_name = "${var.function_name}-${var.env}"
  description   = "My Project"
  handler       = local.constants.lambda.HANDLER
  runtime       = local.constants.lambda.VERSION
  memory_size                       = 128
  cloudwatch_logs_retention_in_days = 14
  source_path               = "./function/"
  timeout                   = local.constants.lambda.TIMEOUT
  create_async_event_config = true
  maximum_retry_attempts    = local.constants.lambda.RETRIES_ATTEMPT
    
  layers = [
    data.aws_lambda_layer_version.layer_requests.arn
  ]

  environment_variables = {
    AWS_ACCOUNT        = var.env
    SLACK_HOOK_CHANNEL = var.SLACK_HOOK_CHANNEL
  }

  tags = {
    Name = "${var.function_name}-${var.env}"
  }

  trusted_entities = local.constants.lambda.TRUSTED_ENTITIES
}

此代码运行良好,lambda 已部署。现在我需要将 lambda 放入 VPC 中。当我在资源块中添加以下代码时,出现错误 error modifying Lambda Function (lambda_name) configuration : ValidationException: │ status code: 400, request id: de2641f6-1125-4c83-87fa-3fe32dee7b06 │ │ with module.lambda_function.aws_lambda_function.this[0], │ on .terraform/modules/lambda_function/main.tf line 22, in resource "aws_lambda_function" "this": │ 22: resource "aws_lambda_function" "this" {

vpc 的代码是:

# * VPC configurations
  vpc_subnet_ids         = ["10.21.0.0/26", "10.21.0.64/26", "10.21.0.128/26"]
  vpc_security_group_ids = ["sg-ffffffffff"] # Using a dummy value here
  attach_network_policy  = true

如果我在 AWS 控制台中使用相同的值并在 VPC 中部署 lambda,它工作正常。

有人可以帮忙吗?

您必须提供有效的子网 ID,而不是 CIDR 范围。所以而不是

vpc_subnet_ids         = ["10.21.0.0/26", "10.21.0.64/26", "10.21.0.128/26"]

应该是

vpc_subnet_ids         = ["subnet-asfid1", "subnet-asfid2", "subnet-as4id1"]