|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create an Azure key vault and key using Terraform' |
| 3 | +description: 'In this article, you create an Azure key vault and key using Terraform' |
| 4 | +services: key-vault |
| 5 | +author: TomArcherMsft |
| 6 | +ms.topic: quickstart |
| 7 | +ms.custom: devx-track-terraform |
| 8 | +ms.service: key-vault |
| 9 | +ms.subservice: keys |
| 10 | +ms.author: tarcher |
| 11 | +ms.date: 4/13/2023 |
| 12 | +--- |
| 13 | + |
| 14 | +# Quickstart: Create an Azure key vault and key using Terraform |
| 15 | + |
| 16 | +[Azure Key Vault](../general/overview.md) is a cloud service that provides a secure store for secrets, such as keys, passwords, and certificate. This article focuses on the process of deploying a Terraform file to create a key vault and a key. |
| 17 | + |
| 18 | +[!INCLUDE [Terraform abstract](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 19 | + |
| 20 | +In this article, you learn how to: |
| 21 | + |
| 22 | +> [!div class="checklist"] |
| 23 | +> * Create a random value for the Azure resource group name using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) |
| 24 | +> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) |
| 25 | +> * Create a random value using [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) |
| 26 | +> * Create an Azure key vault using [azurerm_key_vault](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault) |
| 27 | +> * Create an Azure key vault key using [azurerm_key_vault_key](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault_key) |
| 28 | +
|
| 29 | +[!INCLUDE [AI attribution](../../../includes/ai-generated-attribution.md)] |
| 30 | + |
| 31 | +## Prerequisites |
| 32 | + |
| 33 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure) |
| 34 | + |
| 35 | +## Implement the Terraform code |
| 36 | + |
| 37 | +> [!NOTE] |
| 38 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-key-vault-key). You can view the log file containing the [test results from current and previous versions of Terraform](https://github.com/Azure/terraform/tree/master/quickstart/101-key-vault-key/TestRecord.md). |
| 39 | +> |
| 40 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform) |
| 41 | +
|
| 42 | +1. Create a directory in which to test and run the sample Terraform code and make it the current directory. |
| 43 | + |
| 44 | +1. Create a file named `providers.tf` and insert the following code: |
| 45 | + |
| 46 | + [!code-terraform[master](~/terraform_samples/quickstart/101-key-vault-key/providers.tf)] |
| 47 | + |
| 48 | +1. Create a file named `main.tf` and insert the following code: |
| 49 | + |
| 50 | + [!code-terraform[master](~/terraform_samples/quickstart/101-key-vault-key/main.tf)] |
| 51 | + |
| 52 | +1. Create a file named `variables.tf` and insert the following code: |
| 53 | + |
| 54 | + [!code-terraform[master](~/terraform_samples/quickstart/101-key-vault-key/variables.tf)] |
| 55 | + |
| 56 | +1. Create a file named `outputs.tf` and insert the following code: |
| 57 | + |
| 58 | + [!code-terraform[master](~/terraform_samples/quickstart/101-key-vault-key/outputs.tf)] |
| 59 | + |
| 60 | +## Initialize Terraform |
| 61 | + |
| 62 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 63 | + |
| 64 | +## Create a Terraform execution plan |
| 65 | + |
| 66 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 67 | + |
| 68 | +## Apply a Terraform execution plan |
| 69 | + |
| 70 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 71 | + |
| 72 | +## Verify the results |
| 73 | + |
| 74 | +#### [Azure CLI](#tab/azure-cli) |
| 75 | + |
| 76 | +1. Get the Azure key vault name. |
| 77 | + |
| 78 | + ```console |
| 79 | + azurerm_key_vault_name=$(terraform output -raw azurerm_key_vault_name) |
| 80 | + ``` |
| 81 | + |
| 82 | +1. Run [az keyvault key list](/cli/azure/keyvault/key#az-keyvault-key-list) to display information about the key vault's keys. |
| 83 | + |
| 84 | + ```azurecli |
| 85 | + az keyvault key list --vault-name $azurerm_key_vault_name |
| 86 | + ``` |
| 87 | + |
| 88 | +#### [Azure PowerShell](#tab/azure-powershell) |
| 89 | + |
| 90 | +1. Get the Azure key vault name. |
| 91 | + |
| 92 | + ```console |
| 93 | + $azurerm_key_vault_name=$(terraform output -raw azurerm_key_vault_name) |
| 94 | + ``` |
| 95 | + |
| 96 | +1. Run [Get-AzKeyVault](/powershell/module/az.keyvault/get-azkeyvault) to display information about the new key vault. |
| 97 | + |
| 98 | + ```azurepowershell |
| 99 | + Get-AzKeyVaultKey -VaultName $azurerm_key_vault_name |
| 100 | + ``` |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## Clean up resources |
| 105 | + |
| 106 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 107 | + |
| 108 | +## Troubleshoot Terraform on Azure |
| 109 | + |
| 110 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot) |
| 111 | + |
| 112 | +## Next steps |
| 113 | + |
| 114 | +> [!div class="nextstepaction"] |
| 115 | +> [Key Vault security overview](../general/security-features.md) |
0 commit comments