如何使用数据“ AWS_VPC_ENDPOINT”检索多个端点。资源?

发布于 2025-02-01 09:58:46 字数 623 浏览 4 评论 0原文

错误:“匹配的多个VPC端点”

我正在使用一个数据“ AWS_VPC_ENDPOINT”来根据VPC ID检索多个端点ID。如何检索这些终点以在另一个资源中引用它们?还是可以从此数据资源中检索多端点。有什么建议吗?或建议将不胜感激。这是代码段。 Count.Index已在资源“ AWS_ROUTE”中正确考虑到我的重点是检索多个端点以添加到AWS_ROUTE中。

data "aws_vpc_endpoint" "firewall-endpoints" { 
  vpc_id = aws_vpc.vpc.id

  filter {
    name = "tag:Example"
    values = [true]
  }
}

resource "aws_route" "example" {
  count                  = var.number_azs
  route_table_id         = aws_route_table.example[count.index].id
  destination_cidr_block = var.tgw_aws_route[0]
  vpc_endpoint_id = data.aws_vpc_endpoint_service.firewall-endpoints.id
}

Error: “multiple VPC Endpoints matched”

I am using a data “aws_vpc_endpoint” to retrieve multiple endpoint IDs based on the vpc ID. How can I retrieve these endpoints to reference them in another resource? Or is it possible to retrieve multiple endpoint from this data resource. Any suggestions? Or advice would be much appreciated. Here is the code snippet. The count.index has been accounted for correctly already in resource "aws_route" now I am focused on retrieving multiple endpoints to add to the aws_route.

data "aws_vpc_endpoint" "firewall-endpoints" { 
  vpc_id = aws_vpc.vpc.id

  filter {
    name = "tag:Example"
    values = [true]
  }
}

resource "aws_route" "example" {
  count                  = var.number_azs
  route_table_id         = aws_route_table.example[count.index].id
  destination_cidr_block = var.tgw_aws_route[0]
  vpc_endpoint_id = data.aws_vpc_endpoint_service.firewall-endpoints.id
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳公公是暖光 2025-02-08 09:58:47

此数据源的参数是查询可用VPC端点的过滤器。给定的过滤器必须完全匹配一个VPC端点,其数据将被导出为属性。

如果您想将VPC端点用于多个服务,则需要为每个服务创建一个数据源。可以用 for_each 来完成此操作。


更新:我不确定如何设置端点,但是您需要找到一种独特的方式来引用它们。在这里使用for_each的一个示例可能是这样的:


data "aws_region" "current" {}

locals {
  services = {
    s3  = "com.amazonaws.${data.aws_region.current.name}.s3"
    ssm = "com.amazonaws.${data.aws_region.current.name}.ssm"
  }
}

data "aws_vpc_endpoint" "services" {
  for_each = local.services

  vpc_id = aws_vpc.vpc.id
  service_name = each.value
}

然后使用端点,您可以将其称为eg data.aws_vpc_endpoint.services.services [“ s3”]。id 。而且,如果您想循环循环它们,则可以再次参考local.services字典。

The documentation is pretty explicit:

The arguments of this data source act as filters for querying the available VPC endpoints. The given filters must match exactly one VPC endpoint whose data will be exported as attributes.

If you want to use VPC endpoints for multiple services, you'll need to create a data source for each one. This could be done concisely with for_each.


Update: I'm not sure how your endpoints are set up, but you need to find a unique way to refer to them. An example of using for_each here could look like this:


data "aws_region" "current" {}

locals {
  services = {
    s3  = "com.amazonaws.${data.aws_region.current.name}.s3"
    ssm = "com.amazonaws.${data.aws_region.current.name}.ssm"
  }
}

data "aws_vpc_endpoint" "services" {
  for_each = local.services

  vpc_id = aws_vpc.vpc.id
  service_name = each.value
}

To then use the endpoint, you can refer to it as e.g. data.aws_vpc_endpoint.services["s3"].id. And if you want to loop over them, you can again refer to the local.services dictionary.

待天淡蓝洁白时 2025-02-08 09:58:47

您可以尝试

data "aws_resourcegroupstaggingapi_resources" "test" {

  tag_filter {
    key    = "Example"
    values = ["tag-value-1", "tag-value-2"]
  }
}

您可以添加,但我不确定VPC端点的类型是什么。

You can try aws_resourcegroupstaggingapi_resources to return multiple resources that have specific tags:

data "aws_resourcegroupstaggingapi_resources" "test" {

  tag_filter {
    key    = "Example"
    values = ["tag-value-1", "tag-value-2"]
  }
}

you can add resource_type_filters but I'm not sure what is the type for VPC endpoints.

我们的影子 2025-02-08 09:58:47

作为格式作为答案的答案很烂,当将其作为评论以答案:

data "aws_resourcegroupstaggingapi_resources" "vpce" {
  resource_type_filters = ["ec2:vpc-endpoint"]

  tag_filter {
    key    = "Name"
    values = ["dev-vpc-endpoint-dev-use1-msk-shared-cluster"]
  }
}
    
data "aws_vpc_endpoint" "vpce" {
  for_each = toset([for arn in data.aws_resourcegroupstaggingapi_resources.vpce.resource_tag_mapping_list[*].resource_arn : reverse(split("/", arn))[0]])

  id    = each.value
  state = "available"
}

Posting as an answer as formatting sucks when posted as comment to an answer:

data "aws_resourcegroupstaggingapi_resources" "vpce" {
  resource_type_filters = ["ec2:vpc-endpoint"]

  tag_filter {
    key    = "Name"
    values = ["dev-vpc-endpoint-dev-use1-msk-shared-cluster"]
  }
}
    
data "aws_vpc_endpoint" "vpce" {
  for_each = toset([for arn in data.aws_resourcegroupstaggingapi_resources.vpce.resource_tag_mapping_list[*].resource_arn : reverse(split("/", arn))[0]])

  id    = each.value
  state = "available"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文