2023-03-15 16:14:29 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.create_key_pair.inc]
# include <aws/ec2/EC2Client.h>
# include <aws/ec2/model/CreateKeyPairRequest.h>
# include <iostream>
2024-08-02 10:24:50 -04:00
# include <fstream>
2021-11-15 13:15:43 +00:00
// snippet-end:[ec2.cpp.create_key_pair.inc]
2023-03-15 16:14:29 -04:00
# include "ec2_samples.h"
2021-11-15 13:15:43 +00:00
2024-08-02 10:24:50 -04:00
// snippet-start:[cpp.example_code.ec2,CreateKeyPair]
2023-03-15 16:14:29 -04:00
//! Create an Amazon Elastic Compute Cloud (Amazon EC2) instance key pair.
/*!
\param keyPairName: A name for a key pair.
2024-08-02 10:24:50 -04:00
\param keyFilePath: File path where the credentials are stored. Ignored if it is an empty string;
2023-03-15 16:14:29 -04:00
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
2021-11-15 13:15:43 +00:00
*/
2024-08-02 10:24:50 -04:00
bool AwsDoc : : EC2 : : createKeyPair ( const Aws : : String & keyPairName , const Aws : : String & keyFilePath ,
2023-03-15 16:14:29 -04:00
const Aws : : Client : : ClientConfiguration & clientConfiguration ) {
// snippet-start:[ec2.cpp.create_key_pair.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 ( ) ) {
2024-08-02 10:24:50 -04:00
std : : cerr < < " Failed to create key pair - " < < keyPairName < < " . " < <
2023-03-15 16:14:29 -04:00
outcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
2024-08-02 10:24:50 -04:00
} else {
2023-03-15 16:14:29 -04:00
std : : cout < < " Successfully created key pair named " < <
keyPairName < < std : : endl ;
2024-08-02 10:24:50 -04:00
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 ;
}
2023-03-15 16:14:29 -04:00
}
// snippet-end:[ec2.cpp.create_key_pair.code]
return outcome . IsSuccess ( ) ;
}
2024-08-02 10:24:50 -04:00
// snippet-end:[cpp.example_code.ec2,CreateKeyPair]
2023-03-15 16:14:29 -04:00
/*
* main function
*
2024-08-02 10:24:50 -04:00
* Usage: 'run_create_key_pair <key_pair_name> [key_file_path]'
2023-03-15 16:14:29 -04:00
*
*/
# ifndef TESTING_BUILD
int main ( int argc , char * * argv ) {
2024-08-02 10:24:50 -04:00
if ( argc < 2 ) {
std : : cout < < " Usage: 'run_create_key_pair <key_pair_name> [key_file_path] "
2023-03-15 16:14:29 -04:00
< < std : : endl ;
2021-11-15 13:15:43 +00:00
return 1 ;
}
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
{
2023-03-15 16:14:29 -04:00
Aws : : Client : : ClientConfiguration clientConfig ;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
Aws : : String keyPairName = argv [ 1 ] ;
2024-08-02 10:24:50 -04:00
Aws : : String keyFilePath ;
if ( argc > 2 ) {
keyFilePath = argv [ 2 ] ;
}
AwsDoc : : EC2 : : createKeyPair ( keyPairName , keyFilePath , clientConfig ) ;
2021-11-15 13:15:43 +00:00
}
Aws : : ShutdownAPI ( options ) ;
return 0 ;
}
2023-03-15 16:14:29 -04:00
# endif // TESTING_BUILD