翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Amazon EC2 のキーペアでの作業
前提条件
作業を始める前に「AWS SDK for C++ の開始方法」を読むことをお勧めします。
コード例をダウンロードし、「コード例の開始方法」の説明に従ってソリューションをビルドします。
例を実行するには、リクエストに使用するユーザープロファイルに、AWS のサービスとアクションに対する適切なアクセス許可が付与されている必要があります。詳細については、「AWS 認証情報の提供」を参照してください。
キーペアの作成
キーペアを作成するには、EC2Client の CreateKeyPair 関数の呼び出しで、キーの名前を CreateKeyPairRequest で指定して渡します。
含まれるもの:
#include <aws/ec2/EC2Client.h> #include <aws/ec2/model/CreateKeyPairRequest.h> #include <iostream> #include <fstream>
コード
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; } }
完全な例
キーペアの記述
キーペアを一覧表示したり、それらに関する情報を取得したりするには、EC2Client の DescribeKeyPairs 関数の呼び出しで、DescribeKeyPairsRequest を渡します。
DescribeKeyPairsResponse が返されます。その GetKeyPairs 関数を呼び出して、キーペアの一覧にアクセスできます。この関数は、KeyPairInfo オブジェクトのリストを返します。
含まれるもの:
#include <aws/ec2/EC2Client.h> #include <aws/ec2/model/DescribeKeyPairsRequest.h> #include <aws/ec2/model/DescribeKeyPairsResponse.h> #include <iomanip> #include <iostream>
コード
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; }
完全な例
キーペアの削除
キーペアを削除するには、EC2Client の DeleteKeyPair 関数の呼び出しで、削除するキーペアの名前を DeleteKeyPairRequest で指定して渡します。
含まれるもの:
#include <aws/ec2/EC2Client.h> #include <aws/ec2/model/DeleteKeyPairRequest.h> #include <iostream>
コード
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; }
完全な例
詳細情報
-
「Amazon EC2 ユーザーガイド」の「Amazon EC2 キーペア」
-
「Amazon EC2 API リファレンス」の「CreateKeyPair」
-
「Amazon EC2 API リファレンス」の「DescribeKeyPairs」
-
「Amazon EC2 API リファレンス」の「DeleteKeyPair」