一,引言
最近有网友私信我,将 Terraform 部署到 Azure 是一种将基础结构作为代码进行管理的好方法,但是如何使用 Azure Key Vault 来存储我们的 Secret ?在这篇博文中,我将给大家展示一下展示如何使用 Terraform 引用 Azure Key Vault Secret。
1)这个时候就有人问了,Secret 信息为什么要存储在 Azure Key Vault ?
Azure Key Vault 是在 Azure 中存储机密的好方法。它是存储和访问 Secret 的安全场所。您可以生成、存储和控制对令牌、密码、证书、API 密钥和其他秘密的访问。
让我们一起开始今天的内容来展示如何做到这一点 ?
二,正文
1,使用 Terraform 创建 Azure Key Vault
让我们使用Terraform 创建一个 Key Vault
data "azurerm_resource_group" "cnbate_resource_group" { name = "Web_Test_TF_RG" } resource "azurerm_key_vault" "keyvault1" { name = "cnbateblogwebkv" resource_group_name = data.azurerm_resource_group.cnbate_resource_group.name tenant_id = data.azurerm_client_config.current.tenant_id sku_name = "standard" location = data.azurerm_resource_group.cnbate_resource_group.location access_policy { tenant_id = data.azurerm_client_config.current.tenant_id object_id = data.azurerm_client_config.current.object_id secret_permissions = [ "Get", "List", "Set", "Delete" ] } }
2,使用 Terraform 和 Azure CLI 创建 Secret
Terraform:
resource "azurerm_key_vault_secret" "keyvault_secret1" { name = "Name1" value = "cnbatelisi" key_vault_id = azurerm_key_vault.kv.id }
使用 Azure CLI 创建第二个 Key Vault 机密
az keyvault secret set --vault-name cnabteblogwebkv --name Name2 --value cnbatewangwu
登录 Azure Portal,找到资源组 " Web_Test_TF_RG " 下的名字叫 “” 的 Key vault
下面创建的两个 Secret :
3,引用 Terraform 中的两个秘密
在这个例子中,我在上面添加了两个 Secret 。我现在将展示如何在 Terraform 中引用它们。
第一个将根据 Terraform 创建的 Secret 创建一个 Storage Account。第二个将根据 Azure CLI 创建的机密创建一个存储帐户。
resource "azurerm_storage_account" "storage_account1" { name = azurerm_key_vault_secret.keyvault_secret1.value resource_group_name = data.azurerm_resource_group.cnbate_resource_group.name location = data.azurerm_resource_group.cnbate_resource_group.location account_tier = "Standard" account_replication_type = "LRS" depends_on = [ data.azurerm_resource_group.cnbate_resource_group, azurerm_key_vault_secret.keyvault_secret1 ] }
对于第二个示例,我将使用 datasource 来引用 Azure CLI 创建的 keyvault_secret2
data "azurerm_key_vault_secret" "keyvault_secret2" { name = "Name2" key_vault_id = azurerm_key_vault.keyvault1.id }resource "azurerm_storage_account" "storage_account2" { name = data.azurerm_key_vault_secret.keyvault_secret2.value resource_group_name = data.azurerm_resource_group.cnbate_resource_group.name location = data.azurerm_resource_group.cnbate_resource_group.location account_tier = "Standard" account_replication_type = "LRS"
depends_on = [ data.azurerm_resource_group.cnbate_resource_group ] }
完整代码:
terraform { # backend "azurerm" { # storage_account_name = "cnbatestorestatefile004" # container_name = "terraform-state" # key = "cnbate.terraform.stats" # } required_providers { azurerm = { } } } provider "azurerm" { features {} } locals { location_eastAsia = "East Asia" } data "azurerm_client_config" "current" { } data "azurerm_resource_group" "cnbate_resource_group" { name = "Web_Test_TF_RG" } resource "azurerm_key_vault" "keyvault1" { name = "cnbateblogwebkv" resource_group_name = data.azurerm_resource_group.cnbate_resource_group.name tenant_id = data.azurerm_client_config.current.tenant_id sku_name = "standard" location = data.azurerm_resource_group.cnbate_resource_group.location access_policy { tenant_id = data.azurerm_client_config.current.tenant_id object_id = data.azurerm_client_config.current.object_id secret_permissions = [ "Get", "List", "Set", "Delete" ] } } resource "azurerm_key_vault_secret" "keyvault_secret1" { name = "Name1" value = "cnbatelisi" key_vault_id = azurerm_key_vault.keyvault1.id } resource "azurerm_storage_account" "storage_account1" { name = azurerm_key_vault_secret.keyvault_secret1.value resource_group_name = data.azurerm_resource_group.cnbate_resource_group.name location = data.azurerm_resource_group.cnbate_resource_group.location account_tier = "Standard" account_replication_type = "LRS" depends_on = [ data.azurerm_resource_group.cnbate_resource_group, azurerm_key_vault_secret.keyvault_secret1 ] } data "azurerm_key_vault_secret" "keyvault_secret2" { name = "Name2" key_vault_id = azurerm_key_vault.keyvault1.id } resource "azurerm_storage_account" "storage_account2" { name = data.azurerm_key_vault_secret.keyvault_secret2.value resource_group_name = data.azurerm_resource_group.cnbate_resource_group.name location = data.azurerm_resource_group.cnbate_resource_group.location account_tier = "Standard" account_replication_type = "LRS" depends_on = [ data.azurerm_resource_group.cnbate_resource_group ] }Terraform Code
登录 Azure Portal,查看利用Terraform 创建好的 Storage Account
Bingo!!!!!
标签:resource,azurerm,Terraform,Key,vault,Azure,group,data,name From: https://www.cnblogs.com/AllenMaster/p/17420450.html