Terraform
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:
- Infisical (you can use Infisical Cloud or a self-hosted instance of Infisical) to store your secrets
- The Terraform Provider to fetch secrets for your infrastructure
Prerequisites
Before you begin, make sure you have:
- Terraform installed (v1.10.0+ for ephemeral resources)
- An Infisical account with access to a project
- Basic understanding of Terraform and infrastructure as code
Project Setup
Configure Provider
First, specify the Infisical provider in your Terraform configuration:
Authentication
Configure the provider using one of these authentication methods:
Machine Identity (Recommended)
Using a Machine Identity, you can authenticate your Terraform provider using either OIDC Auth or Universal Auth methods.
Learn more about machine identities.
Service Token (Legacy)
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.
Using Secrets in Terraform
Infisical provides two methods to fetch and use secrets in your Terraform configurations:
Method 1: Ephemeral Resources (Recommended)
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.
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
Method 2: Data Sources
For backwards compatibility or when working with older Terraform versions, you can use the traditional data source approach:
When using data sources, secret values are stored in Terraform’s state file. Ensure your state file is properly secured.
Common Use Cases
Secure Database Credential Management
Manage database credentials securely without exposing sensitive information in your state files:
GitOps Workflow with OIDC
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:
For detailed instructions on setting up OIDC authentication with GitHub Actions, refer to our GitHub Actions OIDC guide.
Best Practices
-
Use Ephemeral Resources: Whenever possible, use ephemeral resources instead of data sources for improved security.
-
Organize Secrets: Structure your secrets in Infisical using folders to maintain clean separation:
-
Variable Usage: Use Terraform variables for workspace IDs and environment slugs:
-
Error Handling: Add lifecycle blocks for critical secrets:
FAQ
See also:
Was this page helpful?