PrivateKey class

> If the managed resource supports a write-only attribute for the private key (first introduced in Terraform 1.11), then the ephemeral variant of tls.PrivateKey should be used, when possible, to avoid storing the private key data in the plan or state file.

Creates a PEM (and OpenSSH) formatted private key.

Generates a secure private key and encodes it in PEM (RFC 1421) and OpenSSH PEM (RFC 4716) formats. This resource is primarily intended for easily bootstrapping throwaway development environments.

> Security Notice The private key generated by this resource will be stored unencrypted in your Terraform state file. Use of this resource for production deployments is not recommended. Instead, generate a private key file outside of Terraform and distribute it securely to the system where Terraform will be run.

This is a logical resource, so it contributes only to the current Terraform state and does not create any external managed resources.

Example Usage

import * as pulumi from "@pulumi/pulumi";
import * as tls from "@pulumi/tls";

// ECDSA key with P384 elliptic curve
const ecdsa_p384_example = new tls.PrivateKey("ecdsa-p384-example", {
    algorithm: "ECDSA",
    ecdsaCurve: "P384",
});
// RSA key of size 4096 bits
const rsa_4096_example = new tls.PrivateKey("rsa-4096-example", {
    algorithm: "RSA",
    rsaBits: 4096,
});
// ED25519 key
const ed25519_example = new tls.PrivateKey("ed25519-example", {algorithm: "ED25519"});
import pulumi
import pulumi_tls as tls

# ECDSA key with P384 elliptic curve
ecdsa_p384_example = tls.PrivateKey("ecdsa-p384-example",
    algorithm="ECDSA",
    ecdsa_curve="P384")
# RSA key of size 4096 bits
rsa_4096_example = tls.PrivateKey("rsa-4096-example",
    algorithm="RSA",
    rsa_bits=4096)
# ED25519 key
ed25519_example = tls.PrivateKey("ed25519-example", algorithm="ED25519")
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Tls = Pulumi.Tls;

return await Deployment.RunAsync(() =>
{
    // ECDSA key with P384 elliptic curve
    var ecdsa_p384_example = new Tls.PrivateKey("ecdsa-p384-example", new()
    {
        Algorithm = "ECDSA",
        EcdsaCurve = "P384",
    });

    // RSA key of size 4096 bits
    var rsa_4096_example = new Tls.PrivateKey("rsa-4096-example", new()
    {
        Algorithm = "RSA",
        RsaBits = 4096,
    });

    // ED25519 key
    var ed25519_example = new Tls.PrivateKey("ed25519-example", new()
    {
        Algorithm = "ED25519",
    });

});
package main

import (
	"github.com/pulumi/pulumi-tls/sdk/v5/go/tls"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		// ECDSA key with P384 elliptic curve
		_, err := tls.NewPrivateKey(ctx, "ecdsa-p384-example", &tls.PrivateKeyArgs{
			Algorithm:  pulumi.String("ECDSA"),
			EcdsaCurve: pulumi.String("P384"),
		})
		if err != nil {
			return err
		}
		// RSA key of size 4096 bits
		_, err = tls.NewPrivateKey(ctx, "rsa-4096-example", &tls.PrivateKeyArgs{
			Algorithm: pulumi.String("RSA"),
			RsaBits:   pulumi.Int(4096),
		})
		if err != nil {
			return err
		}
		// ED25519 key
		_, err = tls.NewPrivateKey(ctx, "ed25519-example", &tls.PrivateKeyArgs{
			Algorithm: pulumi.String("ED25519"),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
pulumi {
  required_providers {
    tls = {
      source = "pulumi/tls"
    }
  }
}

# ECDSA key with P384 elliptic curve
resource "tls_privatekey" "ecdsa-p384-example" {
  algorithm   = "ECDSA"
  ecdsa_curve = "P384"
}
# RSA key of size 4096 bits
resource "tls_privatekey" "rsa-4096-example" {
  algorithm = "RSA"
  rsa_bits  = 4096
}
# ED25519 key
resource "tls_privatekey" "ed25519-example" {
  algorithm = "ED25519"
}
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.tls.PrivateKey;
import com.pulumi.tls.PrivateKeyArgs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) {
        Pulumi.run(App::stack);
    }

    public static void stack(Context ctx) {
        // ECDSA key with P384 elliptic curve
        var ecdsa_p384_example = new PrivateKey("ecdsa-p384-example", PrivateKeyArgs.builder()
            .algorithm("ECDSA")
            .ecdsaCurve("P384")
            .build());

        // RSA key of size 4096 bits
        var rsa_4096_example = new PrivateKey("rsa-4096-example", PrivateKeyArgs.builder()
            .algorithm("RSA")
            .rsaBits(4096)
            .build());

        // ED25519 key
        var ed25519_example = new PrivateKey("ed25519-example", PrivateKeyArgs.builder()
            .algorithm("ED25519")
            .build());

    }
}
resources:
  # ECDSA key with P384 elliptic curve
  ecdsa-p384-example:
    type: tls:PrivateKey
    properties:
      algorithm: ECDSA
      ecdsaCurve: P384
  # RSA key of size 4096 bits
  rsa-4096-example:
    type: tls:PrivateKey
    properties:
      algorithm: RSA
      rsaBits: 4096
  # ED25519 key
  ed25519-example:
    type: tls:PrivateKey
    properties:
      algorithm: ED25519

Generating a New Key

Since a private key is a logical resource that lives only in the Terraform state, it will persist until it is explicitly destroyed by the user.

In order to force the generation of a new key within an existing state, the private key instance can be "tainted":

terraform taint tls_private_key.example

A new key will then be generated on the next pulumi up.

Constructors

PrivateKey(String name, {PrivateKeyArgs? args, CustomResourceOptions? options})
Creates a new PrivateKey. name The Pulumi resource name. args Arguments used to configure this PrivateKey. The set of arguments for PrivateKey. options Resource options controlling this resource's behavior.
PrivateKey.reference(String urn)
Creates a typed reference to an existing PrivateKey resource.

Properties

algorithm ↔ Output<String>
Name of the algorithm to use when generating the private key. Currently-supported values are: RSA, ECDSA, ED25519.
latefinal
childResources → Set<Resource>
finalinherited
completionSources ↔ Map<String, IOutputCompletionSource>
latefinalinherited
ecdsaCurve ↔ Output<String>
When algorithm is ECDSA, the name of the elliptic curve to use. Currently-supported values are: P224, P256, P384, P521. (default: P224).
latefinal
hashCode → int
The hash code for this object.
no setterinherited
id ↔ Output<String>
getter/setter pairinherited
isCustom → bool
Returns whether this resource is provider-managed.
no setterinherited
isProtected → bool
Returns whether this resource is protected from deletion.
no setterinherited
isRemote → bool
Whether this resource is registered as remote.
no setterinherited
isResourceReference → bool
Whether this instance represents a resource value returned over RPC.
finalinherited
privateKeyOpenssh ↔ Output<String>
Private key data in OpenSSH PEM (RFC 4716) format.
latefinal
privateKeyPem ↔ Output<String>
Private key data in PEM (RFC 1421) format.
latefinal
privateKeyPemPkcs8 ↔ Output<String>
Private key data in PKCS#8 PEM (RFC 5208) format.
latefinal
publicKeyFingerprintMd5 ↔ Output<String>
The fingerprint of the public key data in OpenSSH MD5 hash format, e.g. aa:bb:cc:.... Only available if the selected private key format is compatible, similarly to publicKeyOpenssh and the ECDSA P224 limitations.
latefinal
publicKeyFingerprintSha256 ↔ Output<String>
The fingerprint of the public key data in OpenSSH SHA256 hash format, e.g. SHA256:.... Only available if the selected private key format is compatible, similarly to publicKeyOpenssh and the ECDSA P224 limitations.
latefinal
publicKeyOpenssh ↔ Output<String>
The public key data in "Authorized Keys" format. This is not populated for ECDSA with curve P224, as it is not supported. NOTE: the underlying libraries that generate this value append a \n at the end of the PEM. In case this disrupts your use case, we recommend using trimspace().
latefinal
publicKeyPem ↔ Output<String>
Public key data in PEM (RFC 1421) format. NOTE: the underlying libraries that generate this value append a \n at the end of the PEM. In case this disrupts your use case, we recommend using trimspace().
latefinal
resourceTransforms → List<ResourceTransform>
Inherited/explicit async transforms.
no setterinherited
rsaBits ↔ Output<int>
When algorithm is RSA, the size of the generated RSA key, in bits (default: 2048).
latefinal
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
transformations → List<ResourceTransformation>
Inherited/explicit legacy transformations.
no setterinherited
urn ↔ Output<String>
latefinalinherited

Methods

failId(Object error) → void
Completes this resource ID with an error when registration fails.
inherited
failOutputs(Object error) → void
Completes all output properties with error.
inherited
failUrn(Object error) → void
Completes this resource URN with an error when registration fails.
inherited
getProvider(String moduleMember) → ProviderResource?
Returns provider for moduleMember's package, if configured.
inherited
getResourceName() → String
Returns this resource's logical name.
inherited
getResourceType() → String
Returns this resource's Pulumi type token.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerOutput<T>(String propertyName, {Object? decoder(Object?)?, bool isSecret = false}) → Output<T>
Registers a dynamic output property for this resource.
inherited
resolveId(String? value, {required bool isKnown}) → void
Resolves the provider-assigned ID for this resource.
inherited
resolveOutputs(Struct outputs) → void
Resolves all output properties from a monitor response payload.
inherited
resolveUrn(String value) → void
Resolves this resource's URN once assigned by the engine.
inherited
serializeProperties(Map<String, dynamic> properties) → Future<Struct>
Serializes resource properties for RPC transmission.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

get(String name, Input<String> id, {PrivateKeyState? state, CustomResourceOptions? options}) → PrivateKey
Gets an existing PrivateKey resource's state with the given name and id.