Azure Key Vault Terraform module — parameterised, azurerm 4.x, CI-validated.
This Terraform module creates a comprehensive Azure Key Vault with enterprise-grade security features, compliance controls, and operational capabilities.
- Premium SKU support with enhanced security features
- Purge protection and soft delete with configurable retention
- RBAC authorization with granular role assignments
- Network ACLs with IP restrictions and subnet whitelisting
- Private endpoints for secure access
- Azure Policy integration for automated compliance
- RSA and ECDSA keys with configurable sizes
- Automatic key rotation policies
- Key versioning and lifecycle management
- Hardware Security Module (HSM) support
- Secure secret storage with encryption at rest
- Secret versioning and access controls
- Expiration dates and notifications
- Content type classification
- Automated certificate lifecycle management
- Integration with certificate authorities (DigiCert, GlobalSign, etc.)
- Certificate contacts for renewal notifications
- Subject alternative names and extended key usage
- Diagnostic settings with Log Analytics integration
- Audit logging for all operations
- Metrics collection for performance monitoring
- Azure Monitor integration
- Resource locks to prevent accidental deletion
- Tags for resource organization and cost tracking
- Microsoft Cloud Adoption Framework (CAF) naming conventions
- Comprehensive validation and error handling
graph TB
subgraph "Azure Key Vault"
KV[Key Vault<br/>Premium SKU]
Keys[Encryption Keys<br/>RSA/ECDSA]
Secrets[Application Secrets<br/>API Keys, etc.]
Certs[SSL Certificates<br/>Managed/Automated]
end
subgraph "Security Controls"
RBAC[RBAC Authorization<br/>Role Assignments]
ACLs[Network ACLs<br/>IP/Subnets]
PE[Private Endpoints<br/>Secure Access]
PP[Purge Protection<br/>Soft Delete]
end
subgraph "Monitoring"
Logs[Diagnostic Logs<br/>Audit Events]
Metrics[Performance Metrics<br/>Usage Stats]
Alerts[Azure Monitor<br/>Alerts & Dashboards]
end
subgraph "Compliance"
Policies[Azure Policy<br/>Compliance Rules]
Locks[Resource Locks<br/>Deletion Protection]
Tags[Resource Tags<br/>Governance]
end
KV --> Keys
KV --> Secrets
KV --> Certs
Keys --> RBAC
Secrets --> RBAC
Certs --> RBAC
KV --> ACLs
KV --> PE
KV --> PP
KV --> Logs
KV --> Metrics
Logs --> Alerts
KV --> Policies
KV --> Locks
KV --> Tags
Pin the module to a release tag:
module "key_vault" {
source = "git::https://github.com/AIRCLOUD-PL/azure-key-vault-module.git?ref=v1.0.0"
# required inputs
environment = "..."
location = "..."
location_short = "..."
resource_group_name = "..."
}All inputs and outputs are listed in the generated reference below (Inputs, Outputs). Worked examples live in examples/.
module "key_vault" {
source = "git::https://github.com/AIRCLOUD-PL/azure-key-vault-module.git?ref=v1.0.0"
resource_group_name = "rg-enterprise-kv"
location = "East US"
location_short = "eus"
environment = "prod"
# RBAC Authorization
enable_rbac_authorization = true
rbac_administrators = [data.azurerm_client_config.current.object_id]
# Security settings
sku_name = "premium"
purge_protection_enabled = true
soft_delete_retention_days = 90
# Network security
enable_network_acls = true
network_acls_default_action = "Deny"
# Monitoring
enable_diagnostic_settings = true
log_analytics_workspace_id = azurerm_log_analytics_workspace.example.id
tags = {
BusinessUnit = "IT"
CostCenter = "12345"
}
}module "key_vault_enterprise" {
source = "git::https://github.com/AIRCLOUD-PL/azure-key-vault-module.git?ref=v1.0.0"
resource_group_name = "rg-enterprise-kv"
location = "East US"
location_short = "eus"
environment = "prod"
# RBAC with multiple roles
enable_rbac_authorization = true
rbac_administrators = [azuread_group.platform_admins.object_id]
rbac_crypto_officers = [azuread_group.security_team.object_id]
rbac_secrets_users = [azuread_group.app_developers.object_id]
# Premium security
sku_name = "premium"
purge_protection_enabled = true
soft_delete_retention_days = 90
public_network_access_enabled = false
# Private networking
enable_private_endpoint = true
private_endpoint_subnet_id = azurerm_subnet.private_endpoints.id
private_dns_zone_ids = [azurerm_private_dns_zone.keyvault.id]
# Encryption keys
keys = {
"app-key" = {
name = "application-encryption-key"
key_type = "RSA"
key_size = 2048
key_opts = ["decrypt", "encrypt", "sign", "verify"]
rotation_policy = {
expire_after = "P1Y"
notify_before_expiry = "P30D"
}
}
}
# Application secrets
secrets = {
"db-conn" = {
name = "database-connection-string"
value = var.database_connection_string
expiration_date = "2024-12-31T23:59:59Z"
}
}
# SSL certificates
certificates = {
"ssl-cert" = {
name = "wildcard-domain-com"
issuer_parameters = {
name = "DigiCert"
}
x509_certificate_properties = {
subject = "CN=*.domain.com"
validity_in_months = 12
}
}
}
# Certificate contacts
contacts = [
{
email = "security@domain.com"
name = "Security Team"
}
]
# Comprehensive monitoring
enable_diagnostic_settings = true
log_analytics_workspace_id = azurerm_log_analytics_workspace.example.id
diagnostic_logs = ["AuditEvent", "AzurePolicyEvaluationDetails"]
diagnostic_metrics = ["AllMetrics"]
# Compliance controls
enable_resource_lock = true
enable_policy_assignments = true
enable_policy_initiative = true
tags = {
BusinessUnit = "IT"
Compliance = "SOX"
DataSensitivity = "High"
}
}The module includes comprehensive tests using Terratest:
cd modules/security/key-vault/test
go test -v -timeout 30mTest scenarios include:
- Enterprise Key Vault with RBAC and security features
- Access policies validation
- Network security configuration
- Input validation and error handling
- Purge Protection: Prevents permanent deletion of secrets
- Soft Delete: Allows recovery of deleted items
- RBAC Authorization: Granular access control using Azure RBAC
- Network Restrictions: Private endpoints and network ACLs
- Audit Logging: Comprehensive audit trails
- Azure Policy Integration: Automated compliance monitoring
- Resource Locks: Prevention of accidental deletion
- Key Rotation: Automatic key lifecycle management
- Certificate Management: Automated certificate renewal
- Use premium SKU for production workloads
- Enable purge protection and soft delete
- Configure private endpoints for secure access
- Use RBAC instead of access policies for new deployments
- Enable diagnostic logging and monitoring
- Configure key rotation policies
- Use resource locks in production
- Regularly review access permissions
- Follow the established code structure and naming conventions
- Add comprehensive tests for new features
- Update documentation for any changes
- Ensure all tests pass before submitting PR
- Follow security best practices
This module is licensed under the MIT License. See LICENSE file for details.
For issues and questions:
- Create an issue in the repository
- Review the documentation and examples
- Check the test logs for debugging information
Note: This module is designed for enterprise use and includes security features that may require additional Azure permissions or configurations. Always test in a development environment before deploying to production.
| Name | Version |
|---|---|
| terraform | >= 1.5.0 |
| azurerm | ~> 4.0 |
| Name | Version |
|---|---|
| azurerm | 4.81.0 |
No modules.
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| access_policies | List of access policies for the Key Vault | map(object({ |
{} |
no |
| additional_tags | Additional tags to add to resources | map(string) |
{} |
no |
| certificates | Map of certificates to create in the Key Vault | map(object({ |
{} |
no |
| contacts | List of contacts for certificate management | list(object({ |
[] |
no |
| created_by | Identifier of who created this resource | string |
"terraform" |
no |
| custom_name | Custom name for the Key Vault. If provided, name_prefix and name_suffix are ignored | string |
"" |
no |
| diagnostic_logs | List of diagnostic logs to enable | list(string) |
[ |
no |
| diagnostic_metrics | List of diagnostic metrics to enable | list(string) |
[ |
no |
| enable_custom_policies | Enable custom Azure Policy definitions | bool |
false |
no |
| enable_diagnostic_settings | Enable diagnostic settings for the Key Vault | bool |
true |
no |
| enable_network_acls | Enable network ACLs for the Key Vault | bool |
true |
no |
| enable_policy_assignments | Enable Azure Policy assignments for Key Vault | bool |
true |
no |
| enable_policy_initiative | Enable Azure Policy initiative for Key Vault security | bool |
true |
no |
| enable_private_endpoint | Enable private endpoint for the Key Vault | bool |
true |
no |
| enable_rbac_authorization | Enable RBAC authorization instead of access policies | bool |
true |
no |
| enable_resource_lock | Enable resource lock for the Key Vault | bool |
true |
no |
| enabled_for_deployment | Enable Key Vault for Azure Resource Manager deployment | bool |
false |
no |
| enabled_for_disk_encryption | Enable Key Vault for disk encryption | bool |
true |
no |
| enabled_for_template_deployment | Enable Key Vault for ARM template deployment | bool |
false |
no |
| environment | Environment name (dev, test, prod, etc.) | string |
n/a | yes |
| keys | Map of keys to create in the Key Vault | map(object({ |
{} |
no |
| location | Azure region for the Key Vault | string |
n/a | yes |
| location_short | Short name for the location (e.g., 'eus' for East US) | string |
n/a | yes |
| log_analytics_workspace_id | Log Analytics workspace ID for diagnostic settings | string |
null |
no |
| name_prefix | Prefix for resource naming. If empty, defaults to 'kv-{environment}-{location_short}' | string |
"" |
no |
| name_suffix | Suffix for resource naming | string |
"" |
no |
| network_acls_bypass | Bypass options for network ACLs | string |
"AzureServices" |
no |
| network_acls_default_action | Default action for network ACLs | string |
"Deny" |
no |
| network_acls_ip_rules | List of IP rules for network ACLs | list(string) |
[] |
no |
| network_acls_subnet_ids | List of subnet IDs for network ACLs | list(string) |
[] |
no |
| private_dns_zone_ids | List of private DNS zone IDs for the private endpoint | list(string) |
null |
no |
| private_endpoint_subnet_id | Subnet ID for the private endpoint | string |
null |
no |
| project_name | Name of the project | string |
"enterprise" |
no |
| public_network_access_enabled | Enable public network access | bool |
false |
no |
| purge_protection_enabled | Enable purge protection for the Key Vault | bool |
true |
no |
| rbac_administrators | List of principal IDs for Key Vault Administrator role | list(string) |
[] |
no |
| rbac_certificates_officers | List of principal IDs for Key Vault Certificates Officer role | list(string) |
[] |
no |
| rbac_crypto_officers | List of principal IDs for Key Vault Crypto Officer role | list(string) |
[] |
no |
| rbac_crypto_users | List of principal IDs for Key Vault Crypto User role | list(string) |
[] |
no |
| rbac_secrets_officers | List of principal IDs for Key Vault Secrets Officer role | list(string) |
[] |
no |
| rbac_secrets_users | List of principal IDs for Key Vault Secrets User role | list(string) |
[] |
no |
| resource_group_id | Resource group ID for policy assignments | string |
null |
no |
| resource_group_name | Name of the resource group | string |
n/a | yes |
| resource_lock_level | Level of the resource lock | string |
"CanNotDelete" |
no |
| secrets | Map of secrets to create in the Key Vault | map(object({ |
{} |
no |
| sku_name | SKU name for the Key Vault (standard or premium) | string |
"standard" |
no |
| soft_delete_retention_days | Number of days to retain deleted items | number |
90 |
no |
| Name | Description |
|---|---|
| access_policy_ids | Map of access policy keys to policy IDs |
| certificate_ids | Map of certificate names to certificate IDs |
| certificate_thumbprints | Map of certificate names to certificate thumbprints |
| certificate_versions | Map of certificate names to certificate versions |
| diagnostic_setting_id | The ID of the diagnostic setting |
| key_ids | Map of key names to key IDs |
| key_public_keys | Map of key names to public key PEMs |
| key_vault_id | The ID of the Key Vault |
| key_vault_location | The location of the Key Vault |
| key_vault_name | The name of the Key Vault |
| key_vault_resource_group_name | The resource group name of the Key Vault |
| key_vault_tenant_id | The tenant ID of the Key Vault |
| key_vault_uri | The URI of the Key Vault |
| key_versions | Map of key names to key versions |
| network_acls_enabled | Whether network ACLs are enabled |
| private_endpoint_enabled | Whether private endpoint is enabled |
| private_endpoint_id | The ID of the private endpoint |
| private_endpoint_ip_address | The private IP address of the private endpoint |
| purge_protection_enabled | Whether purge protection is enabled |
| rbac_enabled | Whether RBAC authorization is enabled |
| rbac_role_assignments | Map of RBAC role assignments created |
| resource_lock_id | The ID of the resource lock |
| resource_tags | Tags applied to the Key Vault |
| secret_ids | Map of secret names to secret IDs |
| secret_versions | Map of secret names to secret versions |
| soft_delete_enabled | Whether soft delete is enabled |
Browse all modules with usage snippets in the module catalog or on github.com/AIRCLOUD-PL. Built and maintained by AIRCLOUD: DevOps, cloud and AI engineering. Need a hand with Azure landing zones or platform engineering? See aircloud.com.pl/consulting.
