簡體   English   中英

如何在 terraform 中傳遞不同值的列表

[英]How to pass a list of different values in terraform

我是 terraform 的新手,我正在嘗試使用 terraform 0.14.2 創建一個 EKS 集群。 我正在嘗試在現有 VPC 上創建它,並傳遞子網 ID 和 VPC ID,但我不知道如何將私有子網和公共子網傳遞給 EKS 集群資源:

resource "aws_eks_cluster" "cluster" {
  enabled_cluster_log_types = []
  name                      = var.cluster_name
  role_arn                  = aws_iam_role.cluster.arn
  version                   = var.eks_version

  vpc_config {
    subnet_ids              = var.priv_subnet_id
    security_group_ids      = []
    endpoint_private_access = "true"
    endpoint_public_access  = "true"
  }
}

我的變量是:

priv_subnet_id = {
    pre = [ "subnet-XXX", "subnet-XXX", "subnet-XXX"]
}

你能指導我最好的方法嗎?

謝謝

你會有一個子網ID列表作為變量真的很奇怪......

大多數情況下,子網是我們創建的資源,如下所示:

resource "aws_subnet" "example1" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
}

resource "aws_subnet" "example2" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.2.0/24"
}

resource "aws_eks_cluster" "cluster" {
  enabled_cluster_log_types = []
  name                      = var.cluster_name
  role_arn                  = aws_iam_role.cluster.arn
  version                   = var.eks_version

  vpc_config {
    subnet_ids              = [aws_subnet.example1.id, aws_subnet.example2.id]
    security_group_ids      = []
    endpoint_private_access = "true"
    endpoint_public_access  = "true"
  }
}

如果它們是現有資源,那么您希望使用數據源獲取它們:
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids

data "aws_subnet_ids" "example" {
  vpc_id = var.vpc_id
}

resource "aws_eks_cluster" "cluster" {
  enabled_cluster_log_types = []
  name                      = var.cluster_name
  role_arn                  = aws_iam_role.cluster.arn
  version                   = var.eks_version

  vpc_config {
    subnet_ids              = data.aws_subnet_ids.example.ids
    security_group_ids      = []
    endpoint_private_access = "true"
    endpoint_public_access  = "true"
  }
}

在非常奇怪的情況下,您必須絕對將它們作為變量...
它可能是這樣的:

variable "subnet_ids" {
  type    = list(string)
  default = ["subnet-X", "subnet-Y", "subnet-Z"]
}

resource "aws_eks_cluster" "cluster" {
  enabled_cluster_log_types = []
  name                      = var.cluster_name
  role_arn                  = aws_iam_role.cluster.arn
  version                   = var.eks_version

  vpc_config {
    subnet_ids              = var.subnet_ids
    security_group_ids      = []
    endpoint_private_access = "true"
    endpoint_public_access  = "true"
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM