Skip to content
betterstack-alchemyUnofficial

Cloud.SshKey

Kind ResourceType id BetterStack.Cloud.SshKeySource src/Cloud/SshKey.tsLive test available A live lifecycle test exists: it runs against the offline mock by default and against the real API under bun run test:live. This records that the test exists, not a dated live run (see Compatibility). Test file: test/Cloud/SshKey.test.ts.

An SSH key for the root user of Better Stack Cloud servers. It is added to a server when the server is created, or later through serverIds (attach and detach run as their own API actions, in place).

Keys are global to the team: a key created in one zone exists in every zone. publicKey, slug and teamId can’t change: changing one replaces the key. A deleted key’s slug stays taken until the delete finishes in every zone, so replacing a key that keeps an explicit slug deletes the old key first and retries the create (for up to 2 minutes) while the API rejects that slug; leave slug out to get a fresh generated one instead.

Deleting. The API refuses to delete a key any server still has, so the delete first detaches it from every server (detach with all: true), then deletes, retrying for up to 2 minutes. A server destroyed outside the stack keeps listing its keys until Better Stack purges its record and can’t be detached from: then the delete fails with SshKeyHeld naming that server, the state row stays, and a later destroy succeeds once the record is gone (observed live, 2026-12-01).

Ownership. Every object the stack creates carries an alchemy entry in its metadata (<stack>/<stage>/<resource id>#<instance>), added on every write and left out of the attributes and of diffs; your own keys are untouched (it takes one of the 10 slots). A deploy that lost track of an object it created (a create that failed or was interrupted after the API accepted it) finds it by that marker and converges it instead of creating another; list (and so alchemy nuke) returns only Alchemy-created objects; an object that matches but carries no marker of this resource is taken over only with --adopt.

Generated slug

const key = yield* BetterStack.Cloud.SshKey("deploy", {
publicKey: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEF0uIiLF54lpavoiG7O95cChLJ80vbi0Nh08Kf3+G/a me@example.com",
});

Explicit slug and tags

yield* BetterStack.Cloud.SshKey("laptop", {
slug: "my-laptop",
publicKey: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEF0uIiLF54lpavoiG7O95cChLJ80vbi0Nh08Kf3+G/a me@example.com",
metadata: { owner: "platform" },
});

Add the key to running servers

yield* BetterStack.Cloud.SshKey("ops", {
publicKey: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEF0uIiLF54lpavoiG7O95cChLJ80vbi0Nh08Kf3+G/a me@example.com",
serverIds: ["srv_123"],
});

Reference a key by slug

const existing = BetterStack.Cloud.SshKey.lookup({ slug: "my-laptop" });

publicKey string required

Exactly one OpenSSH public key. Never send a private key. The CLI can read it from a file or from standard input, or import it from GitHub. The public key can't be changed after the key is created.

availabilityZone string

Availability zone the resource lives in, such as us-west-2a. Defaults to the credentials' zone (BETTERSTACK_AVAILABILITY_ZONE, or the profile's). Changing it replaces the resource. A child of a resource in another zone (a firewall rule, a node pool, a snapshot of an instance outside the credentials' zone) must repeat the parent's availabilityZone: it receives only the parent's id, so its zone cannot be derived.

metadata CreateSshKeyRequestMetadataMap

Your tags, as an object of string keys and values, at most 10. An update replaces every tag, so an empty object removes all tags.

serverIds string[]

The IDs of the servers to add the new key to, at most 100, in any zone. Only running servers get the key, and the key is created even when a server can't get it.

slug string

The key's name, unique in its team: up to 255 characters, starting and ending with a letter or digit, with dots, hyphens and underscores in between. Defaults to a unique name generated from the stack, stage and logical id. Changing it replaces the key.

teamId string

Team that owns the resource, such as t123. Defaults to the credentials' team (BETTERSTACK_TEAM_ID, or the profile's team). Changing it replaces the resource.

availabilityZone string always set

Availability zone the resource lives in, such as us-west-2a.

slug string always set

The key's name, unique in its team. It identifies the key.

createdAt string

When the key was created.

id string

The ID of the SSH key.

metadata SshKeyMetadataMap

Your tags, as an object of string keys and values, at most 10. An update replaces every tag, so an empty object removes all tags.

publicKey string

Exactly one OpenSSH public key. Never send a private key. The CLI can read it from a file or from standard input, or import it from GitHub. The public key can't be changed after the key is created.

serverIds string[]

The servers the key was attached to by this stack (the API never returns them).

servers SshKeyServers

What we did with each server.

teamId string

The ID of the team that owns the key. betterstack team list shows it.

updatedAt string

When the key was last updated.

SshKey.lookup(props) reads an object this stack does not manage. It is never created, updated or deleted.

slug string required

The key's slug.

availabilityZone string

Zone to look in, such as us-west-2a. Keys exist in every zone. Defaults to the credentials' zone.