AWS SDK for C++
Developer Guide

Working with Amazon EC2 Key Pairs

Note

These code snippets assume that you understand the material in Getting Started Using the AWS SDK for C++ and have configured default AWS credentials using the information in Providing AWS Credentials.

Creating a Key Pair

To create a key pair, call the EC2Client's CreateKeyPair function with a CreateKeyPairRequest that contains the key's name.

Includes

CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h>

Code

Aws::String pair_name = argv[1]; Aws::EC2::EC2Client ec2; Aws::EC2::Model::CreateKeyPairRequest request; request.SetKeyName(pair_name); auto outcome = ec2.CreateKeyPair(request); if (!outcome.IsSuccess()) { std::cout << "Failed to create key pair:" << outcome.GetError().GetMessage() << std::endl; } else {

See the complete example.

Describing 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

CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/DescribeKeyPairsRequest.h>

Code

std::cout << std::left << std::setw(32) << "Name" << std::setw(64) << "Fingerprint" << std::endl; const auto &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

See the complete example.

Deleting 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

CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h>

Code

Aws::String pair_name = argv[1]; Aws::EC2::EC2Client ec2; Aws::EC2::Model::DeleteKeyPairRequest request; request.SetKeyName(pair_name); auto outcome = ec2.DeleteKeyPair(request); if (!outcome.IsSuccess()) { std::cout << "Failed to delete key pair " << pair_name << ":" << outcome.GetError().GetMessage() << std::endl; } else {

See the complete example.

More Information