oven-sh/bun · error

Failed to import key pair: ${keyName}

Error message

Failed to import key pair: ${keyName}

What it means

importKeyPair() first tries `ec2 import-key-pair`; if that yields nothing it falls back to describeKeyPairs for the name; the error fires only when the import failed AND no key pair with that name already exists — so the import itself errored (bad key material, permissions, region) rather than merely being a duplicate.

Source

Thrown at scripts/machine.mjs:327

    /** @type {AwsKeyPair | undefined} */
    const keyPair = await aws.spawn(
      $`ec2 import-key-pair --key-name ${keyName} --public-key-material ${publicKeyBase64}`,
      {
        throwOnError: error => !/InvalidKeyPair\.Duplicate/i.test(inspect(error)),
      },
    );

    if (keyPair) {
      return keyPair;
    }

    const keyPairs = await aws.describeKeyPairs(keyName);
    if (keyPairs.length) {
      return keyPairs[0];
    }

    throw new Error(`Failed to import key pair: ${keyName}`);
  },

  /**
   * @param {AwsImage | string} imageOrImageId
   * @returns {Promise<AwsImage>}
   */
  async getAvailableImage(imageOrImageId) {
    let imageId = imageOrImageId;
    if (typeof imageOrImageId === "object") {
      const { ImageId, State } = imageOrImageId;
      if (State === "available") {
        return imageOrImageId;
      }
      imageId = ImageId;
    }

    await aws.waitImage("image-available", imageId);
    const [availableImage] = await aws.describeImages({

View on GitHub (pinned to 8c5296ac45)

Solutions

  1. Run `aws ec2 describe-key-pairs --key-names <keyName> --region <region>` to confirm absence
  2. Re-run the import manually with the same PublicKeyMaterial to surface the real error
  3. Grant ec2:ImportKeyPair/ec2:DescribeKeyPairs to the CI role
  4. Retry once after a few seconds in case of eventual consistency
Defensive patterns

Strategy: try-catch

Validate before calling

// Confirm the key is really absent before treating import failure as fatal
const existing = await aws.describeKeyPairs(keyName);
if (!existing.length && !publicKeyMaterial) {
  throw new Error(`cannot import key pair ${keyName}: no key material provided`);
}

Try / catch

try {
  return await aws.importKeyPair(keyName, material);
} catch (error) {
  // Distinguish 'already exists' (fine — describe and reuse) from real failures
  const found = await aws.describeKeyPairs(keyName);
  if (found.length) return found[0];
  throw error;
}

Prevention

When it happens

Trigger: Malformed/unreadable public key material passed to import-key-pair; IAM principal lacking ec2:ImportKeyPair or ec2:DescribeKeyPairs; importing into a different region than the one later described; AWS eventual-consistency lag right after import.

Common situations: CI role permissions narrowed during an IAM cleanup; key file generation step silently failed before this call; region mismatch between import and describe.

Related errors


AI-assisted analysis of oven-sh/bun@8c5296ac45 (2026-08-16). Data as JSON: /api/errors/7167b627fafffb4a. Report an issue: GitHub.