Skip to content

Commit e7499ce

Browse files
rdarcy1cyrilgdn
andauthored
Add credentials section to index.html.markdown (cyrilgdn#318)
Co-authored-by: Cyril Gaudin <[email protected]>
1 parent c264d8b commit e7499ce

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

website/docs/index.html.markdown

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,73 @@ resource "postgresql_database" "my_db2" {
7171
}
7272
```
7373

74+
## Injecting Credentials
75+
There are several methods of providing credentials to the provider without hardcoding them.
76+
77+
### Environment Variables
78+
Provider settings can be specified via environment variables as follows:
79+
80+
```shell
81+
export PGHOST=localhost
82+
export PGPORT=5432
83+
export PGUSER=postgres
84+
export PGPASSWORD=postgres
85+
```
86+
87+
### Terraform Variables
88+
Input variables can be used in provider configuration. These variables can be initialised in your Terraform code, via a [variable file](https://developer.hashicorp.com/terraform/language/values/variables#variable-definitions-tfvars-files), via [`TF_VAR_` environment variables](https://developer.hashicorp.com/terraform/language/values/variables#environment-variables) or any other method that Terraform allows.
89+
90+
For example:
91+
```hcl
92+
variable "host" {
93+
default = "localhost"
94+
}
95+
96+
variable "password" {
97+
default = "adm"
98+
}
99+
100+
variable "port" {
101+
default = 55432
102+
}
103+
104+
provider "postgresql" {
105+
host = var.host
106+
port = var.port
107+
password = var.password
108+
sslmode = "disable"
109+
}
110+
111+
resource postgresql_database "test" {
112+
name = "test"
113+
}
114+
```
115+
116+
You could set the `host` variable by setting the environment variable `TF_VAR_host`.
117+
118+
### Data Sources and Resources
119+
Credentials can be referenced via Terraform data sources, or resource attributes. This is useful for getting values from a secrets store such as AWS Secrets Manager.
120+
121+
Resource attributes may only be referenced in provider config where the value is available in the resource definition; per [Terraform docs](https://developer.hashicorp.com/terraform/language/providers/configuration#provider-configuration-1):
122+
123+
> you can safely reference input variables, but not attributes exported by resources (with an exception for resource arguments that are specified directly in the configuration).
124+
125+
For example:
126+
127+
```hcl
128+
data "aws_secretsmanager_secret" "postgres_password" {
129+
name = "postgres_password"
130+
}
131+
data "aws_secretsmanager_secret_version" "postgres_password" {
132+
secret_id = data.aws_secretsmanager_secret.postgres_password.id
133+
}
134+
135+
provider "postgresql" {
136+
[...]
137+
password = data.aws_secretsmanager_secret_version.postgres_password.secret_string
138+
}
139+
```
140+
74141
## Argument Reference
75142

76143
The following arguments are supported:

0 commit comments

Comments
 (0)