Skip to content

Commit ac62910

Browse files
improve write-only attribute example
1 parent b0dbd66 commit ac62910

File tree

1 file changed

+27
-5
lines changed
  • website/docs/language/resources/ephemeral

1 file changed

+27
-5
lines changed

website/docs/language/resources/ephemeral/index.mdx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,43 @@ Use write-only arguments to securely pass temporary values to resources during a
4444
<CodeBlockConfig highlight="11">
4545

4646
```hcl
47-
ephemeral "random" "password" {
47+
ephemeral "random_password" "db_password" {
4848
length = 16
4949
}
5050
51-
resource "aws_db_instance" "test" {
52-
instance_class = "db.t5.micro"
51+
resource "aws_secretsmanager_secret" "db_password" {
52+
name = "db_password"
53+
}
54+
55+
resource "aws_secretsmanager_secret_version" "db_password" {
56+
secret_id = aws_secretsmanager_secret.db_password.id
57+
secret_string_wo = ephemeral.random_password.db_password.result
58+
secret_string_wo_version = 1
59+
}
60+
61+
ephemeral "aws_secretsmanager_secret_version" "db_password" {
62+
secret_id = aws_secretsmanager_secret.db_password.id
63+
}
64+
65+
resource "aws_db_instance" "example" {
66+
instance_class = "db.t3.micro"
5367
allocated_storage = "5"
5468
engine = "postgres"
55-
username = "admin"
69+
username = "example"
5670
skip_final_snapshot = true
57-
password_wo = ephemeral.random.password.value
71+
72+
password_wo = ephemeral.aws_secretsmanager_secret_version.db_password.secret_string
5873
password_wo_version = 1
5974
}
6075
```
6176

77+
### Deferring ephemeral resources
78+
79+
If an input argument of an ephemeral resource references a value that is not yet known but will be during or after the plan, Terraform defers the resource’s execution to the apply stage instead of running it during the plan. This behavior allows Terraform to evaluate the ephemeral resource at the correct time and ensures that the resource is not executed prematurely.
80+
81+
In the above example, the ephemeral resource aws_secretsmanager_secret_version references an input argument that is initially unknown. As a result, Terraform defers its execution to the apply stage, ensuring that the resource is evaluated at the correct time.
82+
This allows Terraform to first create the secret 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.
83+
6284
</CodeBlockConfig>
6385

6486
When Terraform creates the `aws_db_instance` resource, Terraform sends the `password_wo` argument to the `aws` provider. The `aws` provider then uses the `password_wo` value to configure the database instance, and then Terraform discards the password value without ever storing it.

0 commit comments

Comments
 (0)