Skip to content

Commit a45acb6

Browse files
committed
Quickstart using Terraform
1 parent 0103b8c commit a45acb6

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
# Customer intent: As a security admin who is new to Azure, I want to use Key Vault to securely store keys and passwords in Azure.
13+
---
14+
15+
# Quickstart: Create an Azure key vault and key using Terraform
16+
17+
[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.
18+
19+
[!INCLUDE [Terraform abstract](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)]
20+
21+
In this article, you learn how to:
22+
23+
> [!div class="checklist"]
24+
> * Create a random value for the Azure resource group name using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet)
25+
> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group)
26+
> * Create a random value using [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string)
27+
> * Create an Azure Key Vault using [azurerm_key_vault](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault)
28+
> * Create an Azure Key Vault key using [azurerm_key_vault_key](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/key_vault_key)
29+
30+
[!INCLUDE [AI attribution](../../../includes/ai-generated-attribution.md)]
31+
32+
## Prerequisites
33+
34+
- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure)
35+
36+
## Implement the Terraform code
37+
38+
> [!NOTE]
39+
> 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).
40+
>
41+
> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform)
42+
43+
1. Create a directory in which to test and run the sample Terraform code and make it the current directory.
44+
45+
1. Create a file named `providers.tf` and insert the following code:
46+
47+
[!code-terraform[master](~/terraform_samples/quickstart/101-key-vault-key/providers.tf)]
48+
49+
1. Create a file named `main.tf` and insert the following code:
50+
51+
[!code-terraform[master](~/terraform_samples/quickstart/101-key-vault-key/main.tf)]
52+
53+
1. Create a file named `variables.tf` and insert the following code:
54+
55+
[!code-terraform[master](~/terraform_samples/quickstart/101-key-vault-key/variables.tf)]
56+
57+
1. Create a file named `outputs.tf` and insert the following code:
58+
59+
[!code-terraform[master](~/terraform_samples/quickstart/101-key-vault-key/outputs.tf)]
60+
61+
## Initialize Terraform
62+
63+
[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)]
64+
65+
## Create a Terraform execution plan
66+
67+
[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)]
68+
69+
## Apply a Terraform execution plan
70+
71+
[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)]
72+
73+
## Verify the results
74+
75+
#### [Azure CLI](#tab/azure-cli)
76+
77+
1. Get the Azure key vault name.
78+
79+
```console
80+
azurerm_key_vault_name=$(terraform output -raw azurerm_key_vault_name)
81+
```
82+
83+
1. Run [az keyvault key list](/cli/azure/keyvault/key#az-keyvault-key-list) to display information about the key vault's keys.
84+
85+
```azurecli
86+
az keyvault key list --vault-name $azurerm_key_vault_name
87+
```
88+
89+
#### [Azure PowerShell](#tab/azure-powershell)
90+
91+
1. Get the Azure key vault name.
92+
93+
```console
94+
$azurerm_key_vault_name=$(terraform output -raw azurerm_key_vault_name)
95+
```
96+
97+
1. Run [Get-AzKeyVault](/powershell/module/az.keyvault/get-azkeyvault) to display information about the new Key Vault.
98+
99+
```azurepowershell
100+
Get-AzKeyVaultKey -VaultName $azurerm_key_vault_name
101+
```
102+
103+
---
104+
105+
## Clean up resources
106+
107+
[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)]
108+
109+
## Troubleshoot Terraform on Azure
110+
111+
[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot)
112+
113+
## Next steps
114+
115+
> [!div class="nextstepaction"]
116+
> [Key Vault security overview](../general/security-features.md)

articles/key-vault/keys/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ items:
3333
- name: ARM template
3434
displayName: Resource Manager
3535
href: quick-create-template.md
36+
- name: Terraform
37+
href: quick-create-terraform.md
3638

3739
- name: Tutorials
3840
items:

0 commit comments

Comments
 (0)