SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 35 Java
// 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_instance.inc]
#include <aws/core/Aws.h>
#include <aws/ec2/EC2Client.h>
#include <aws/ec2/model/RunInstancesRequest.h>
#include <iostream>
// snippet-end:[ec2.cpp.create_instance.inc]
#include "ec2_samples.h"
2024-08-02 10:24:50 -04:00
// snippet-start:[cpp.example_code.ec2.RunInstances]
//! Launch an Amazon Elastic Compute Cloud (Amazon EC2) instance.
/*!
\param instanceName: A name for the EC2 instance.
\param amiId: An Amazon Machine Image (AMI) identifier.
2024-08-02 10:24:50 -04:00
\param[out] instanceID: String to return the instance ID.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
2024-08-02 10:24:50 -04:00
bool AwsDoc::EC2::runInstance(const Aws::String &instanceName,
const Aws::String &amiId,
Aws::String &instanceID,
const Aws::Client::ClientConfiguration &clientConfiguration) {
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.create_instance.code]
Aws::EC2::EC2Client ec2Client(clientConfiguration);
Aws::EC2::Model::RunInstancesRequest runRequest;
runRequest.SetImageId(amiId);
runRequest.SetInstanceType(Aws::EC2::Model::InstanceType::t1_micro);
runRequest.SetMinCount(1);
runRequest.SetMaxCount(1);
Aws::EC2::Model::RunInstancesOutcome runOutcome = ec2Client.RunInstances(
runRequest);
if (!runOutcome.IsSuccess()) {
std::cerr << "Failed to launch EC2 instance " << instanceName <<
" based on ami " << amiId << ":" <<
runOutcome.GetError().GetMessage() << std::endl;
return false;
2021-11-15 13:15:43 +00:00
}
const Aws::Vector<Aws::EC2::Model::Instance> &instances = runOutcome.GetResult().GetInstances();
if (instances.empty()) {
std::cerr << "Failed to launch EC2 instance " << instanceName <<
" based on ami " << amiId << ":" <<
runOutcome.GetError().GetMessage() << std::endl;
return false;
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
// snippet-end:[ec2.cpp.create_instance.code]
instanceID = instances[0].GetInstanceId();
2021-11-15 13:15:43 +00:00
return true;
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
// snippet-end:[cpp.example_code.ec2.RunInstances]
2021-11-15 13:15:43 +00:00
/*
*
* main function
*
2024-08-02 10:24:50 -04:00
* Usage: 'run_run_instances <instance_name> <ami_image_id>'
*
2021-11-15 13:15:43 +00:00
*/
#ifndef TESTING_BUILD
int main(int argc, char **argv) {
if (argc != 3) {
2024-08-02 10:24:50 -04:00
std::cout << "Usage: run_run_instances <instance_name> <ami_image_id>"
<< std::endl;
2021-11-15 13:15:43 +00:00
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";
2021-11-15 13:15:43 +00:00
Aws::String instanceName = argv[1];
Aws::String amiId = argv[2];
Aws::String instanceID;
2024-08-02 10:24:50 -04:00
AwsDoc::EC2::runInstance(instanceName, amiId, instanceID, clientConfig);
2021-11-15 13:15:43 +00:00
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD