variable "device_names" { type = set(string) default = [] } # 1. Generate Private Keys for each device resource "tls_private_key" "device_key" { for_each = var.device_names algorithm = "RSA" rsa_bits = 2048 } # 1. Save the private key temporarily (needed for the openssl command) resource "local_sensitive_file" "tmp_key" { for_each = var.device_names content = tls_private_key.device_key[each.key].private_key_pem filename = "${path.module}/output/${each.key}.key" } # 2. Create Certificate Signing Requests (CSR) resource "tls_cert_request" "device_csr" { for_each = var.device_names private_key_pem = tls_private_key.device_key[each.key].private_key_pem subject { common_name = each.key organization = "My Home Lab" } } # 3. Sign the Certificates using your existing CA # Note: You need your CA cert and key available as files resource "tls_locally_signed_cert" "device_cert" { for_each = var.device_names cert_request_pem = tls_cert_request.device_csr[each.key].cert_request_pem ca_private_key_pem = file("${path.module}/MyPrivateCA.key") ca_cert_pem = file("${path.module}/MyPrivateCA.crt") validity_period_hours = 8760 # 1 year allowed_uses = [ "client_auth", "digital_signature", ] } # 4. EXPORT: Generate the .p12 and the CRL automatically # This runs every time a cert is added OR removed resource "null_resource" "manage_certs" { triggers = { devices = join(",", var.device_names) } # When a cert is REMOVED from the list, Terraform marks it for destruction. # We use a 'destroy' provisioner to trigger the revocation in your CRL. provisioner "local-exec" { when = destroy command = <