首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Terrafom在模块中引用地图输出

Terrafom在模块中引用地图输出
EN

Stack Overflow用户
提问于 2022-02-13 06:30:37
回答 1查看 393关注 0票数 0

我有一个地形模块,如下所示

代码语言:javascript
复制
module "vpc" {
  source                      = "./modules"
  my_aws_account_id           = "<aws_account_id>"
  my_aws_vpc_id               = "<aws_vpc_id>"
  my_vpc_peering_connections  = {
    stage    = "<aws_stage_vpc_id>"
    preprod  = "<aws_preprod_vpc_id>"
  }
  my_vpc_route_table          = {
    default   = {
      name    = "test-default"
      routes  = [{
        cidr_block                  = "10.10.16.0/20"
        vpc_peering_connection_id   = module.vpc.op_vpc_peering_connections["stage"]
      }, {
        cidr_block                  = "10.10.32.0/20"
        vpc_peering_connection_id   = module.vpc.op_vpc_peering_connections["preprod"]
      }]
    }
  }
}

变量

代码语言:javascript
复制
variable "my_aws_account_id" {
  type  = string
}

variable "my_aws_vpc_id" {
  type  = string
}

variable "my_vpc_peering_connections" {
  type  = map(string)
}

variable "my_vpc_route_table" {
  type  = object({
    default = object({
      name    = string
      routes  = list(object({
        cidr_block                  = string
        vpc_peering_connection_id   = string
      }))
    })
  })
}

资源

代码语言:javascript
复制
resource "aws_vpc_peering_connection" "peering_connections" {
  for_each      = var.my_vpc_peering_connections

  peer_owner_id = var.my_aws_account_id
  peer_vpc_id   = each.value
  vpc_id        = var.my_aws_vpc_id
  tags          = {
    Name = each.key
  }

  timeouts {}
}

resource "aws_route_table" "default_route_table" {
  vpc_id    = var.my_aws_vpc_id
  tags      = {
    Name = var.my_vpc_route_table.default.name
  }

  dynamic "route" {
    for_each = var.my_vpc_route_table.default.routes
    content {
      cidr_block                  = lookup(route.value, "cidr_block")
      vpc_peering_connection_id   = lookup(route.value, "vpc_peering_connection_id")

      instance_id                 = ""
      gateway_id                  = ""
      carrier_gateway_id          = ""
      destination_prefix_list_id  = ""
      egress_only_gateway_id      = ""
      ipv6_cidr_block             = ""
      local_gateway_id            = ""
      nat_gateway_id              = ""
      network_interface_id        = ""
      transit_gateway_id          = ""
      vpc_endpoint_id             = ""
    }
  }

  depends_on = [
    aws_vpc_peering_connection.peering_connections
  ]
}

输出

代码语言:javascript
复制
output op_vpc_peering_connections {
  value = tomap({
    for name, details in aws_vpc_peering_connection.peering_connections : name => details.id
  })
}

但是,当我运行以下命令时,第一条成功,第二条由于错误而失败

代码语言:javascript
复制
> terraform import module.vpc.aws_vpc_peering_connection.peering_connections["stage"] <peering_connection_id>
> terraform import module.vpc.aws_vpc_peering_connection.peering_connections["preprod"] <peering_connection_id>

错误

代码语言:javascript
复制
╷
│ Error: Invalid index
│ 
│   on /Users/sarad/workspace/personal/terraform-for-each/main.tf line 33, in module "vpc":
│   33:         vpc_peering_connection_id   = module.vpc.op_vpc_peering_connections["preprod"]
│     ├────────────────
│     │ module.vpc.op_vpc_peering_connections is map of string with 1 element
│ 
│ The given key does not identify an element in this collection value.
╵

如果op_vpc_peering_connections中有一个以上的元素,并且我试图在模块中引用它们,比如module.vpc.op_vpc_peering_connections["stage"]module.vpc.op_vpc_peering_connections["preprod"],就会出现错误。

此外,我无法用terraform output调试输出,在这里我会收到以下警告

代码语言:javascript
复制
╷
│ Warning: No outputs found
│ 
│ The state file either has no outputs defined, or all the defined outputs are empty. Please define an output in your configuration with the `output` keyword and run `terraform
│ refresh` for it to become available. If you are using interpolation, please verify the interpolated value is not empty. You can use the `terraform console` command to assist.
╵
EN

回答 1

Stack Overflow用户

发布于 2022-02-13 15:45:31

如果您首先运行一个terraform plan,您将看到每个资源的确切名称。此时,您所要做的就是将这些名称复制/粘贴到plan输出中,并粘贴到terraform import命令中。这还允许您验证您认为已通过for_each配置的每个资源。这应该是您了解为什么module.vpc.aws_vpc_peering_connection.peering_connections["preprod"]不像您期望的那样存在的第一步。

此外,我无法用terraform输出调试输出,在这里我会收到以下警告

这是因为输出只存在于模块中,而terraform output只提供顶级输出。在Terraform代码的顶层,比如在带有module "vpc" { ... }声明的文件中,您需要添加一个顶级输出:

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

https://stackoverflow.com/questions/71098431

复制
相关文章

相似问题

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