2023-03-15 16:14:29 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2024-01-16 10:41:11 -05:00
2023-03-15 16:14:29 -04:00
/**
* 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.start_instance.inc]
2024-08-02 10:24:50 -04:00
2021-11-15 13:15:43 +00:00
# include <aws/ec2/EC2Client.h>
# include <aws/ec2/model/StartInstancesRequest.h>
// snippet-end:[ec2.cpp.start_instance.inc]
// snippet-start:[ec2.cpp.stop_instance.inc]
# include <aws/ec2/model/StopInstancesRequest.h>
// snippet-end:[ec2.cpp.stop_instance.inc]
# include <iostream>
2023-03-15 16:14:29 -04:00
# include "ec2_samples.h"
2024-08-02 10:24:50 -04:00
// snippet-start:[cpp.example_code.ec2.StartInstances]
2023-03-15 16:14:29 -04:00
//! Start an Amazon Elastic Compute Cloud (Amazon EC2) instance.
/*!
\param instanceID: An EC2 instance ID.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
2024-08-02 10:24:50 -04:00
bool AwsDoc : : EC2 : : startInstance ( const Aws : : String & instanceId ,
2023-03-15 16:14:29 -04:00
const Aws : : Client : : ClientConfiguration & clientConfiguration ) {
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.start_instance.code]
2023-03-15 16:14:29 -04:00
Aws : : EC2 : : EC2Client ec2Client ( clientConfiguration ) ;
2021-11-15 13:15:43 +00:00
2024-08-02 10:24:50 -04:00
Aws : : EC2 : : Model : : StartInstancesRequest startRequest ;
startRequest . AddInstanceIds ( instanceId ) ;
startRequest . SetDryRun ( true ) ;
2021-11-15 13:15:43 +00:00
2024-08-02 10:24:50 -04:00
Aws : : EC2 : : Model : : StartInstancesOutcome dryRunOutcome = ec2Client . StartInstances ( startRequest ) ;
if ( dryRunOutcome . IsSuccess ( ) ) {
2023-03-15 16:14:29 -04:00
std : : cerr
< < " Failed dry run to start instance. A dry run should trigger an error. "
< < std : : endl ;
return false ;
2024-08-02 10:24:50 -04:00
} else if ( dryRunOutcome . GetError ( ) . GetErrorType ( ) ! =
Aws : : EC2 : : EC2Errors : : DRY_RUN_OPERATION ) {
2023-03-15 16:14:29 -04:00
std : : cout < < " Failed dry run to start instance " < < instanceId < < " : "
2024-08-02 10:24:50 -04:00
< < dryRunOutcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
2023-03-15 16:14:29 -04:00
return false ;
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
startRequest . SetDryRun ( false ) ;
Aws : : EC2 : : Model : : StartInstancesOutcome startInstancesOutcome = ec2Client . StartInstances ( startRequest ) ;
2021-11-15 13:15:43 +00:00
2024-08-02 10:24:50 -04:00
if ( ! startInstancesOutcome . IsSuccess ( ) ) {
2023-03-15 16:14:29 -04:00
std : : cout < < " Failed to start instance " < < instanceId < < " : " < <
2024-08-02 10:24:50 -04:00
startInstancesOutcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
} else {
2023-03-15 16:14:29 -04:00
std : : cout < < " Successfully started instance " < < instanceId < <
std : : endl ;
2021-11-15 13:15:43 +00:00
}
// snippet-end:[ec2.cpp.start_instance.code]
2023-03-15 16:14:29 -04:00
2024-08-02 10:24:50 -04:00
return startInstancesOutcome . IsSuccess ( ) ;
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
// snippet-end:[cpp.example_code.ec2.StartInstances]
2021-11-15 13:15:43 +00:00
2024-08-02 10:24:50 -04:00
// snippet-start:[cpp.example_code.ec2.StopInstances]
2023-03-15 16:14:29 -04:00
//! Stop an EC2 instance.
/*!
\param instanceID: An EC2 instance ID.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
2024-08-02 10:24:50 -04:00
bool AwsDoc : : EC2 : : stopInstance ( const Aws : : String & instanceId ,
2023-03-15 16:14:29 -04:00
const Aws : : Client : : ClientConfiguration & clientConfiguration ) {
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.stop_instance.code]
2023-03-15 16:14:29 -04:00
Aws : : EC2 : : EC2Client ec2Client ( clientConfiguration ) ;
2021-11-15 13:15:43 +00:00
Aws : : EC2 : : Model : : StopInstancesRequest request ;
2023-03-15 16:14:29 -04:00
request . AddInstanceIds ( instanceId ) ;
2021-11-15 13:15:43 +00:00
request . SetDryRun ( true ) ;
2024-08-02 10:24:50 -04:00
Aws : : EC2 : : Model : : StopInstancesOutcome dryRunOutcome = ec2Client . StopInstances ( request ) ;
if ( dryRunOutcome . IsSuccess ( ) ) {
2023-03-15 16:14:29 -04:00
std : : cerr
< < " Failed dry run to stop instance. A dry run should trigger an error. "
< < std : : endl ;
return false ;
2024-08-02 10:24:50 -04:00
} else if ( dryRunOutcome . GetError ( ) . GetErrorType ( ) ! =
Aws : : EC2 : : EC2Errors : : DRY_RUN_OPERATION ) {
2023-03-15 16:14:29 -04:00
std : : cout < < " Failed dry run to stop instance " < < instanceId < < " : "
2024-08-02 10:24:50 -04:00
< < dryRunOutcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
2023-03-15 16:14:29 -04:00
return false ;
2021-11-15 13:15:43 +00:00
}
request . SetDryRun ( false ) ;
2024-08-02 10:24:50 -04:00
Aws : : EC2 : : Model : : StopInstancesOutcome outcome = ec2Client . StopInstances ( request ) ;
2023-03-15 16:14:29 -04:00
if ( ! outcome . IsSuccess ( ) ) {
std : : cout < < " Failed to stop instance " < < instanceId < < " : " < <
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 stopped instance " < < instanceId < <
std : : endl ;
2021-11-15 13:15:43 +00:00
}
// snippet-end:[ec2.cpp.stop_instance.code]
2023-03-15 16:14:29 -04:00
return outcome . IsSuccess ( ) ;
2021-11-15 13:15:43 +00:00
}
2023-03-15 16:14:29 -04:00
void PrintUsage ( ) {
std : : cout < < " Usage: run_start_stop_instance <instance_id> <start|stop> " < <
std : : endl ;
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
// snippet-end:[cpp.example_code.ec2.StopInstances]
2021-11-15 13:15:43 +00:00
2023-03-15 16:14:29 -04:00
/*
*
* main function
*
* Usage: 'sage: run_start_stop_instance <instance_id> <start|stop>"'
*
* Prerequisites: An EC2 instance to start or stop.
*
*/
# ifndef TESTING_BUILD
int main ( int argc , char * * argv ) {
if ( argc ! = 3 ) {
2021-11-15 13:15:43 +00:00
PrintUsage ( ) ;
return 1 ;
}
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
{
Aws : : String instance_id = argv [ 1 ] ;
bool start_instance ;
2023-03-15 16:14:29 -04:00
if ( Aws : : Utils : : StringUtils : : CaselessCompare ( argv [ 2 ] , " start " ) ) {
2021-11-15 13:15:43 +00:00
start_instance = true ;
}
2023-03-15 16:14:29 -04:00
else if ( Aws : : Utils : : StringUtils : : CaselessCompare ( argv [ 2 ] , " stop " ) ) {
2021-11-15 13:15:43 +00:00
start_instance = false ;
}
2023-03-15 16:14:29 -04:00
else {
2021-11-15 13:15:43 +00:00
PrintUsage ( ) ;
return 1 ;
}
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";
if ( start_instance ) {
2024-08-02 10:24:50 -04:00
AwsDoc : : EC2 : : startInstance ( instance_id , clientConfig ) ;
2021-11-15 13:15:43 +00:00
}
2023-03-15 16:14:29 -04:00
else {
2024-08-02 10:24:50 -04:00
AwsDoc : : EC2 : : stopInstance ( instance_id , 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