Skip to content
GitHubXDiscordRSS

KeyPair

Learn how to create, update, and manage AWS EC2 KeyPairs using Alchemy Cloud Control.

The KeyPair resource lets you manage AWS EC2 KeyPairs used for secure access to your EC2 instances.

Create a basic EC2 KeyPair with a specified name and default options.

import AWS from "alchemy/aws/control";
const basicKeyPair = await AWS.EC2.KeyPair("myKeyPair", {
KeyName: "my-key-pair",
KeyType: "rsa" // Optional: Specifies the type of key
});

Create an EC2 KeyPair with a public key material to import an existing key.

const importedKeyPair = await AWS.EC2.KeyPair("importedKeyPair", {
KeyName: "imported-key-pair",
PublicKeyMaterial: "ssh-rsa AAAAB3...your-public-key..." // Replace with your actual public key
});

Create a KeyPair and apply tags for better resource management.

const taggedKeyPair = await AWS.EC2.KeyPair("taggedKeyPair", {
KeyName: "tagged-key-pair",
Tags: [
{ Key: "Environment", Value: "Production" },
{ Key: "Project", Value: "WebApp" }
]
});

If you want to adopt an existing EC2 KeyPair without failing if it already exists, set the adopt property to true.

const adoptedKeyPair = await AWS.EC2.KeyPair("adoptedKeyPair", {
KeyName: "existing-key-pair",
adopt: true // Set to true to adopt an existing resource
});