Skip to content

Commit 9e4a0e7

Browse files
Backport of Add Azure example into v1.11 (#36707)
1 parent fbe858b commit 9e4a0e7

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

website/docs/language/resources/ephemeral/write-only.mdx

+82-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ resource "aws_db_instance" "example" {
7878

7979
During a Terraform operation, the provider uses the `password_wo` value to create the database instance, and then Terraform discards that value without storing it in the plan or state file.
8080

81-
Note that the way this is written, the `password_wo` value is lost after Terraform generates unless we capture it in another resource or output. For an example of generating, storing, retrieving, and using an ephemeral password as a write-only argument, refer to the [expanded example below](#example).
81+
Note that Terraform does not store the generated value for `password_wo`, but you can capture it in another resource or output. For an example of generating, storing, retrieving, and using an ephemeral password as a write-only argument, refer to the [Examples](#examples).
8282

8383
## Update write-only arguments with versions
8484

@@ -124,7 +124,11 @@ resource "aws_db_instance" "main" {
124124
When you increment the `password_wo_version` argument, Terraform notices that change in its plan and notifies the `aws` provider. The `aws` provider then uses the new `password_wo` value to update the `aws_db_instance` resource.
125125

126126

127-
## Example
127+
## Examples
128+
129+
The following demonstrates how to use write-only arguments with different cloud providers.
130+
131+
### Set and store an ephemeral password in AWS Secrets Manager
128132

129133
You can use an `ephemeral` resource to generate a random password, store it in AWS Secrets Manager, and then retrieve it using another `ephemeral` resource. Finally, you can pass the password to the `password_wo` write-only argument of the `aws_db_instance` resource:
130134

@@ -167,4 +171,80 @@ In the above example, the ephemeral resource `aws_secretsmanager_secret_version`
167171

168172
Terraform first creates the secret in AWS Secrets Manager using the ephemeral `random_password`, then retrieve it using the ephemeral `aws_secretsmanager_secret_version` resource, and finally write the password to the write-only `password_wo` argument of the `aws_db_instance` resource.
169173

174+
### Set and store an ephemeral password in Azure Key Vault
175+
176+
You can use a write-only argument to store a password in Azure's Key Vault, then use that password to create a MySQL database in Azure. In the following example, Terraform generates an password using an `ephemeral` resource, stores that password in a `azurerm_key_vault_secret`, then retrieves it in the `azurerm_mysql_flexible_server` resource:
177+
178+
```hcl
179+
provider "azurerm" {
180+
features {}
181+
}
182+
183+
ephemeral "random_password" "db_password" {
184+
length = 16
185+
override_special = "!#$%&*()-_=+[]{}<>:?"
186+
}
187+
188+
locals {
189+
db_password_version = 1
190+
}
191+
192+
resource "azurerm_resource_group" "example" {
193+
name = "example-resource-group"
194+
location = "westeurope"
195+
}
196+
197+
data "azurerm_client_config" "current" {}
198+
199+
resource "azurerm_key_vault" "example" {
200+
name = "example-key-vault"
201+
location = azurerm_resource_group.example.location
202+
resource_group_name = azurerm_resource_group.example.name
203+
tenant_id = data.azurerm_client_config.current.tenant_id
204+
sku_name = "standard"
205+
soft_delete_retention_days = 7
206+
207+
access_policy {
208+
tenant_id = data.azurerm_client_config.current.tenant_id
209+
object_id = data.azurerm_client_config.current.object_id
210+
211+
key_permissions = [
212+
"Get",
213+
]
214+
215+
secret_permissions = [
216+
"Get",
217+
"Delete",
218+
"List",
219+
"Purge",
220+
"Recover",
221+
"Set",
222+
]
223+
}
224+
}
225+
226+
resource "azurerm_key_vault_secret" "example" {
227+
name = "example-secret"
228+
value_wo = ephemeral.random_password.db_password.result
229+
value_wo_version = local.db_password_version
230+
key_vault_id = azurerm_key_vault.example.id
231+
}
232+
233+
ephemeral "azurerm_key_vault_secret" "db_password" {
234+
name = azurerm_key_vault_secret.example.name
235+
key_vault_id = azurerm_key_vault.example.id
236+
}
237+
238+
resource "azurerm_mysql_flexible_server" "example" {
239+
name = "example-mysql-flexible-server"
240+
resource_group_name = azurerm_resource_group.example.name
241+
location = azurerm_resource_group.example.location
242+
sku_name = "B_Standard_B1s"
243+
244+
administrator_login = "newuser"
245+
administrator_password_wo = ephemeral.azurerm_key_vault_secret.db_password.value
246+
administrator_password_wo_version = local.db_password_version
247+
}
248+
```
170249

250+
The above configuration stores your password in Azure's Key Vault and uses it to create a database in Azure without ever storing that password in a Terraform artifact.

0 commit comments

Comments
 (0)