首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在terraform嵌套模块中引用变量

在terraform嵌套模块中引用变量
EN

Stack Overflow用户
提问于 2021-05-19 17:48:44
回答 1查看 1.2K关注 0票数 0

我正在使用terraform开发一个用于用户池身份验证的lambda授权程序,我需要动态地设置从src>modules>application-services>modules>application-service>variables.tfsrc>modules>lambda-auth>variables.tf的环境变量。我不知道如何引用它,我已经在outputs.tf for application-services>modules>application-service>variables.tf中声明了它们。这是我的文件结构。

代码语言:javascript
复制
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文件

代码语言:javascript
复制
output "user-pool-id" {
  value = var.service.app_name
}

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

代码语言:javascript
复制
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角色和策略。

代码语言:javascript
复制
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文件

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
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 = application.name user_pool_arns =[ application.user_pools中的user_pool :module.iam-info.gov.hk#en1#_info.gov.hk.app_name]app_name=[用于service.environments中的环境:{ name = environment.name vpcLinkId =service.environments domainId = module.gateway-domainapp_file.results.domainapplication.domains.service = environment.uri } }] }}

代码语言:javascript
复制
module "application-services" {
  source = "./modules/application-services"

  providers = {
    aws.gateway = aws.networking
  }

  for_each = local.app_service_input

  application_services = each.value
}

我不知道如何引用从一个模块到另一个模块,谢谢提前。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-19 17:58:53

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

application-services/outputs.tf

代码语言:javascript
复制
output "user-pool-id" {
  value = module.application-service.user-pool-id
}

main.tf

代码语言:javascript
复制
module "lambda-auth" {
  source = "lambda-auth"

  prod_userpoolid = module.application-services.user-pool-id
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67608378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档