Terraform Module: Azure Key Vault

This Terraform module provisions an Azure Key Vault along with necessary role assignments.

Features

  • Creates an Azure Key Vault with soft delete and purge protection enabled.
  • Assigns the "Key Vault Administrator" role to a specified Azure AD group.
  • Outputs essential details like Key Vault ID, name, and resource group.

Requirements

  • Terraform >= 1.0
  • AzureRM Provider >= 4.18.0

Providers

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "4.18.0"
    }
  }
}

provider "azurerm" {
  features {}
}
1
2
3
4
5
6
7
8
9
10
11
12

Inputs

NameTypeDescriptionRequired
key_vault_namestringThe name of the Key Vault.Yes
key_vault_resource_group_namestringThe name of the resource group for the Key Vault.Yes
locationstringThe Azure region where the Key Vault is created.Yes

Outputs

NameDescription
key_vault_idThe ID of the created Key Vault.
key_vault_nameThe name of the created Key Vault.
key_vault_resource_groupThe resource group containing the Key Vault.

Usage Example

module "key_vault" {
  source                        = "./modules/key_vault"
  key_vault_name                = "my-keyvault"
  key_vault_resource_group_name = "my-resource-group"
  location                      = "West Europe"
}

output "vault_id" {
  value = module.key_vault.key_vault_id
}
1
2
3
4
5
6
7
8
9
10

Notes

  • Make sure the Azure AD group exists before assigning the role.
  • Ensure that your Terraform identity has the necessary permissions to create and manage Key Vaults.

License

MIT

Requirements

NameVersion
azuread3.1.0
azurerm4.18.0
random3.6.3

Modules

No modules.

Resources

NameType
azurerm_key_vault.key_vaultopen in new windowresource
azurerm_resource_group.key_vaultopen in new windowresource
azurerm_role_assignment.cloudfoundation_tfdeployopen in new windowresource
random_string.resource_codeopen in new windowresource
azuread_user.usersopen in new windowdata source
azurerm_client_config.currentopen in new windowdata source
azurerm_role_definition.keyvaultopen in new windowdata source
azurerm_subscription.currentopen in new windowdata source

Inputs

NameDescriptionTypeDefaultRequired
key_vault_nameThe name of the key vault.stringn/ayes
key_vault_resource_group_nameThe name of the resource group containing the key vault.stringn/ayes
locationThe location/region where the key vault is created.stringn/ayes
public_network_access_enabledn/aboolfalseno
subscription_idn/astringn/ayes
usersUsers and their roles provided by meshStack (Note that users must exist in stackit)
list(object(
{
meshIdentifier = string
username = string
firstName = string
lastName = string
email = string
euid = string
roles = list(string)
}
))
n/ayes

Outputs

NameDescription
key_vault_idn/a
key_vault_namen/a
key_vault_resource_groupn/a

Source code & Installation

The source code of this kit module can be found hereopen in new window

Run the following command to install the kit module:

collie kit import azure/buildingblocks/key-vault/buildingblock
1