Menu
AWS SDK for JavaScript
Developer Guide for SDK v2.152.0

Working with Amazon EC2 Key Pairs


                                            Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to:

  • Retrieve information about your key pairs

  • Create a key pair to access an Amazon EC2 instance

  • 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:

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object then setting the region for your code. In this example, the region is set to us-west-2.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'us-west-2'});

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 to the describeKeyPairs method.

Copy
// 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.

Copy
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.

Copy
// 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.

Copy
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.

Copy
// 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.

Copy
node ec2_deletekeypair.js

This sample code can be found here on GitHub.