如何在 Terraform 中创建数据源列表?
How to create a list of data sources in Terraform?
我在 AWS Secrets Manager 上有一些秘密。我已经创建了所有机密的 ARN 列表,如下所示
secretList = {
"secret1" = "ARN1",
"secret2" = "ARN2"
}
我可以使用数据源检索单个机密的内容,例如
//Get the secret ID
data "aws_secretsmanager_secret" "secrets" {
arn = "ARN1"
}
//Retrive the latest content
data "aws_secretsmanager_secret_version" "current" {
secret_id = data.aws_secretsmanager_secret.secrets.id
}
现在我想创建一个包含我所有机密内容的列表,我该怎么做?
由于每个秘密都可以有多对 key/value,我认为结果应该是这样的:
secretValue = {
"secret1" = {"key1" = "value1","key2" = "value2"},
"secret2" = {"key1" = "value1","key2" = "value2","key3" = "value3"}
}
您可以使用 for_each:
data "aws_secretsmanager_secret" "secrets" {
for_each = var.secretList
arn = each.value
}
data "aws_secretsmanager_secret_version" "current" {
for_each = var.secretList
secret_id = data.aws_secretsmanager_secret.secrets[each.key].id
}
然后您可以使用以下方法访问机密:
data.aws_secretsmanager_secret_version.current["secret1"].secret_string
我在 AWS Secrets Manager 上有一些秘密。我已经创建了所有机密的 ARN 列表,如下所示
secretList = {
"secret1" = "ARN1",
"secret2" = "ARN2"
}
我可以使用数据源检索单个机密的内容,例如
//Get the secret ID
data "aws_secretsmanager_secret" "secrets" {
arn = "ARN1"
}
//Retrive the latest content
data "aws_secretsmanager_secret_version" "current" {
secret_id = data.aws_secretsmanager_secret.secrets.id
}
现在我想创建一个包含我所有机密内容的列表,我该怎么做? 由于每个秘密都可以有多对 key/value,我认为结果应该是这样的:
secretValue = {
"secret1" = {"key1" = "value1","key2" = "value2"},
"secret2" = {"key1" = "value1","key2" = "value2","key3" = "value3"}
}
您可以使用 for_each:
data "aws_secretsmanager_secret" "secrets" {
for_each = var.secretList
arn = each.value
}
data "aws_secretsmanager_secret_version" "current" {
for_each = var.secretList
secret_id = data.aws_secretsmanager_secret.secrets[each.key].id
}
然后您可以使用以下方法访问机密:
data.aws_secretsmanager_secret_version.current["secret1"].secret_string