【问题标题】:Referencing variables in nested modules in terraform在 terraform 的嵌套模块中引用变量
【发布时间】:2021-08-08 23:58:56
【问题描述】:

我正在使用 terraform 开发用于用户池身份验证的 lambda 授权器,我需要将环境变量从 src>modules>application-services>modules>application-service>variables.tf 动态设置为 src>modules>lambda-auth>variables.tf。我不确定如何引用它,我已在 output.tf 中为application-services>modules>application-service>variables.tf 声明了它们。这是我的文件结构。

????src
 ┣ ????modules
 ┃ ┣ ????application-services
 ┃ ┃ ┣ ????modules
 ┃ ┃ ┃ ┗ ????application-service
 ┃ ┃ ┃ ┃ ┣ ????api.tf
 ┃ ┃ ┃ ┃ ┣ ????outputs.tf
 ┃ ┃ ┃ ┃ ┣ ????providers.tf
 ┃ ┃ ┃ ┃ ┣ ????stage-variables.tf
 ┃ ┃ ┃ ┃ ┣ ????stages.tf
 ┃ ┃ ┃ ┃ ┗ ????variables.tf
 ┃ ┃ ┣ ????application-service.tf
 ┃ ┃ ┣ ????providers.tf
 ┃ ┃ ┗ ????variables.tf
 ┃ ┣ ????lambda-auth
 ┃ ┃ ┣ ????resource
 ┃ ┃ ┃ ┗ ????lambda-authorizer.zip
 ┃ ┃ ┣ ????src
 ┃ ┃ ┃ ┗ ????auth.go
 ┃ ┃ ┣ ????lambda.tf
 ┃ ┃ ┣ ????providers.tf
 ┃ ┃ ┗ ????variables.tf
 ┣ ????application-services.tf
 ┣ ????main.tf
 ┣ ????outputs.tf
 ┣ ????providers.tf
 ┣ ????remote.tf
 ┗ ????variables.tf
 ┗ ????lambda-main.tf

这是我的 src>modules>application>services>modules>application-service>outputs.tf 文件

output "user-pool-id" {
  value = var.service.app_name
}

这是我的 src>modules>application>services>modules>application-service>variables.tf 文件

variable "service" {
  description = "The service which we want to deploy into the gateway"
  type = object({
    name           = string
    app_name       = string
    route          = string
    attributes     = map(string)
    user_pool_arns = list(string)
    environments = list(object({
      name      = string
      vpcLinkId = string
      domainId  = string
      uri       = string
    }))
  })
}

我想获取“app_name”的值,并在src>modules>lambda-auth>lambda.tf 中使用它,我想用那个“app_name”代替“var.dev_appid”,我已经注意了其他方面,比如创建IAM 角色和策略。

resource "aws_lambda_function" "authorizer_lambda_parser" {
  filename      = data.archive_file.lambda_resources_zip.output_path
  function_name = "lambda-authorizer"
  handler       = "auth.go"
  runtime       = "go1.x"
  role          = aws_iam_role.lambda_authorizer_parser_role.arn

  source_code_hash = data.archive_file.lambda_resources_zip.output_base64sha256
  environment {
    variables = {
      Dev_Region = var.dev_region
      Dev_AppID  = var.dev_appid
      Dev_Stage  = var.dev_stage
      Dev_UserPoolId = var.dev_userpoolid
      Dev_CognitoClients = var.dev_cognitoclient
      Prod_Region = var.prod_region
      Prod_AppId  = var.prod_appid
      Prod_Stage  = var.prod_stage
      Prod_UserPoolId = var.prod_userpoolid
      Prod_CognitoClients = var.prod_cognitoclient
    }
  }
}

这是我的src>modules>lambda-auth>variables.tf 文件

variable "dev_region" {
    default = ""
    type    = string
    description = "Region for Dev Environment"
}

variable "dev_appid" {
    default = ""
    type    = string
    description = " App ID for Dev Environment"
}
variable "dev_stage" {
    default = ""
    type    = string
    description = " Stage for Dev Environment"
}
variable "dev_userpoolid" {
    default = ""
    type    = string
    description = " User Pool ID for Dev Environment"
}
variable "dev_cognitoclient" {
    default = ""
    type    = string
    description = " Cognito Client ID for Dev Environment"
}
variable "prod_region" {
    default = ""
    type    = string
    description = "Region for Prod Environment"
}
variable "prod_appid" {
    default = ""
    type    = string
    description = " App ID for Prod Environment"
}
variable "prod_stage" {
    default = ""
    type    = string
    description = " Stage for Prod Environment"
}
variable "prod_userpoolid" {
    default = ""
    type    = string
    description = " User Pool ID for Prod Environment"
}
variable "prod_cognitoclient" {
    default = ""
    type    = string
    description = " Cognito Client ID for Prod Environment"
}

这是我的 lambda-main.tf 文件:

module "lambda-auth" {
  source = "lambda-auth"

  prod_userpoolid = module.application-services.user-pool-id
}

这是我的src>application-serivces.tf 文件:
# 我们检索每个服务的必要信息,包括:user_pool_arns、vpcLinkId、domainId 当地人{ app_service_input = { 对于 app_file,local.app_object_list 中的应用程序:application.name => flatten([ 对于 application.services 中的服务:[合并(服务, { app_name = 应用程序名称 user_pool_arns = [对于 application.user_pools 中的 user_pool:module.iam-pools[user_pool].results.pool.arn] 环境 = [对于 service.environments 中的环境: { 名称 = 环境名称 vpcLinkId = module.gateway-link[environment.link].results.vpcLinkId domainId = module.gateway-domain[app_file].results.domain[application.domains.service][environment.name] uri = environment.uri }] })] ]) } }

module "application-services" {
  source = "./modules/application-services"

  providers = {
    aws.gateway = aws.networking
  }

  for_each = local.app_service_input

  application_services = each.value
}

我不知道如何从一个模块引用到另一个模块,在此先感谢。

【问题讨论】:

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


    【解决方案1】:

    您根本无法将值直接从一个模块引用到另一个模块。声明module 的级别是唯一可以访问模块输出的级别。要将这些值传递到其他级别,您还必须将该值声明为application-services 模块的输出,这将使其在main 中可用。然后为lambda 模块声明一个输入变量,并让main 将值传递给lambda 模块。


    application-services/outputs.tf

    output "user-pool-id" {
      value = module.application-service.user-pool-id
    }
    

    main.tf

    module "lambda-auth" {
      source = "lambda-auth"
    
      prod_userpoolid = module.application-services.user-pool-id
    }
    

    【讨论】:

    • 我已经在 output.tf 中声明了这个变量,理论上这意味着我应该能够在我的 lambda.tf 中使用它,但是你说我需要为 lambda 声明一个输入变量的最后一部分模块,并让 main 将值传递给 lambda 模块'。我怎么做。感谢您的帮助。
    • 不,您在 application-service 的 outputs.tf 中声明了它,这意味着它目前在 application-services 中可用,其他任何地方都没有。您缺少子模块的输出仅对调用模块可用的部分。您首先需要将 output.tf 添加到 application-services 并从那里再次输出该值,这将使其在 main 中可用。然后你需要将它作为输入传递给lambda,就像你已经将其他输入传递给lambda一样。
    • @figuring 查看我添加到答案中的代码示例
    • 很抱歉,这又是一个初学者的问题,但我已经在“application-services”下创建了 outputs.tf 文件,但我不确定如何声明它,是否应该再次使用相同的代码如在“应用程序服务”输出.tf 中。它是否也会影响要在 lambda.tf 中声明的变量是变量映射的代码。
    • @figuring 请查看我的更新答案,我给了您需要添加到该 output.tf 文件中的确切代码。
    猜你喜欢
    • 2022-01-26
    • 2019-06-16
    • 2021-04-01
    • 1970-01-01
    • 2018-01-26
    • 2022-01-19
    • 2022-11-11
    • 2023-04-01
    • 2021-05-04
    相关资源
    最近更新 更多