|
| 1 | +# Azure Key Vault Terraform Module |
| 2 | + |
| 3 | +Terraform Module to create a Key Vault also adds required access policies for azure AD users, groups and azure AD service principals. This module also creates private endpoint and sends all logs to log analytic workspace or storage. |
| 4 | + |
| 5 | +## Module Usage to enable privaite endpoint using existing VNet and Subnet |
| 6 | + |
| 7 | +```hcl |
| 8 | +# Azurerm Provider configuration |
| 9 | +provider "azurerm" { |
| 10 | + features {} |
| 11 | +} |
| 12 | +
|
| 13 | +data "azurerm_virtual_network" "example" { |
| 14 | + name = "vnet-shared-hub-westeurope-001" |
| 15 | + resource_group_name = "rg-shared-westeurope-01" |
| 16 | +} |
| 17 | +
|
| 18 | +data "azurerm_subnet" "example" { |
| 19 | + name = "snet-private-ep" |
| 20 | + virtual_network_name = data.azurerm_virtual_network.example.name |
| 21 | + resource_group_name = data.azurerm_virtual_network.example.resource_group_name |
| 22 | +} |
| 23 | +
|
| 24 | +module "key-vault" { |
| 25 | + source = "kumarvna/key-vault/azurerm" |
| 26 | + version = "2.2.0" |
| 27 | +
|
| 28 | + # By default, this module will not create a resource group and expect to provide |
| 29 | + # a existing RG name to use an existing resource group. Location will be same as existing RG. |
| 30 | + # set the argument to `create_resource_group = true` to create new resrouce. |
| 31 | + resource_group_name = "rg-shared-westeurope-01" |
| 32 | + key_vault_name = "demo-project-shard" |
| 33 | + key_vault_sku_pricing_tier = "premium" |
| 34 | +
|
| 35 | + # Once `Purge Protection` has been Enabled it's not possible to Disable it |
| 36 | + # Deleting the Key Vault with `Purge Protection` enabled will schedule the Key Vault to be deleted |
| 37 | + # The default retention period is 90 days, possible values are from 7 to 90 days |
| 38 | + # use `soft_delete_retention_days` to set the retention period |
| 39 | + enable_purge_protection = false |
| 40 | + # soft_delete_retention_days = 90 |
| 41 | +
|
| 42 | + # Access policies for users, you can provide list of Azure AD users and set permissions. |
| 43 | + # Make sure to use list of user principal names of Azure AD users. |
| 44 | + access_policies = [ |
| 45 | + { |
| 46 | + azure_ad_user_principal_names = ["[email protected]", "[email protected]"] |
| 47 | + key_permissions = ["get", "list"] |
| 48 | + secret_permissions = ["get", "list"] |
| 49 | + certificate_permissions = ["get", "import", "list"] |
| 50 | + storage_permissions = ["backup", "get", "list", "recover"] |
| 51 | + }, |
| 52 | +
|
| 53 | + # Access policies for AD Groups |
| 54 | + # to enable this feature, provide a list of Azure AD groups and set permissions as required. |
| 55 | + { |
| 56 | + azure_ad_group_names = ["ADGroupName1", "ADGroupName2"] |
| 57 | + key_permissions = ["get", "list"] |
| 58 | + secret_permissions = ["get", "list"] |
| 59 | + certificate_permissions = ["get", "import", "list"] |
| 60 | + storage_permissions = ["backup", "get", "list", "recover"] |
| 61 | + }, |
| 62 | +
|
| 63 | + # Access policies for Azure AD Service Principlas |
| 64 | + # To enable this feature, provide a list of Azure AD SPN and set permissions as required. |
| 65 | + { |
| 66 | + azure_ad_service_principal_names = ["azure-ad-dev-sp1", "azure-ad-dev-sp2"] |
| 67 | + key_permissions = ["get", "list"] |
| 68 | + secret_permissions = ["get", "list"] |
| 69 | + certificate_permissions = ["get", "import", "list"] |
| 70 | + storage_permissions = ["backup", "get", "list", "recover"] |
| 71 | + } |
| 72 | + ] |
| 73 | +
|
| 74 | + # Create a required Secrets as per your need. |
| 75 | + # When you Add `usernames` with empty password this module creates a strong random password |
| 76 | + # use .tfvars file to manage the secrets as variables to avoid security issues. |
| 77 | + secrets = { |
| 78 | + "message" = "Hello, world!" |
| 79 | + "vmpass" = "" |
| 80 | + } |
| 81 | +
|
| 82 | + # Creating Private Endpoint requires, VNet name and address prefix to create a subnet |
| 83 | + # By default this will create a `privatelink.vaultcore.azure.net` DNS zone. |
| 84 | + # To use existing private DNS zone specify `existing_private_dns_zone` with valid zone name |
| 85 | + enable_private_endpoint = true |
| 86 | + existing_vnet_id = data.azurerm_virtual_network.example.id |
| 87 | + existing_subnet_id = data.azurerm_subnet.example.id |
| 88 | + # existing_private_dns_zone = "demo.example.com" |
| 89 | +
|
| 90 | + # (Optional) To enable Azure Monitoring for Azure Application Gateway |
| 91 | + # (Optional) Specify `storage_account_id` to save monitoring logs to storage. |
| 92 | + log_analytics_workspace_id = var.log_analytics_workspace_id |
| 93 | + #storage_account_id = var.storage_account_id |
| 94 | +
|
| 95 | + # Adding additional TAG's to your Azure resources |
| 96 | + tags = { |
| 97 | + ProjectName = "demo-project" |
| 98 | + Env = "dev" |
| 99 | + |
| 100 | + BusinessUnit = "CORP" |
| 101 | + ServiceClass = "Gold" |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +## Terraform Usage |
| 107 | + |
| 108 | +To run this example you need to execute following Terraform commands |
| 109 | + |
| 110 | +```hcl |
| 111 | +terraform init |
| 112 | +terraform plan |
| 113 | +terraform apply |
| 114 | +``` |
| 115 | + |
| 116 | +Run `terraform destroy` when you don't need these resources. |
0 commit comments