如何从 terraform 状态打印出敏感值?

How can I print out sensitive values from terraform state?

目标:我想打印出一个敏感值foo_resource.name.sensitive_field

最初我试图创建一个 output:

output "password" {
  value       = foo_resource.name.sensitive_field
}

我得到了

 Error: Output refers to sensitive values
│ 
│   on main.tf line 186:
│  186: output "password" {
│ 
│ To reduce the risk of accidentally exporting sensitive data that was intended to be only internal, Terraform requires that any root module output containing sensitive data be
│ explicitly marked as sensitive, to confirm your intent.
│ 
│ If you do intend to export this data, annotate the output value as sensitive by adding the following argument:
│     sensitive = true
╵

所以我添加了sensitive = true:

output "password" {
  value       = foo_resource.name.sensitive_field
  sensitive = true
}

然后当我再次 运行 时,我得到了:

$ terraform output
password = <sensitive>
terraform output -raw password

成功了。

有关详细信息,请参阅 the doc

Note: When using the -json or -raw command-line flag, any sensitive values in Terraform state will be displayed in plain text. For more information, see Sensitive Data in State.