【问题标题】:How to attach AWS Lambda fn to EXISTING vpc using terraform?如何使用 terraform 将 AWS Lambda fn 附加到现有 vpc?
【发布时间】:2022-01-12 04:21:29
【问题描述】:

TLDR:我们使用 Terraform 部署 Lambda 函数。新的 lambda 需要将 VPC 附加到现有 VPC。如何在 terraform 中定义此网络附件?我当前的解决方案通过了所有 terraform 步骤,但是在控制台中检查我的 Lambda 时,它没有连接到任何 VPC。

我发现this article Deploy AWS Lambda to VPC with Terraform 很有见地,但该示例涉及添加一个 VPC(带有子网、安全组等),而不是附加到现有 VPC,现有子网、安全组等

这是我目前的解决方案。从我项目的 main.tf 我调用一个模块...

module "lambda" {
    source = "git::https://corpsource.io/corp-cloud-platform-team/corpcloudv2/terraform/lambda-modules.git?ref=dev" 
    lambda_name = var.name
    lambda_role = "arn:aws:iam::${var.ACCOUNT}:role/${var.lambda_role}"
    lambda_handler = var.handler
    lambda_runtime = var.runtime
    default_lambda_timeout = var.timeout
    ACCOUNT = var.ACCOUNT
    vpc_subnet_ids = "${var.SUBNET_IDS}"
    vpc_security_group_ids = "${var.SECURITY_GROUP_IDS}"
}

这是模块:

resource "aws_lambda_function" "lambda_function" {
  filename         = "lambda_package.zip"
  function_name    = var.lambda_name
  role             = var.lambda_role
  handler          = var.lambda_handler
  runtime          = var.lambda_runtime
  memory_size      = 256
  timeout          = var.default_lambda_timeout
  source_code_hash = filebase64sha256("lambda_code/lambda_package.zip")
  vpc_config {
    subnet_ids = var.vpc_subnet_ids
    security_group_ids = var.vpc_security_group_ids
  }
}

它顺利通过了所有 Terraform 步骤,但似乎没有将我的 Lambda 附加到 VPC。我做错了什么?

提前致谢。

更新:

Terraform Plan 的输出:

$ terraform plan
Acquiring state lock. This may take a few moments...
module.lambda.aws_lambda_function.lambda_function: Refreshing state... [id=create-vault-entry]
module.lambda_iam.aws_iam_policy.base_policy: Refreshing state... [id=arn:aws:iam::############:policy/create-vault-entry-role]
module.lambda_iam.aws_iam_role.module_role: Refreshing state... [id=create-vault-entry-role]
module.lambda_iam.aws_iam_role_policy_attachment.lambda_attach: Refreshing state... [id=create-vault-entry-role-############################]
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  ~ update in-place
Terraform will perform the following actions:
  # module.lambda.aws_lambda_function.lambda_function will be updated in-place
  ~ resource "aws_lambda_function" "lambda_function" {
        id                             = "create-vault-entry"
      ~ last_modified                  = "2022-01-11T19:48:18.000+0000" -> (known after apply)
      ~ source_code_hash               = "g/hash/hash=" -> "hash/hash"
        tags                           = {}
        # (18 unchanged attributes hidden)
        # (2 unchanged blocks hidden)
    }
Plan: 0 to add, 1 to change, 0 to destroy.
Warning: Interpolation-only expressions are deprecated
  on main.tf line 3, in locals:
   3:     vault_HOST = "${var.vault_HOST}",
Terraform 0.11 and earlier required all non-constant expressions to be
provided via interpolation syntax, but this pattern is now deprecated. To
silence this warning, remove the "${ sequence from the start and the }"
sequence from the end of this expression, leaving just the inner expression.
Template interpolation syntax is still used to construct strings from
expressions when the template includes multiple interpolation sequences or a
mixture of literal strings and interpolations. This deprecation applies only
to templates that consist entirely of a single interpolation sequence.
(and 5 more similar warnings elsewhere)

【问题讨论】:

  • 我建议将所有内容移至 TF。这种类型的混合 IaC 很容易出错。
  • @208_man vpc_subnet_idsvpc_security_group_ids 变量的值是多少?它们是空的吗?
  • @208_man 您的subnet_idssecurity_group_ids 值是否在list(string) 中?
  • @MarkoE,它们不是空的。它们是字符串列表。
  • @Ravichandran,是的列表(字符串)

标签: amazon-web-services aws-lambda terraform terraform-provider-aws


【解决方案1】:

您似乎正在将列表转换为字符串。 Lambda VPC subnet_idssecurity_group_ids 属性需要一个列表,而不是字符串。我真的不确定您当前的代码是如何工作的而没有报告任何错误。

看起来你需要改变这个:

vpc_subnet_ids = "${var.SUBNET_IDS}"
vpc_security_group_ids = "${var.SECURITY_GROUP_IDS}"

到这里:

vpc_subnet_ids = var.SUBNET_IDS
vpc_security_group_ids = var.SECURITY_GROUP_IDS

【讨论】:

  • 这是一个很棒的观察。由于这些变量的类型为list(string),因此我的错误语法意味着我正在传递字符串化列表。但是,正如您所建议的那样,切换到正确的语法并没有改变结果。 TF 步骤全部通过,Lambda 配置中没有 VPC 附件。令人费解!
  • 您所说的“步骤全部通过”是什么意思?也许您应该在问题中包含terraform plan 的输出。
  • 我想我正在混合使用 terraform 术语和 gitlab ci 术语。我的 ci 脚本运行 terrform init、plan、apply 等。我将 terraform plan 的输出添加到我原来的问题中。感谢您的意见。
  • 我真的不认为您问题中的 Terraform 代码是 GitLab 正在运行的代码。此外,在提交任何内容之前,您真的应该在本地运行 terraform plan
猜你喜欢
  • 2021-07-29
  • 2023-01-24
  • 2021-01-27
  • 1970-01-01
  • 2017-04-16
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
相关资源
最近更新 更多