Working with Amazon EC2 Key Pairs - AWS SDK for C++

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 that contains the key’s name.

Includes

#include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/CreateKeyPairRequest.h> #include <aws/ec2/model/CreateKeyPairResponse.h> #include <iostream>

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:" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully created key pair named " << keyPairName << 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 DescribeKeyPairsResponse that you can use to access the list of key pairs by calling its GetKeyPairs function, which returns a list of KeyPairInfo objects.

Includes

#include <aws/core/Aws.h> #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; auto 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 that contains the name of the key pair to delete.

Includes

#include <aws/core/Aws.h> #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