文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
CreateKeyPair
搭配 a AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 CreateKeyPair
。
動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:
- .NET
-
- AWS SDK for .NET
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /// <summary> /// Create an Amazon EC2 key pair with a specified name. /// </summary> /// <param name="keyPairName">The name for the new key pair.</param> /// <returns>The Amazon EC2 key pair created.</returns> public async Task<KeyPair?> CreateKeyPair(string keyPairName) { try { var request = new CreateKeyPairRequest { KeyName = keyPairName, }; var response = await _amazonEC2.CreateKeyPairAsync(request); var kp = response.KeyPair; // Return the key pair so it can be saved if needed. // Wait until the key pair exists. int retries = 5; while (retries-- > 0) { Console.WriteLine($"Checking for new KeyPair {keyPairName}..."); var keyPairs = await DescribeKeyPairs(keyPairName); if (keyPairs.Any()) { return kp; } Thread.Sleep(5000); retries--; } _logger.LogError($"Unable to find newly created KeyPair {keyPairName}."); throw new DoesNotExistException("KeyPair not found"); } catch (AmazonEC2Exception ec2Exception) { if (ec2Exception.ErrorCode == "InvalidKeyPair.Duplicate") { _logger.LogError( $"A key pair called {keyPairName} already exists."); } throw; } catch (Exception ex) { _logger.LogError( $"An error occurred while creating the key pair.: {ex.Message}"); throw; } } /// <summary> /// Save KeyPair information to a temporary file. /// </summary> /// <param name="keyPair">The name of the key pair.</param> /// <returns>The full path to the temporary file.</returns> public string SaveKeyPair(KeyPair keyPair) { var tempPath = Path.GetTempPath(); var tempFileName = $"{tempPath}\\{Path.GetRandomFileName()}"; var pemFileName = Path.ChangeExtension(tempFileName, "pem"); // Save the key pair to a file in a temporary folder. using var stream = new FileStream(pemFileName, FileMode.Create); using var writer = new StreamWriter(stream); writer.WriteLine(keyPair.KeyMaterial); return pemFileName; }
-
如需 API 詳細資訊,請參閱 CreateKeyPair AWS SDK for .NET 參考中的 API。
-
- Bash
-
- AWS CLI 使用 Bash 指令碼
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 ############################################################################### # function ec2_create_keypair # # This function creates an Amazon Elastic Compute Cloud (Amazon EC2) ED25519 or 2048-bit RSA key pair # and writes it to a file. # # Parameters: # -n key_pair_name - A key pair name. # -f file_path - File to store the key pair. # # And: # 0 - If successful. # 1 - If it fails. ############################################################################### function ec2_create_keypair() { local key_pair_name file_path response local option OPTARG # Required to use getopts command in a function. # bashsupport disable=BP5008 function usage() { echo "function ec2_create_keypair" echo "Creates an Amazon Elastic Compute Cloud (Amazon EC2) ED25519 or 2048-bit RSA key pair" echo " and writes it to a file." echo " -n key_pair_name - A key pair name." echo " -f file_path - File to store the key pair." echo "" } # Retrieve the calling parameters. while getopts "n:f:h" option; do case "${option}" in n) key_pair_name="${OPTARG}" ;; f) file_path="${OPTARG}" ;; h) usage return 0 ;; \?) echo "Invalid parameter" usage return 1 ;; esac done export OPTIND=1 if [[ -z "$key_pair_name" ]]; then errecho "ERROR: You must provide a key name with the -n parameter." usage return 1 fi if [[ -z "$file_path" ]]; then errecho "ERROR: You must provide a file path with the -f parameter." usage return 1 fi response=$(aws ec2 create-key-pair \ --key-name "$key_pair_name" \ --query 'KeyMaterial' \ --output text) || { aws_cli_error_log ${?} errecho "ERROR: AWS reports create-access-key operation failed.$response" return 1 } if [[ -n "$file_path" ]]; then echo "$response" >"$file_path" fi return 0 }
此範例中使用的公用程式函數。
############################################################################### # function errecho # # This function outputs everything sent to it to STDERR (standard error output). ############################################################################### function errecho() { printf "%s\n" "$*" 1>&2 } ############################################################################## # function aws_cli_error_log() # # This function is used to log the error messages from the AWS CLI. # # The function expects the following argument: # $1 - The error code returned by the AWS CLI. # # Returns: # 0: - Success. # ############################################################################## function aws_cli_error_log() { local err_code=$1 errecho "Error code : $err_code" if [ "$err_code" == 1 ]; then errecho " One or more S3 transfers failed." elif [ "$err_code" == 2 ]; then errecho " Command line failed to parse." elif [ "$err_code" == 130 ]; then errecho " Process received SIGINT." elif [ "$err_code" == 252 ]; then errecho " Command syntax invalid." elif [ "$err_code" == 253 ]; then errecho " The system environment or configuration was invalid." elif [ "$err_code" == 254 ]; then errecho " The service returned an error." elif [ "$err_code" == 255 ]; then errecho " 255 is a catch-all error." fi return 0 }
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateKeyPair。
-
- C++
-
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 //! Create an Amazon Elastic Compute Cloud (Amazon EC2) instance key pair. /*! \param keyPairName: A name for a key pair. \param keyFilePath: File path where the credentials are stored. Ignored if it is an empty string; \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::createKeyPair(const Aws::String &keyPairName, const Aws::String &keyFilePath, const Aws::Client::ClientConfiguration &clientConfiguration) { 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; } } return outcome.IsSuccess(); }
-
如需 API 詳細資訊,請參閱 CreateKeyPair AWS SDK for C++ 參考中的 API。
-
- CLI
-
- AWS CLI
-
建立一組金鑰對
此範例會建立名稱為
MyKeyPair
的金鑰對。命令:
aws ec2 create-key-pair --key-name
MyKeyPair
輸出是私有金鑰和金鑰指紋的 ASCII 版本。您需要將金鑰儲存到檔案。
如需詳細資訊,請參閱《AWS 命令行介面使用者指南》中的「使用金鑰對」。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateKeyPair
。
-
- Java
-
- Java 2.x 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /** * Creates a new key pair asynchronously. * * @param keyName the name of the key pair to create * @param fileName the name of the file to write the key material to * @return a {@link CompletableFuture} that represents the asynchronous operation * of creating the key pair and writing the key material to a file */ public CompletableFuture<CreateKeyPairResponse> createKeyPairAsync(String keyName, String fileName) { CreateKeyPairRequest request = CreateKeyPairRequest.builder() .keyName(keyName) .build(); CompletableFuture<CreateKeyPairResponse> responseFuture = getAsyncClient().createKeyPair(request); responseFuture.whenComplete((response, exception) -> { if (response != null) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); writer.write(response.keyMaterial()); writer.close(); } catch (IOException e) { throw new RuntimeException("Failed to write key material to file: " + e.getMessage(), e); } } else { throw new RuntimeException("Failed to create key pair: " + exception.getMessage(), exception); } }); return responseFuture; }
-
如需 API 詳細資訊,請參閱 CreateKeyPair AWS SDK for Java 2.x 參考中的 API。
-
- JavaScript
-
- SDK for JavaScript (v3)
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import { CreateKeyPairCommand, EC2Client } from "@aws-sdk/client-ec2"; /** * Creates an ED25519 or 2048-bit RSA key pair with the specified name and in the specified PEM or PPK format. * Amazon EC2 stores the public key and displays the private key for you to save to a file. * @param {{ keyName: string }} options */ export const main = async ({ keyName }) => { const client = new EC2Client({}); const command = new CreateKeyPairCommand({ KeyName: keyName, }); try { const { KeyMaterial, KeyName } = await client.send(command); console.log(KeyName); console.log(KeyMaterial); } catch (caught) { if (caught instanceof Error && caught.name === "InvalidKeyPair.Duplicate") { console.warn(`${caught.message}. Try another key name.`); } else { throw caught; } } };
-
如需 API 詳細資訊,請參閱 CreateKeyPair AWS SDK for JavaScript 參考中的 API。
-
- Kotlin
-
- SDK for Kotlin
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 suspend fun createEC2KeyPair(keyNameVal: String) { val request = CreateKeyPairRequest { keyName = keyNameVal } Ec2Client { region = "us-west-2" }.use { ec2 -> val response = ec2.createKeyPair(request) println("The key ID is ${response.keyPairId}") } }
-
如需 API 詳細資訊,請參閱 CreateKeyPair
AWS for Kotlin SDK 參考中的 API。
-
- PowerShell
-
- for PowerShell 工具
-
範例 1:此範例會建立金鑰對,並在具有指定名稱的檔案中擷取 PEM 編碼的 RSA 私有金鑰。當您使用 PowerShell 時,編碼必須設定為 ascii,才能產生有效的金鑰。如需詳細資訊,請參閱 AWS 命令列介面使用者指南中的建立、顯示和刪除 Amazon EC2 金鑰對 (https://docs.aws.amazon.com/cli/latest/userguide/cliWord-services-ec2-keypairs.html)。
(New-EC2KeyPair -KeyName "my-key-pair").KeyMaterial | Out-File -Encoding ascii -FilePath C:\path\my-key-pair.pem
-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 CreateKeyPair。
-
- Python
-
- SDK for Python (Boto3)
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 class KeyPairWrapper: """ Encapsulates Amazon Elastic Compute Cloud (Amazon EC2) key pair actions. This class provides methods to create, list, and delete EC2 key pairs. """ def __init__( self, ec2_client: boto3.client, key_file_dir: Union[tempfile.TemporaryDirectory, str], key_pair: Optional[dict] = None, ): """ Initializes the KeyPairWrapper with the specified EC2 client, key file directory, and an optional key pair. :param ec2_client: A Boto3 Amazon EC2 client. This client provides low-level access to AWS EC2 services. :param key_file_dir: The folder where the private key information is stored. This should be a secure folder. :param key_pair: A dictionary representing the Boto3 KeyPair object. This is a high-level object that wraps key pair actions. Optional. """ self.ec2_client = ec2_client self.key_pair = key_pair self.key_file_path: Optional[str] = None self.key_file_dir = key_file_dir @classmethod def from_client(cls) -> "KeyPairWrapper": """ Class method to create an instance of KeyPairWrapper using a new EC2 client and a temporary directory for storing key files. :return: An instance of KeyPairWrapper. """ ec2_client = boto3.client("ec2") return cls(ec2_client, tempfile.TemporaryDirectory()) def create(self, key_name: str) -> dict: """ Creates a key pair that can be used to securely connect to an EC2 instance. The returned key pair contains private key information that cannot be retrieved again. The private key data is stored as a .pem file. :param key_name: The name of the key pair to create. :return: A dictionary representing the Boto3 KeyPair object that represents the newly created key pair. :raises ClientError: If there is an error in creating the key pair, for example, if a key pair with the same name already exists. """ try: response = self.ec2_client.create_key_pair(KeyName=key_name) self.key_pair = response self.key_file_path = os.path.join( self.key_file_dir.name, f"{self.key_pair['KeyName']}.pem" ) with open(self.key_file_path, "w") as key_file: key_file.write(self.key_pair["KeyMaterial"]) except ClientError as err: if err.response["Error"]["Code"] == "InvalidKeyPair.Duplicate": logger.error( f"A key pair called {key_name} already exists. " "Please choose a different name for your key pair " "or delete the existing key pair before creating." ) raise else: return self.key_pair
-
如需 API 詳細資訊,請參閱 CreateKeyPair AWS SDK for Python (Boto3) Word 參考中的 API。
-
- Ruby
-
- Ruby 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 # This code example does the following: # 1. Creates a key pair in Amazon Elastic Compute Cloud (Amazon EC2). # 2. Displays information about available key pairs. # 3. Deletes the key pair. require 'aws-sdk-ec2' # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param key_pair_name [String] The name for the key pair and private # key file. # @return [Boolean] true if the key pair and private key file were # created; otherwise, false. # @example # exit 1 unless key_pair_created?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'my-key-pair' # ) def key_pair_created?(ec2_client, key_pair_name) key_pair = ec2_client.create_key_pair(key_name: key_pair_name) puts "Created key pair '#{key_pair.key_name}' with fingerprint " \ "'#{key_pair.key_fingerprint}' and ID '#{key_pair.key_pair_id}'." filename = File.join(Dir.home, "#{key_pair_name}.pem") File.open(filename, 'w') { |file| file.write(key_pair.key_material) } puts "Private key file saved locally as '#{filename}'." true rescue Aws::EC2::Errors::InvalidKeyPairDuplicate puts "Error creating key pair: a key pair named '#{key_pair_name}' " \ 'already exists.' false rescue StandardError => e puts "Error creating key pair or saving private key file: #{e.message}" false end # Displays information about available key pairs in # Amazon Elastic Compute Cloud (Amazon EC2). # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @example # describe_key_pairs(Aws::EC2::Client.new(region: 'us-west-2')) def describe_key_pairs(ec2_client) result = ec2_client.describe_key_pairs if result.key_pairs.count.zero? puts 'No key pairs found.' else puts 'Key pair names:' result.key_pairs.each do |key_pair| puts key_pair.key_name end end rescue StandardError => e puts "Error getting information about key pairs: #{e.message}" end # Deletes a key pair in Amazon Elastic Compute Cloud (Amazon EC2). # # Prerequisites: # # - The key pair to delete. # # @param ec2_client [Aws::EC2::Client] An initialized EC2 client. # @param key_pair_name [String] The name of the key pair to delete. # @return [Boolean] true if the key pair was deleted; otherwise, false. # @example # exit 1 unless key_pair_deleted?( # Aws::EC2::Client.new(region: 'us-west-2'), # 'my-key-pair' # ) def key_pair_deleted?(ec2_client, key_pair_name) ec2_client.delete_key_pair(key_name: key_pair_name) true rescue StandardError => e puts "Error deleting key pair: #{e.message}" false end # Example usage: def run_me key_pair_name = '' region = '' # Print usage information and then stop. if ARGV[0] == '--help' || ARGV[0] == '-h' puts 'Usage: ruby ec2-ruby-example-key-pairs.rb KEY_PAIR_NAME REGION' puts 'Example: ruby ec2-ruby-example-key-pairs.rb my-key-pair us-west-2' exit 1 # If no values are specified at the command prompt, use these default values. # Replace us-west-2 with the AWS Region you're using for Amazon EC2. elsif ARGV.count.zero? key_pair_name = 'my-key-pair' region = 'us-west-2' # Otherwise, use the values as specified at the command prompt. else key_pair_name = ARGV[0] region = ARGV[1] end ec2_client = Aws::EC2::Client.new(region: region) puts 'Displaying existing key pair names before creating this key pair...' describe_key_pairs(ec2_client) puts '-' * 10 puts 'Creating key pair...' unless key_pair_created?(ec2_client, key_pair_name) puts 'Stopping program.' exit 1 end puts '-' * 10 puts 'Displaying existing key pair names after creating this key pair...' describe_key_pairs(ec2_client) puts '-' * 10 puts 'Deleting key pair...' unless key_pair_deleted?(ec2_client, key_pair_name) puts 'Stopping program. You must delete the key pair yourself.' exit 1 end puts 'Key pair deleted.' puts '-' * 10 puts 'Now that the key pair is deleted, ' \ 'also deleting the related private key pair file...' filename = File.join(Dir.home, "#{key_pair_name}.pem") File.delete(filename) if File.exist?(filename) puts "Could not delete file at '#{filename}'. You must delete it yourself." else puts 'File deleted.' end puts '-' * 10 puts 'Displaying existing key pair names after deleting this key pair...' describe_key_pairs(ec2_client) end run_me if $PROGRAM_NAME == __FILE__
-
如需 API 詳細資訊,請參閱 CreateKeyPair AWS SDK for Ruby 參考中的 API。
-
- Rust
-
- Rust 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 Rust 實作會呼叫 EC2 Client 的 create_key_pair,並擷取傳回的資料。
pub async fn create_key_pair(&self, name: String) -> Result<(KeyPairInfo, String), EC2Error> { tracing::info!("Creating key pair {name}"); let output = self.client.create_key_pair().key_name(name).send().await?; let info = KeyPairInfo::builder() .set_key_name(output.key_name) .set_key_fingerprint(output.key_fingerprint) .set_key_pair_id(output.key_pair_id) .build(); let material = output .key_material .ok_or_else(|| EC2Error::new("Create Key Pair has no key material"))?; Ok((info, material)) }
呼叫 create_key impl 並安全地儲存 PEM 私有金鑰的函數。
/// Creates a key pair that can be used to securely connect to an EC2 instance. /// The returned key pair contains private key information that cannot be retrieved /// again. The private key data is stored as a .pem file. /// /// :param key_name: The name of the key pair to create. pub async fn create( &mut self, ec2: &EC2, util: &Util, key_name: String, ) -> Result<KeyPairInfo, EC2Error> { let (key_pair, material) = ec2.create_key_pair(key_name.clone()).await.map_err(|e| { self.key_pair = KeyPairInfo::builder().key_name(key_name.clone()).build(); e.add_message(format!("Couldn't create key {key_name}")) })?; let path = self.key_file_dir.join(format!("{key_name}.pem")); // Save the key_pair information immediately, so it can get cleaned up if write_secure fails. self.key_file_path = Some(path.clone()); self.key_pair = key_pair.clone(); util.write_secure(&key_name, &path, material)?; Ok(key_pair) }
-
如需 API 詳細資訊,請參閱 CreateKeyPair
AWS for Rust SDK 中的 API 參考。
-
- SAP ABAP
-
- SDK for SAP ABAP
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 TRY. oo_result = lo_ec2->createkeypair( iv_keyname = iv_key_name ). " oo_result is returned for testing purposes. " MESSAGE 'Amazon EC2 key pair created.' TYPE 'I'. CATCH /aws1/cx_rt_service_generic INTO DATA(lo_exception). DATA(lv_error) = |"{ lo_exception->av_err_code }" - { lo_exception->av_err_msg }|. MESSAGE lv_error TYPE 'E'. ENDTRY.
-
如需 API 詳細資訊,請參閱 CreateKeyPair AWS SDK for SAP ABAP 參考中的 API。
-