Defining Public Key Infrastructure as Code for Automated Renewals

The intersection of Infrastructure as Code (IaC) and Public Key Infrastructure (PKI) has shifted from an optional DevOps optimization to a strict security mandate. The primary catalyst for this shift ...

Tim Henrich
December 03, 2026
6 min read
15 views

Defining Public Key Infrastructure as Code for Automated Renewals

The intersection of Infrastructure as Code (IaC) and Public Key Infrastructure (PKI) has shifted from an optional DevOps optimization to a strict security mandate. The primary catalyst for this shift is the shrinking lifespan of digital certificates. With Google Chrome's proposal to reduce the maximum validity of public TLS certificates from 398 days to just 90 days, the industry is bracing for a reality where manual certificate management is mathematically impossible to maintain at scale.

When certificates expire every three months, IT ticketing systems and manual deployment checklists buckle under the volume. Organizations are now forced to treat certificates as ephemeral infrastructure—short-lived, easily replaceable, and provisioned entirely through code. Integrating certificate provisioning into IaC pipelines using tools like Terraform, OpenTofu, and Pulumi ensures crypto-agility, eliminates human error, and prevents the costly outages associated with expired machine identities.

This shift, however, introduces complex challenges. Generating cryptographic material within deployment pipelines requires strict boundaries to prevent secrets sprawl and state file compromises.

The State File Vulnerability in Native IaC Key Generation

The most common mistake engineering teams make when adopting IaC for certificate management is treating private keys like standard infrastructure attributes. When using providers to generate certificates natively, such as the Terraform tls provider, the resulting private key is stored in plaintext within the terraform.tfstate file.

Consider a scenario where an engineer uses the tls_private_key resource to generate an RSA key for a new web server. While the infrastructure provisions successfully, the raw, unencrypted private key now lives in the state file. If that state file is stored in an improperly secured S3 bucket, committed to a version control system by mistake, or accessed by an over-privileged CI/CD pipeline, the private key is compromised. Consequently, every certificate associated with that key is also compromised.

To solve this, architectural patterns must decouple key generation from the IaC state. Infrastructure as Code should be used to configure the infrastructure to request its own certificates, or it should interface with external secret managers where the API call is triggered by the pipeline, but the private key never touches the state file.

Architectural Patterns for Automated Certificate Management

Building a secure, automated PKI requires selecting the right pattern for your specific environment. Whether you are operating entirely in a public cloud, running cloud-native workloads on Kubernetes, or managing a hybrid enterprise environment, there is a secure IaC pattern to match.

Cloud-Native Managed Certificates with AWS ACM

For organizations heavily invested in Amazon Web Services, utilizing AWS Certificate Manager (ACM) in conjunction with Route 53 provides the safest basic pattern for certificate automation. In this architecture, AWS generates and manages the private key internally. The private key never touches your Terraform or OpenTofu state file, entirely mitigating the secrets sprawl vulnerability.

By defining the certificate and the DNS validation records in the same configuration, you create a self-validating infrastructure block. When applied, the IaC tool requests the certificate, reads the required DNS challenge from the ACM API, provisions the CNAME record in Route 53, and waits for AWS to validate domain ownership.

resource "aws_acm_certificate" "main" {
  domain_name       = "api.example.com"
  validation_method = "DNS"

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_route53_record" "validation" {
  for_each = {
    for dvo in aws_acm_certificate.main.domain_validation_options : dvo.domain_name => {
      name   = dvo.resource_record_name
      record = dvo.resource_record_value
      type   = dvo.resource_record_type
    }
  }

  allow_overwrite = true
  name            = each.value.name
  records         = [each.value.record]
  ttl             = 60
  type            = each.value.type
  zone_id         = data.aws_route53_zone.main.zone_id
}

resource "aws_acm_certificate_validation" "main" {
  certificate_arn         = aws_acm_certificate.main.arn
  validation_record_fqdns = [for record in aws_route53_record.validation : record.fqdn]
}

This pattern ensures that when the certificate nears expiration, AWS automatically handles the renewal process because the DNS validation record remains in place. The infrastructure code simply ensures the configuration remains consistent.

Kubernetes Native PKI with GitOps and cert-manager

For containerized workloads, the de facto standard for certificate automation is cert-manager. When combined with a GitOps workflow using tools like ArgoCD or Flux, your entire certificate infrastructure is defined in version-controlled YAML files, providing a complete audit trail of who requested a certificate and when.

In this pattern, the IaC pipeline does not provision the certificate directly. Instead, it deploys the cert-manager operator and defines ClusterIssuer resources that connect to external Certificate Authorities like Let's Encrypt via the ACME protocol.

Developers then define Certificate Custom Resource Definitions (CRDs) alongside their application deployments.

apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: letsencrypt-production
spec:
  acme:
    server: https://acme-v02.api.letsencrypt.org/directory
    email: security@example.com
    privateKeySecretRef:
      name: letsencrypt-production-account-key
    solvers:
    - http01:
        ingress:
          class: nginx
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: api-example-com-tls
  namespace: production
spec:
  secretName: api-example-com-tls-secret
  duration: 2160h # 90 days
  renewBefore: 720h # 30 days
  issuerRef:
    name: letsencrypt-production
    kind: ClusterIssuer
  dnsNames:
  - api.example.com

When this configuration is applied to the cluster, cert-manager handles the Certificate Signing Request (CSR) generation, executes the HTTP-01 or DNS-01 challenge, and stores the resulting cryptographic keypair securely in a Kubernetes Secret. The application pods can then mount this secret to serve TLS traffic. This approach treats certificates as highly ephemeral, allowing organizations to issue short-lived certificates (often 24 hours for internal mTLS) without any manual intervention.

Enterprise PKI Integration with HashiCorp Vault

In hybrid environments or organizations with strict internal compliance requirements, public certificate authorities are often insufficient for internal service-to-service communication. These organizations typically rely on centralized secret managers like HashiCorp Vault to act as an internal Root or Intermediate Certificate Authority.

When using OpenTofu or Terraform to request certificates from Vault, special care must be taken to protect the outputs. The vault_pki_secret_backend_cert resource allows the IaC pipeline to request a certificate, but it will return the private key.

To secure this workflow, you must mark the output variables as sensitive and ensure your remote state backend is encrypted at rest using a Key Management Service (KMS).

resource "vault_pki_secret_backend_cert" "internal_service" {
  backend = "pki_int"
  name    = "internal-services"

  common_name = "service.internal.example.com"
  ttl         = "720h" # 30 days
}

output "certificate" {
  value       = vault_pki_secret_backend_cert.internal_service.certificate
  description = "The public certificate"
}

output "private_key" {
  value       = vault_pki_secret_backend_cert.internal_service.private_key
  description = "The private key for the service"
  sensitive   = true
}

By setting sensitive = true, you prevent the IaC tool from printing the private key to the console logs during a CI/CD run. This pattern is highly effective for provisioning certificates for virtual machines, load balancers, and database servers that do not natively support ACME clients.

Preparing for Post-Quantum Cryptography

The push toward IaC for certificate management is not solely driven by shrinking lifespans; it is also a prerequisite for the impending transition to Post-Quantum Cryptography (PQC). In August 2024, the National Institute of Standards and Technology (NIST) finalized the first three PQC algorithms (FIPS 203, 204, and 205).

Quantum computers capable of breaking current RSA and ECC encryption are approaching reality, forcing organizations to build "crypto-agile" infrastructure. Crypto-agility is the ability to swap out cryptographic algorithms and key sizes rapidly without disrupting the underlying system architecture.

If your certificates are managed manually or via disjointed bash scripts, migrating thousands of endpoints to quantum-resistant algorithms will take years and incur massive operational risk. However, if your PKI is defined as code, this migration becomes a variables update. By centralizing certificate policies in a single repository, security teams can update a Helm chart or a Terraform variable to issue FIPS 204 compliant certificates, roll out the changes via the CI/CD pipeline, and upgrade the entire infrastructure fleet in a matter of hours.

Compliance and State File Security

The operational benefits of defining PKI as code are matched by significant compliance advantages. Regulatory frameworks are increasingly focused on cryptographic inventories and automated remediation.

The PCI-DSS v4.0 standard mandates that organizations maintain a strict, documented inventory of bespoke and custom cryptography and demonstrate the ability to respond rapidly to vulnerabilities

Share This Insight

Related Posts