Learn how to fetch secrets from Infisical with Terraform using both traditional data sources and ephemeral resources
This guide demonstrates how to use Infisical to manage secrets in your Terraform infrastructure code, supporting both traditional data sources and ephemeral resources for enhanced security. It uses:
Machine Identity authentication is strongly recommended as the secure and modern method. Service tokens are considered legacy and will be deprecated in a future release.
Ephemeral resources, introduced in Terraform v1.10, provide enhanced security by ensuring sensitive values are never persisted in state files. This is the recommended approach for handling secrets in your infrastructure code.
Copy
Ask AI
# Fetch database credentials ephemerallyephemeral "infisical_secret" "db_creds" { name = "DB_CREDENTIALS" env_slug = "prod" workspace_id = var.infisical_workspace_id folder_path = "/database"}# Use the credentials to configure a providerprovider "postgresql" { host = data.aws_db_instance.example.address port = data.aws_db_instance.example.port username = jsondecode(ephemeral.infisical_secret.db_creds.value)["username"] password = jsondecode(ephemeral.infisical_secret.db_creds.value)["password"]}
Key benefits:
Values are never stored in state files
Secrets are fetched on-demand during each Terraform operation
Perfect for GitOps workflows
Improved security posture for your infrastructure as code
To eliminate the need for static credentials, you can authenticate your workflow using OpenID Connect (OIDC) through providers like the Infisical Secrets GitHub Action.
Once authenticated, you can securely access secrets through the Infisical provider:
Copy
Ask AI
provider "infisical" { # Auth credentials automatically injected from the environment}# Fetch deployment credentialsephemeral "infisical_secret" "deploy_token" { name = "DEPLOY_TOKEN" env_slug = "prod" workspace_id = var.infisical_workspace_id folder_path = "/deployment"}
For detailed instructions on setting up OIDC authentication with GitHub Actions, refer to our GitHub Actions OIDC guide.
What happens if I'm using an older version of Terraform?
If you’re using Terraform < v1.10.0, you’ll need to use the data source approach.
Consider upgrading to take advantage of the enhanced security features provided
by ephemeral resources.
Can I mix ephemeral resources and data sources?
Yes, you can use both in the same configuration. However, we recommend using
ephemeral resources for any sensitive values to ensure they’re not stored in state.
How do I secure my state file when using data sources?
When using data sources, follow Terraform’s best practices for state management:
Use remote state with encryption at rest
Implement proper access controls
Consider using state encryption
Treat the state like a secret
Better yet, use ephemeral resources to avoid storing sensitive values in state entirely.