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.
*
**/
# include <aws/ec2/EC2Client.h>
# include <aws/ec2/model/TerminateInstancesRequest.h>
# include <iostream>
# include "ec2_samples.h"
2024-08-02 10:24:50 -04:00
// snippet-start:[cpp.example_code.ec2.TerminateInstances]
2023-03-15 16:14:29 -04:00
//! Terminate 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 : : terminateInstances ( const Aws : : String & instanceID ,
2023-03-15 16:14:29 -04:00
const Aws : : Client : : ClientConfiguration & clientConfiguration ) {
Aws : : EC2 : : EC2Client ec2Client ( clientConfiguration ) ;
Aws : : EC2 : : Model : : TerminateInstancesRequest request ;
request . SetInstanceIds ( { instanceID } ) ;
Aws : : EC2 : : Model : : TerminateInstancesOutcome outcome =
ec2Client . TerminateInstances ( request ) ;
if ( outcome . IsSuccess ( ) ) {
std : : cout < < " Ec2 instance ' " < < instanceID < <
" ' was terminated. " < < std : : endl ;
2024-08-02 10:24:50 -04:00
} else {
2023-03-15 16:14:29 -04:00
std : : cerr < < " Failed to terminate ec2 instance " < < instanceID < <
" , " < <
outcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
return false ;
}
return outcome . IsSuccess ( ) ;
}
2024-08-02 10:24:50 -04:00
// snippet-end:[cpp.example_code.ec2.TerminateInstances]
2023-03-15 16:14:29 -04:00
/*
*
* main function
*
* Usage: 'run_terminate_instances <instance_name>'
*
* Prerequisites: An EC2 instance to terminate.
*
*/
# ifndef TESTING_BUILD
int main ( int argc , char * * argv ) {
if ( argc ! = 2 ) {
std : : cout < < " Usage: run_terminate_instances <instance_id> "
< < std : : endl ;
return 1 ;
}
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
{
Aws : : Client : : ClientConfiguration clientConfig ;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
Aws : : String instanceID = argv [ 1 ] ;
2024-08-02 10:24:50 -04:00
AwsDoc : : EC2 : : terminateInstances ( instanceID , clientConfig ) ;
2023-03-15 16:14:29 -04:00
}
Aws : : ShutdownAPI ( options ) ;
return 0 ;
}
# endif // TESTING_BUILD