
Terraform uses state to track the infrastructure you manage with Infrastructure as Code (IaC). Database passwords, API keys, tokens, and other Terraform secrets end up in it because Terraform records everything it needs to manage resources and calculate changes. This is one way secrets end up in unexpected places across your infrastructure.
In its SCARLETEEL investigation, Sysdig found that an attacker obtained a plaintext IAM access key and secret key from a terraform.tfstate file stored in S3. The attacker used those credentials to attempt to access resources in a second AWS account. The requests only failed because the credentials lacked the required permissions.
Common controls, such as sensitive = true and encrypting remote backends, reduce exposure, but they do not prevent secrets from being stored in the state file. Ephemeral resources only keep supported values out of state.
This guide explains how Terraform handles secrets in state, what the common protections actually change, and how to keep secrets out of the Terraform graph so they don't end up in state.
Terraform state files track the infrastructure you manage and also record resource attributes, such as resource IDs, configuration values, connection details, and other values Terraform needs to manage those resources and calculate changes.
Some of these attributes can contain sensitive information. Terraform needs those attributes to manage infrastructure and compute changes across providers such as AWS or GitHub. This is not an issue with a particular provider; it's how Terraform state works.
Here are two common ways this shows up: a random_password resource generates a database password and stores the value in state so Terraform can track it. With data sources, an aws_secretsmanager_secret_version can fetch a secret at plan time so it can be referenced elsewhere in your configuration; the fetched value is also recorded in state, even though you never typed it into your configuration.
Understanding why secrets end up in state also shows where the common protections fall short.
You may already use some of these controls to protect sensitive values in your Terraform workflow. Each one addresses a different part of the exposure, but not all of them keep the value out of state.
If you've marked a variable or output as sensitive = true, you've probably already noticed what it does. Terraform redacts the raw value in plan and apply output, showing (sensitive value). But what it doesn't do is change how the value is stored in state. It is still written there, so anyone with access to that state (whether a teammate, a CI runner, or a compromised IAM role) can read it directly.
A remote backend stores your Terraform state in a shared location, such as S3 or Terraform Cloud, rather than using local state. Encryption at rest protects the stored state from unauthorized access to the underlying storage. The secrets remain in the state, so anyone with valid backend access can still read the values stored there.
Terraform v1.10 introduced ephemeral values, the closest native solution for keeping secrets entirely out of state. Ephemeral resources are one way Terraform produces ephemeral values. They can fetch or generate a value, such as a password or short-lived token, make it available in your configuration, and keep the value out of state. Ephemeral resources require support from the provider and resource being used, so they are not yet universally supported across providers.
These controls can reduce the extent to which a secret is exposed, but they do not all prevent the value from being entered into Terraform state. To keep a secret out of state entirely, you need to change how Terraform receives it in the first place.
The most reliable way to keep a secret out of Terraform state is to keep it outside Terraform's graph. Instead of including credentials in your Terraform configuration, inject them into the shell environment running Terraform. Terraform can then consume those credentials at runtime through provider-specific environment variables.
When the value is used for provider authentication, Terraform does not record the environment variable itself in state. The secret is available to the Terraform process when it needs to authenticate, without becoming a value Terraform needs to track.
This is a pattern that doppler run implements, and the next section covers how to use Doppler with Terraform in practice.
Here are a few ways to configure Doppler to interact with Terraform.
As indicated earlier, the doppler run command can be used to inject secrets stored in Doppler directly into Terraform via Doppler's CLI. A use case is supplying tokens for provider authentication. For example, authenticating into the Doppler provider means setting up the provider configuration:
Before applying Terraform, supply a Doppler token through the DOPPLER_TOKEN environment variable. For example, if your CI job exposes an existing Doppler token as a protected environment variable named DOPPLER_SERVICE_TOKEN, export it like this:
The Doppler CLI uses that token to retrieve secrets from your selected configuration, and the Terraform provider reads the same token from the environment.
Then apply the Terraform configuration:
This implementation prevents Terraform from recording the token value in state. If you want to try this pattern locally, you can start with Doppler's free plan. Service account identities for OIDC authentication, covered later in this guide, are available only on the Team and Enterprise plans.
Another use case is for supplying input variables for Terraform without using the Doppler provider. When you run Terraform through doppler run and use the tf-var name transformer, it can convert Doppler secrets into Terraform input variables:
The --name-transformer tf-var flag converts a secret such as API_KEY into TF_VAR_api_key, which Terraform automatically recognizes when you define a matching api_key variable. Doppler injects the value into the Terraform process as an environment variable when the command runs.
The only caveat is that if Terraform uses the variable's value to populate a resource attribute, it can still become part of that resource's state. This method also only lets you supply secrets; if you want Terraform to fetch and manage secrets stored in Doppler, you need a different setup.
To fetch and manage Doppler secrets with Terraform, you need the Doppler provider. There are two ways to achieve this: the doppler_secrets data source and the doppler_secret resource.
The doppler_secrets data source lets you fetch secrets only. The secrets become available inside the Terraform configuration.
The doppler_secret resource lets you create, read, update, and delete secrets. The only nuance is that secrets handled through both means end up being stored in Terraform state. So you have to account for the exposure.
Beyond how Terraform handles secret values in state and authenticates to Doppler locally, you also need to consider how Terraform interacts with Doppler in CI/CD environments. Using OIDC can eliminate the need to store long-lived Doppler tokens in your pipeline.
For CI/CD, you also need to consider how your pipeline authenticates to Doppler before running Terraform. Doppler supports OpenID Connect (OIDC) through service account identities, allowing a supported OIDC provider such as GitHub Actions or CircleCI to exchange its OIDC token for a short-lived Doppler API token.
This removes the need to store a long-lived Doppler token in your CI/CD environment. Your pipeline can authenticate with its workload identity, obtain temporary access to the required Doppler configuration, and then run Terraform with the secrets injected into its environment.
If you already have secrets in your Terraform state, take these steps:






Protect secrets across your entire stack with centralized management, automated workflows, and the flexibility to deploy in the cloud or on-prem.
