Working with Key Pairs in Amazon EC2
The following examples show you how to use the AWS SDK for Ruby with Amazon EC2 to:
-
Create a key pair.
-
Get information about key pairs.
-
Delete a key pair.
For more information about 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.
For additional code that you can use to run these examples, see Complete Example.
Create a Key Pair
Call the create_key_pair method, specifying the name of the key pair to create.
key_pair = ec2.create_key_pair({ key_name: key_pair_name })
In this code:
-
ec2
is a variable representing an Aws::EC2::Client object. -
key_pair_name
is a string variable representing the name of the key pair. -
key_pair
is a variable representing an Aws::EC2::KeyPair object that is returned by calling thecreate_key_pair
method.
For more information, see Complete Example.
Get Information about Key Pairs
To get information about a single key pair, use attributes such as:
-
key_name, which gets the key pair's name.
-
key_fingerprint, which gets the SHA-1 digest of the DER encoded private key.
-
key_material, which gets the unencrypted PEM encoded RSA private key.
puts "Created key pair '#{key_pair.key_name}'." puts "\nSHA-1 digest of the DER encoded private key:" puts "#{key_pair.key_fingerprint}" puts "\nUnencrypted PEM encoded RSA private key:" puts "#{key_pair.key_material}"
In this code, key_pair
is a variable representing an Aws::EC2::KeyPair
object. This is
returned by calling the create_key_pair method in the previous example.
To get information about multiple key pairs, call the describe_key_pairs method.
key_pairs_result = ec2.describe_key_pairs() if key_pairs_result.key_pairs.count > 0 puts "\nKey pair names:" key_pairs_result.key_pairs.each do |key_pair| puts key_pair.key_name end end
In this code:
-
ec2
is a variable representing an Aws::EC2::Client object. -
key_pair_result
is a variable representing an Aws::EC2::Types::DescribeKeyPairsResult object that is returned by calling thedescribe_key_pairs
method. -
Calling the
Aws::EC2::Types::DescribeKeyPairsResult
object's key_pairs method returns an array of Aws::EC2::Types::KeyPairInfo objects, which represent the key pairs.
For more information, see Complete Example.
Delete a Key Pair
Call the delete_key_pair method, specifying the name of the key pair to delete.
ec2.delete_key_pair({ key_name: key_pair_name })
In this code:
-
ec2
is a variable representing an Aws::EC2::Client object. -
key_pair_name
is a string variable representing the name of the key pair.
For more information, see Complete Example.
Complete Example
The following code, which you can adapt and run, combines the preceding examples into a single example.
require 'aws-sdk-ec2' # v2: require 'aws-sdk' ec2 = Aws::EC2::Client.new(region: 'us-east-1') key_pair_name = "my-key-pair" # Create a key pair. begin key_pair = ec2.create_key_pair({ key_name: key_pair_name }) puts "Created key pair '#{key_pair.key_name}'." puts "\nSHA-1 digest of the DER encoded private key:" puts "#{key_pair.key_fingerprint}" puts "\nUnencrypted PEM encoded RSA private key:" puts "#{key_pair.key_material}" rescue Aws::EC2::Errors::InvalidKeyPairDuplicate puts "A key pair named '#{key_pair_name}' already exists." end # Get information about Amazon EC2 key pairs. key_pairs_result = ec2.describe_key_pairs() if key_pairs_result.key_pairs.count > 0 puts "\nKey pair names:" key_pairs_result.key_pairs.each do |key_pair| puts key_pair.key_name end end # Delete the key pair. ec2.delete_key_pair({ key_name: key_pair_name })
To run this code, you must:
-
Install the AWS SDK for Ruby. For more information, see Installing the AWS SDK for Ruby.
-
Set the AWS access credentials that the AWS SDK for Ruby will use to verify your access to AWS services and resources. For more information, see Configuring the AWS SDK for Ruby. Be sure the AWS credentials map to an AWS Identity and Access Management (IAM) entity with access to the AWS actions and resources described in this example. This example assumes you have set the credentials in the AWS credentials profile file or in the
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
environment variables on your local system.