首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将变量从一个嵌套模块传递到另一个嵌套模块(terraform和AWS)

将变量从一个嵌套模块传递到另一个嵌套模块(terraform和AWS)
EN

Stack Overflow用户
提问于 2022-09-19 22:32:56
回答 1查看 112关注 0票数 0

编辑以包括根配置:

我有一个名为ecs的terraform模块,它本身就是我在根级调用的一个模块。在ecs模块中,我识别了30个不同的服务,并将其划分为每个服务自己的tf文件。

根目录看起来像(只显示一个有问题的模块,其余模块工作得很好):

代码语言:javascript
复制
module "aws-ecs-app" {
  source = "/Users/bshutter/Dev/ghorg/kion/delivery-support/terraform-modules/terraform-aws-ecs"
  # version     = "0.0.1"
  install_id                                   = local.install_id
  private_subnets                              = module.aws-networking.private_subnets
  vpc_id                                       = module.aws-networking.vpc_id
  private_security_groups                      = [module.aws-networking.private_security_group]
  public_security_groups                       = [module.aws-networking.public_security_group]
  region                                       = var.region
  namespace                                    = local.namespace
  internal_balancer_arn                        = module.terraform-aws-ecs-load-balancers.internal_balancer_arn
  external_balancer_arn                        = module.terraform-aws-ecs-load-balancers.external_balancer_arn
  task_role_arn                                = module.aws-ecs-iam.task_role_arn
  execution_role_arn                           = module.aws-ecs-iam.execution_role_arn
  s3_bucket_envfile                            = module.aws-ecs-lambda-functions.s3_bucket_envfile
  ulb_http                                     = module.terraform-aws-ecs-load-balancers.ulb_http
  ulb_https                                    = module.terraform-aws-ecs-load-balancers.ulb_https
  external_lb_https_webapi                     = module.terraform-aws-ecs-load-balancers.external_lb_https_webapi
  aws_lb_target_group_lb_https                 = module.terraform-aws-ecs-load-balancers.aws_lb_target_group_lb_https
  aws_lb_target_group_ulb_http                 = module.terraform-aws-ecs-load-balancers.aws_lb_target_group_ulb_http
  aws_lb_target_group_external_lb_https_webapi = module.terraform-aws-ecs-load-balancers.aws_lb_target_group_external_lb_https_webapi
  aws_lb_target_group_external_lb_http_webapi  = module.terraform-aws-ecs-load-balancers.aws_lb_target_group_external_lb_http_webapi
  aws_lb_target_group_api                      = module.terraform-aws-ecs-load-balancers.aws_lb_target_group_api
  internal_load_balancer_app_url               = module.terraform-aws-ecs-load-balancers.internal_load_balancer_app_url
  external_load_balancer_app_url               = module.terraform-aws-ecs-load-balancers.external_load_balancer_app_url
}

在每个tf文件中,我调用了其他4个模块。模块所在的文件夹/文件结构如下所示:

代码语言:javascript
复制
terraform-modules 
│
└───terraform-aws-ecs
│      service1.tf
│      service2.tf
│      service3.tf
│      variables.tf
│      outputs.tf
│ 
└───terraform-aws-ecs-service
    │   main.tf
    │   variables.tf

例如,4个模块中的一个用于ecs-服务,在本例中:帐户创建:

代码语言:javascript
复制
module "ecs_service_accountcreation" {
  source           = "/Users/bshutter/Dev/terraform-modules/terraform-aws-ecs-service"
  component_name   = var.service_accountcreation_name
  container_port   = var.container_port_accountcreation
  target_group_arn = aws_lb_target_group.accountcreation.arn
  task_definition  = module.taskdefinition_accountcreation
}

我遇到了一些问题,在这里设置这些变量,并在其他模块的variables.tf文件中添加一个部分,并期望该变量通过,但事实并非如此。

例如,terraform-aws服务模块的main.tf如下所示:

代码语言:javascript
复制
resource "aws_ecs_service" "service" {
  cluster                            = aws_ecs_cluster.app.arn
  deployment_maximum_percent         = var.service_deployment_max_percent
  deployment_minimum_healthy_percent = var.service_deployment_min_healthy_percent
  desired_count                      = var.service_desired_count
  enable_ecs_managed_tags            = var.service_enable_ecs_managed_tags
  enable_execute_command             = var.enable_ecs_exec
  health_check_grace_period_seconds  = var.service_health_check_grace_period_seconds
  launch_type                        = var.service_launch_type
  name                               = "${var.namespace}-${var.component_name}-${var.service_label}-${var.install_id}"
  platform_version                   = var.service_platform_version
  propagate_tags                     = var.service_propagate_tags
  scheduling_strategy                = var.service_scheduling_strategy
  task_definition                    = var.task_definition

  deployment_circuit_breaker {
    enable   = var.service_deployment_circuit_breaker_enabled
    rollback = var.service_deployment_circuit_breaker_rollback
  }

  deployment_controller {
    type = var.service_deployment_controller_type
  }

  load_balancer {
    container_name   = var.component_name
    container_port   = var.container_port
    target_group_arn = var.target_group_arn
  }

  network_configuration {
    assign_public_ip = var.service_assign_public_ip
    security_groups  = var.private_security_groups
    subnets          = var.private_subnets
  }
}

variables.tf看起来是这样的:

代码语言:javascript
复制
variable "enable_ecs_exec" {
  type        = bool
  default     = true
  description = "This will set 'EnableExecuteCommand' to true on ECS services. This allows using the AWS CLI to run commands on running containers or create interactive sessions. This also attaches an additional IAM policy to ensure ECS tasks have permissions to create data channels through SSM."
}

variable "service_launch_type" {
  type        = string
  description = "Launch Type for ECS Service"
  default     = "FARGATE"
}

variable "service_deployment_max_percent" {
  type    = number
  default = 200
}

variable "service_deployment_min_healthy_percent" {
  type    = number
  default = 100
}

variable "service_desired_count" {
  type    = number
  default = 1
}

variable "service_enable_ecs_managed_tags" {
  type    = bool
  default = false
}

variable "service_health_check_grace_period_seconds" {
  type    = number
  default = 0
}

variable "service_platform_version" {
  type    = string
  default = "LATEST"
}

variable "service_propagate_tags" {
  type    = string
  default = "NONE"
}

variable "service_scheduling_strategy" {
  type    = string
  default = "REPLICA"
}

variable "service_deployment_circuit_breaker_enabled" {
  type    = bool
  default = false
}

variable "service_deployment_circuit_breaker_rollback" {
  type    = bool
  default = false
}

variable "service_deployment_controller_type" {
  type    = string
  default = "ECS"
}
variable "service_assign_public_ip" {
  type    = bool
  default = false
}

variable "service_label" {
  type        = string
  default     = "ECSService"
  description = "Name of Service - Primarily Used in Tags"
}

variable "target_group_arn" {
  type = string
}

variable "container_port" {
  type = string
}

variable "component_name" {
  type = string
}

variable "task_definition" {
  type = string
}

当我运行terraform计划时,我会得到错误。

代码语言:javascript
复制
│ Error: Unsupported argument
│ 
│   on .terraform/modules/aws-ecs-app/service-accountcreation.tf line 3, in module "ecs_service_accountcreation":
│    3:   component_name   = var.service_accountcreation_name
│ 
│ An argument named "component_name" is not expected here.

我在调用这4个模块的模块和包含ecs服务代码的模块中特别为component_name设置了一个变量。

我在寻找这两行地形中的变量:

代码语言:javascript
复制
  name  = "${var.namespace}-${var.component_name}-${var.service_label}-${var.install_id}"
代码语言:javascript
复制
  load_balancer {
    container_name   = var.component_name
    container_port   = var.container_port
    target_group_arn = var.target_group_arn
  }

terraform验证工作在terraform-aws-service目录中,但不在根目录中,我在根目录中调用引用“子模块”的模块。

EN

回答 1

Stack Overflow用户

发布于 2022-09-20 13:18:37

变量根本不“通过”模块。它们的作用域始终是特定的模块。如果要在多个模块中使用该变量,则需要在每个需要使用该变量的模块中将该变量声明为输入。换句话说,将其添加到aws-ecs-app模块中:

代码语言:javascript
复制
variable "component_name" {
  type = string
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73779888

复制
相关文章

相似问题

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