Working with Amazon EC2 Key Pairs
Prerequisites
Before you begin, we recommend you read Getting started using the AWS SDK for C++.
Download the example code and build the solution as described in Get started on code examples.
To run the examples, the user profile your code uses to make the requests must have proper permissions in AWS (for the service and the action). For more information, see Providing AWS credentials.
Create a Key Pair
To create a key pair, call the EC2Client’s CreateKeyPair
function with a
CreateKeyPairRequest
Includes
#include <aws/ec2/EC2Client.h>
#include <aws/ec2/model/CreateKeyPairRequest.h>
#include <iostream>
#include <fstream>
Code
Aws::EC2::EC2Client ec2Client(clientConfiguration);
Aws::EC2::Model::CreateKeyPairRequest request;
request.SetKeyName(keyPairName);
Aws::EC2::Model::CreateKeyPairOutcome outcome = ec2Client.CreateKeyPair(request);
if (!outcome.IsSuccess()) {
std::cerr << "Failed to create key pair - " << keyPairName << ". " <<
outcome.GetError().GetMessage() << std::endl;
} else {
std::cout << "Successfully created key pair named " <<
keyPairName << std::endl;
if (!keyFilePath.empty()) {
std::ofstream keyFile(keyFilePath.c_str());
keyFile << outcome.GetResult().GetKeyMaterial();
keyFile.close();
std::cout << "Keys written to the file " <<
keyFilePath << std::endl;
}
}
See the complete example
Describe Key Pairs
To list your key pairs or to get information about them, call the EC2Client’s
DescribeKeyPairs
function with a DescribeKeyPairsRequest
You will receive a DescribeKeyPairsResponseGetKeyPairs
function, which returns a list of
KeyPairInfo
Includes
#include <aws/ec2/EC2Client.h>
#include <aws/ec2/model/DescribeKeyPairsRequest.h>
#include <aws/ec2/model/DescribeKeyPairsResponse.h>
#include <iomanip>
#include <iostream>
Code
Aws::EC2::EC2Client ec2Client(clientConfiguration);
Aws::EC2::Model::DescribeKeyPairsRequest request;
Aws::EC2::Model::DescribeKeyPairsOutcome outcome = ec2Client.DescribeKeyPairs(request);
if (outcome.IsSuccess()) {
std::cout << std::left <<
std::setw(32) << "Name" <<
std::setw(64) << "Fingerprint" << std::endl;
const std::vector<Aws::EC2::Model::KeyPairInfo> &key_pairs =
outcome.GetResult().GetKeyPairs();
for (const auto &key_pair: key_pairs) {
std::cout << std::left <<
std::setw(32) << key_pair.GetKeyName() <<
std::setw(64) << key_pair.GetKeyFingerprint() << std::endl;
}
} else {
std::cerr << "Failed to describe key pairs:" <<
outcome.GetError().GetMessage() << std::endl;
}
See the complete example
Delete a Key Pair
To delete a key pair, call the EC2Client’s DeleteKeyPair
function, passing it a
DeleteKeyPairRequest
Includes
#include <aws/ec2/EC2Client.h>
#include <aws/ec2/model/DeleteKeyPairRequest.h>
#include <iostream>
Code
Aws::EC2::EC2Client ec2Client(clientConfiguration);
Aws::EC2::Model::DeleteKeyPairRequest request;
request.SetKeyName(keyPairName);
const Aws::EC2::Model::DeleteKeyPairOutcome outcome = ec2Client.DeleteKeyPair(
request);
if (!outcome.IsSuccess()) {
std::cerr << "Failed to delete key pair " << keyPairName <<
":" << outcome.GetError().GetMessage() << std::endl;
} else {
std::cout << "Successfully deleted key pair named " << keyPairName <<
std::endl;
}
See the complete example
More Information
-
Amazon EC2 Key Pairs in the Amazon EC2 User Guide
-
CreateKeyPair in the Amazon EC2 API Reference
-
DescribeKeyPairs in the Amazon EC2 API Reference
-
DeleteKeyPair in the Amazon EC2 API Reference