Working with Amazon EC2 Key Pairs - AWS SDK for JavaScript

We announced the upcoming end-of-support for AWS SDK for JavaScript v2. We recommend that you migrate to AWS SDK for JavaScript v3. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

Working with Amazon EC2 Key Pairs

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • How to retrieve information about your key pairs.

  • How to create a key pair to access an Amazon EC2 instance.

  • How to delete an existing key pair.

The Scenario

Amazon EC2 uses public–key cryptography to encrypt and decrypt login information. Public–key cryptography uses a public key to encrypt data, then the recipient uses the private key to decrypt the data. The public and private keys are known as a key pair.

In this example, you use a series of Node.js modules to perform several Amazon EC2 key pair management operations. The Node.js modules use the SDK for JavaScript to manage instances by using these methods of the Amazon EC2 client class:

For more information about the Amazon EC2 key pairs, see Amazon EC2 Key Pairs in the Amazon EC2 User Guide for Linux Instances or Amazon EC2 Key Pairs and Windows Instances in the Amazon EC2 User Guide for Windows Instances.

Prerequisite Tasks

To set up and run this example, first complete these tasks:

Describing Your Key Pairs

Create a Node.js module with the file name ec2_describekeypairs.js. Be sure to configure the SDK as previously shown. To access Amazon EC2, create an AWS.EC2 service object. Create an empty JSON object to hold the parameters needed by the describeKeyPairs method to return descriptions for all your key pairs. You can also provide an array of names of key pairs in the KeyName portion of the parameters in the JSON file to the describeKeyPairs method.

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create EC2 service object var ec2 = new AWS.EC2({ apiVersion: "2016-11-15" }); // Retrieve key pair descriptions; no params needed ec2.describeKeyPairs(function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", JSON.stringify(data.KeyPairs)); } });

To run the example, type the following at the command line.

node ec2_describekeypairs.js

This sample code can be found here on GitHub.

Creating a Key Pair

Each key pair requires a name. Amazon EC2 associates the public key with the name that you specify as the key name. Create a Node.js module with the file name ec2_createkeypair.js. Be sure to configure the SDK as previously shown. To access Amazon EC2, create an AWS.EC2 service object. Create the JSON parameters to specify the name of the key pair, then pass them to call the createKeyPair method.

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create EC2 service object var ec2 = new AWS.EC2({ apiVersion: "2016-11-15" }); var params = { KeyName: "KEY_PAIR_NAME", }; // Create the key pair ec2.createKeyPair(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log(JSON.stringify(data)); } });

To run the example, type the following at the command line.

node ec2_createkeypair.js

This sample code can be found here on GitHub.

Deleting a Key Pair

Create a Node.js module with the file name ec2_deletekeypair.js. Be sure to configure the SDK as previously shown. To access Amazon EC2, create an AWS.EC2 service object. Create the JSON parameters to specify the name of the key pair you want to delete. Then call the deleteKeyPair method.

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create EC2 service object var ec2 = new AWS.EC2({ apiVersion: "2016-11-15" }); var params = { KeyName: "KEY_PAIR_NAME", }; // Delete the key pair ec2.deleteKeyPair(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Key Pair Deleted"); } });

To run the example, type the following at the command line.

node ec2_deletekeypair.js

This sample code can be found here on GitHub.