Skip to content

About

Azure Key Vault Terraform module — parameterised, azurerm 4.x, CI-validated · AIRCLOUD

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

AIRCLOUD: DevOps, cloud and AI engineering

azure-key-vault-module

Azure Key Vault Terraform module — parameterised, azurerm 4.x, CI-validated.

CI License: MIT Release

This Terraform module creates a comprehensive Azure Key Vault with enterprise-grade security features, compliance controls, and operational capabilities.

Features

🔐 Security & Compliance

  • 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

🗝️ Key Management

  • RSA and ECDSA keys with configurable sizes
  • Automatic key rotation policies
  • Key versioning and lifecycle management
  • Hardware Security Module (HSM) support

🔒 Secret Management

  • Secure secret storage with encryption at rest
  • Secret versioning and access controls
  • Expiration dates and notifications
  • Content type classification

📜 Certificate Management

  • Automated certificate lifecycle management
  • Integration with certificate authorities (DigiCert, GlobalSign, etc.)
  • Certificate contacts for renewal notifications
  • Subject alternative names and extended key usage

📊 Monitoring & Logging

  • Diagnostic settings with Log Analytics integration
  • Audit logging for all operations
  • Metrics collection for performance monitoring
  • Azure Monitor integration

🔧 Operational Features

  • 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

Architecture

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
Loading

Usage

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/.

Basic Example

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"
  }
}

Enterprise Example

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"
  }
}

Testing

The module includes comprehensive tests using Terratest:

cd modules/security/key-vault/test
go test -v -timeout 30m

Test scenarios include:

  • Enterprise Key Vault with RBAC and security features
  • Access policies validation
  • Network security configuration
  • Input validation and error handling

Security Considerations

🔐 Key Security Features

  • 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

🛡️ Compliance

  • Azure Policy Integration: Automated compliance monitoring
  • Resource Locks: Prevention of accidental deletion
  • Key Rotation: Automatic key lifecycle management
  • Certificate Management: Automated certificate renewal

📋 Best Practices

  1. Use premium SKU for production workloads
  2. Enable purge protection and soft delete
  3. Configure private endpoints for secure access
  4. Use RBAC instead of access policies for new deployments
  5. Enable diagnostic logging and monitoring
  6. Configure key rotation policies
  7. Use resource locks in production
  8. Regularly review access permissions

Contributing

  1. Follow the established code structure and naming conventions
  2. Add comprehensive tests for new features
  3. Update documentation for any changes
  4. Ensure all tests pass before submitting PR
  5. Follow security best practices

License

This module is licensed under the MIT License. See LICENSE file for details.

Support

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.

Reference

Requirements

Name Version
terraform >= 1.5.0
azurerm ~> 4.0

Providers

Name Version
azurerm 4.81.0

Modules

No modules.

Resources

Name Type
azurerm_key_vault.this resource
azurerm_key_vault_access_policy.this resource
azurerm_key_vault_certificate.this resource
azurerm_key_vault_key.this resource
azurerm_key_vault_secret.this resource
azurerm_management_lock.this resource
azurerm_monitor_diagnostic_setting.this resource
azurerm_policy_definition.key_vault_certificate_auto_renewal resource
azurerm_policy_definition.key_vault_key_rotation resource
azurerm_policy_definition.key_vault_secret_expiration resource
azurerm_policy_set_definition.key_vault_security_initiative resource
azurerm_private_endpoint.this resource
azurerm_resource_group_policy_assignment.key_vault_firewall resource
azurerm_resource_group_policy_assignment.key_vault_logging resource
azurerm_resource_group_policy_assignment.key_vault_private_link resource
azurerm_resource_group_policy_assignment.key_vault_private_network resource
azurerm_resource_group_policy_assignment.key_vault_purge_protection resource
azurerm_resource_group_policy_assignment.key_vault_security_initiative resource
azurerm_resource_group_policy_assignment.key_vault_soft_delete resource
azurerm_role_assignment.key_vault_administrator resource
azurerm_role_assignment.key_vault_certificates_officer resource
azurerm_role_assignment.key_vault_crypto_officer resource
azurerm_role_assignment.key_vault_crypto_user resource
azurerm_role_assignment.key_vault_secrets_officer resource
azurerm_role_assignment.key_vault_secrets_user resource
azurerm_client_config.current data source
azurerm_policy_definition.key_vault_firewall data source
azurerm_policy_definition.key_vault_managed_hsm data source
azurerm_policy_definition.key_vault_private_endpoint data source
azurerm_policy_definition.key_vault_purge_protection data source
azurerm_policy_definition.key_vault_rbac data source
azurerm_policy_definition.key_vault_soft_delete data source

Inputs

Name Description Type Default Required
access_policies List of access policies for the Key Vault
map(object({
tenant_id = string
object_id = string
key_permissions = list(string)
secret_permissions = list(string)
certificate_permissions = list(string)
storage_permissions = list(string)
}))
{} no
additional_tags Additional tags to add to resources map(string) {} no
certificates Map of certificates to create in the Key Vault
map(object({
name = string
issuer_parameters = object({
name = string
})
key_properties = object({
exportable = bool
key_size = number
key_type = string
reuse_key = bool
})
secret_properties = object({
content_type = string
})
x509_certificate_properties = object({
extended_key_usage = list(string)
key_usage = list(string)
subject = string
validity_in_months = number
subject_alternative_names = object({
dns_names = list(string)
emails = list(string)
upns = list(string)
})
})
tags = optional(map(string), {})
}))
{} no
contacts List of contacts for certificate management
list(object({
email = string
name = optional(string)
phone = optional(string)
}))
[] 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)
[
"AuditEvent",
"AzurePolicyEvaluationDetails"
]
no
diagnostic_metrics List of diagnostic metrics to enable list(string)
[
"AllMetrics"
]
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({
name = string
key_type = string
key_size = optional(number)
key_opts = list(string)
curve = optional(string)
not_before_date = optional(string)
expiration_date = optional(string)
rotation_policy = optional(object({
time_before_expiry = optional(string)
expire_after = optional(string)
notify_before_expiry = optional(string)
}))
tags = optional(map(string), {})
}))
{} 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({
name = string
value = string
content_type = optional(string)
not_before_date = optional(string)
expiration_date = optional(string)
tags = optional(map(string), {})
}))
{} 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

Outputs

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

Part of the AIRCLOUD Azure module catalog

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.

About

Azure Key Vault Terraform module — parameterised, azurerm 4.x, CI-validated · AIRCLOUD

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages