Terraform Provider: TLS
repository·main·Indexed 42 days ago
https://github.com/hashicorp/terraform-provider-tlsThe Terraform TLS provider manages Transport Layer Security (TLS) resources, including private keys, certificates, and certificate requests. It supports cryptographic formats such as PEM, X.509, and PKCS #8, and key algorithms including RSA, ECDSA (P224, P256, P384, P521), and ED25519. The provider includes data sources for fetching certificate info from URLs and extracting public keys, as well as ephemeral resources for secure private key generation.
What's inside Terraform TLS Provider
- The TLS provider provides utilities for working with Transport Layer Security (TLS) keys and certificates. It allows users to manage private keys, certificates, and certificate requests as part of a Terraform deployment.
Overview of the TLS Provider
mainThe TLS provider provides utilities for working with Transport Layer Security (TLS) keys and certificates (also known as SSL). It allows you to create private keys, certificates, and certificate requests as part of a Terraform deployment.
While the provider is often used in conjunction with other providers (e.g., passing a generated certificate to an AWS IAM resource), it is primarily used to provision the cryptographic material required for TLS services.
What is the tls_private_key ephemeral resource and when to use it
mainThe
tls_private_keyephemeral resource creates a secure private key encoded in PEM (RFC 1421) and OpenSSH PEM (RFC 4716) formats.Primary Use Case: Use this ephemeral resource in combination with a write-only argument in a managed resource (a feature introduced in Terraform 1.11). This pattern prevents Terraform from storing the sensitive private key data in your plan or state files, significantly improving security.
Note: If the resource you are interacting with does not support a write-only argument for the private key, you should use the standard managed resource variant of
tls_private_keyinstead.Security Warning: Secrets and Terraform state
mainResources like
tls_private_keyare marked as sensitive to prevent accidental leakage in logs. However, the values (including secrets) are stored unencrypted in the Terraform state file.Recommendations:
- Production Use: Using these resources for production deployments is not recommended due to the unencrypted state storage.
- Mitigation: If you must use them, strongly protect the content of your Terraform state file.
- Best Practice: Generate secrets outside of Terraform and distribute them securely to the systems that require them.
How automatic renewal works in tls_locally_signed_cert
mainThe
tls_locally_signed_certresource supports a mechanism for automatic renewal.An instance is considered ready for renewal (setting
ready_for_renewaltotrue) when:- The
validity_period_hourshas elapsed (the certificate is expired). - The current time is within the
early_renewal_hourswindow before actual expiration.
When
ready_for_renewalistrue, applying the Terraform configuration will trigger the generation of a new certificate. This is particularly useful in development environments to ensure certificates are rotated automatically before they expire.Warning: Because this resource does not support certificate revocation, the old certificate remains valid until its true expiration time. Additionally, updating the certificate may cause dependent resources to be replaced depending on their lifecycle rules.
- The
Rotate private keys without persisting to state using `private_key_pem_wo`
mainFor security-sensitive workflows, you can use the write-only argument
private_key_pem_wo(available in Terraform 1.11+). Unlike the standardprivate_key_pem, the value provided toprivate_key_pem_wois never stored in the Terraform state file.To rotate the key using this method:
- Set
private_key_pem_woto your new PEM key. - Increment the
private_key_pem_wo_versionnumber. This version is the only signal the provider uses to detect that the write-only key has changed and triggers a re-issue.
resource "tls_self_signed_cert" "example" { # Use write-only version to avoid storing key in state private_key_pem_wo = file("new_private_key.pem") private_key_pem_wo_version = 2 # ... other required arguments ... }- Set
Use the `tls_private_key` ephemeral resource
mainThe
tls_private_keyephemeral resource generates a secure private key in PEM and OpenSSH formats.Primary Use Case: Use this resource in combination with a write-only argument in a managed resource (a feature introduced in Terraform 1.11). This pattern prevents the private key data from being stored in your Terraform plan or state file, significantly improving security.
If a managed resource does not support a write-only argument for a private key, you must use the standard managed resource variant of
tls_private_keyinstead of this ephemeral version.# Example of an ephemeral ECDSA key ephemeral "tls_private_key" "ecdsa-p384-example" { algorithm = "ECDSA" ecdsa_curve = "P384" }How automatic renewal works for `tls_self_signed_cert`
mainThe
tls_self_signed_certresource supports automatic renewal via thereadyForRenewalattribute and theearlyRenewalHoursconfiguration.- Trigger: The resource becomes
readyForRenewal = truewhen the certificate expires (based onvalidityPeriodHours) OR when the current time falls within theearlyRenewalHourswindow before actual expiry. - Action: When
readyForRenewalis true, applying the Terraform configuration will cause a new certificate to be generated. - Usage: This is useful in development environments to automatically provision new certificates before the old ones expire.
Note: This does not support certificate revocation. The old certificate remains valid until its true expiration time. Applying the update only works if the Terraform configuration is applied during the early renewal period.
- Trigger: The resource becomes
Rotate write-only private keys in tls_cert_request
mainWhen using the write-only private key argument
private_key_pem_wo(available in Terraform 1.11+), the key value is not stored in the Terraform state. To trigger a re-issuance of the Certificate Signing Request (CSR) when you rotate the key, you must manually increment theprivate_key_pem_wo_versionnumber. This version acts as the signal to the provider that the key has changed.resource "tls_cert_request" "example" { private_key_pem_wo = var.new_private_key private_key_pem_wo_version = 2 # Increment this to rotate the key subject { common_name = "example.com" } }How automatic renewal works in `tls_self_signed_cert`
mainThe
tls_self_signed_certresource supports automatic renewal to facilitate frequent deployments in development environments.Renewal is triggered when the
ready_for_renewalattribute becomestrue. This happens under two conditions:- The certificate has expired (exceeded
validity_period_hours). - The certificate has entered the early renewal period (within
early_renewal_hoursof expiry).
When
ready_for_renewalistrue, applying the Terraform configuration will generate a new certificate. Note that applying a new certificate may cause dependent resources to be updated or replaced depending on their lifecycle rules.- The certificate has expired (exceeded
Use the tls_certificate data source to inspect TLS certificates
mainThe
tls_certificatedata source allows you to retrieve information about the TLS certificates securing a host. You can use it to extract details like the SHA1 fingerprint, serial number, or issuer information.You can provide the certificate information in one of two ways:
- Via URL: Provide a
urlto fetch certificates from a live website. This method supportsverify_chainto validate the certificate chain. - Via Content: Provide the raw certificate content in PEM (RFC 1421) format using the
contentargument.
Note: You cannot use both
urlandcontentin the same data source block. If usingurl, you can also setverify_chain(defaults totrue).data "tls_certificate" "example" { url = "https://example.com" } # OR data "tls_certificate" "example_content" { content = file("example.pem") }- Via URL: Provide a
Use automatic renewal with `LocallySignedCert`
mainThe
LocallySignedCertresource supports automatic renewal via thereadyForRenewalattribute.An instance is considered ready for renewal if:
- The certificate has expired (exceeded
validityPeriodHours). - The certificate is within the
earlyRenewalHourswindow before its actual expiration.
When
readyForRenewalistrue, applying the Terraform configuration will trigger the generation of a new certificate. This is useful in development environments to ensure certificates are rotated before they expire.Warning: This resource does not support certificate revocation. The old certificate remains valid until its true expiration time.
- The certificate has expired (exceeded