我最近在 Terraform 中创建了一个 cosmos 数据库,并且尝试将其数据库连接字符串作为 keyvault 中的 secret 传递,但是在执行此操作时出现以下错误:
错误:属性值类型不正确 │ │ 在 keyvault.tf 第 282 行,资源“azurerm_key_vault_secret”“Authentication_Server_Cosmos_DB_ConnectionString”中: │ 282:值 = azurerm_cosmosdb_account.nsauthsrvcosmosdb.connection_strings │ ├──────── ──────── │ │ azurerm_cosmosdb_account.nsauthsrvcosmosdb.connection_strings 具有敏感值 │ │ 属性“value”的值不合适:需要字符串。
我也尝试过使用敏感参数,但 Key Vault 不喜欢该参数,而且我也找不到任何有关如何执行此操作的文档。在 Terraform 网站上,它只是将其列为您可以调用的属性。
我的 Terraform Secret 代码如下,我不会将所有代码都放在这里,因为堆栈溢出不喜欢我拥有的代码量。
所以请假设,我正在使用最新的 Azurerm 代理,并且我的其余代码都是正确的,只是 secret 部分不起作用。
resource "azurerm_key_vault_secret" "Authentication_Server_Cosmos_DB_ConnectionString" { //Auth Server Cosmos Connection String Secret
name = "AuthenticationServerCosmosDBConnectionString"
value = azurerm_cosmosdb_account.nsauthsrvcosmosdb.connection_strings
key_vault_id = azurerm_key_vault.nscsecrets.id
depends_on = [
azurerm_key_vault_access_policy.client,
azurerm_key_vault_access_policy.service_principal,
azurerm_cosmosdb_account.nsauthsrvcosmosdb,
]
}
最佳答案
您提供的值中有 4 个连接字符串,并且这些值的类型为 secure_string
。因此,您需要将它们转换为 String
值,并为要存储在 keyvault 中的值应用索引。
用于存储您可以在下面使用的所有 4 个连接字符串:
resource "azurerm_key_vault_secret" "example" {
count = length(azurerm_cosmosdb_account.nsauthsrvcosmosdb.connection_strings)
name = "AuthenticationServerCosmosDBConnectionString-${count.index}"
value = tostring("${azurerm_cosmosdb_account.nsauthsrvcosmosdb.connection_strings[count.index]}")
key_vault_id = azurerm_key_vault.example.id
}
输出:
如果您只想存储一个连接字符串,那么您可以根据您的要求使用索引(例如:如果您想存储第一个connection_string
,则使用 '0 ' 作为索引,同样为 1/2/3 。) 在下面的代码中:
resource "azurerm_key_vault_secret" "example1" {
name = "AuthenticationServerCosmosDBConnectionString"
value = tostring("${azurerm_cosmosdb_account.nsauthsrvcosmosdb.connection_strings[0]}")
key_vault_id = azurerm_key_vault.example.id
}
输出:
关于azure - Terraform 将 Cosmos 数据库连接字符串传递到 KeyVault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70080780/