/
create_instance.cpp
129 lines (112 loc) · 4.49 KB
/
create_instance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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.
*
**/
// snippet-start:[ec2.cpp.create_instance.inc]
#include <aws/core/Aws.h>
#include <aws/ec2/EC2Client.h>
#include <aws/ec2/model/CreateTagsRequest.h>
#include <aws/ec2/model/RunInstancesRequest.h>
#include <aws/ec2/model/RunInstancesResponse.h>
#include <iostream>
// snippet-end:[ec2.cpp.create_instance.inc]
#include "ec2_samples.h"
//! Launch an Amazon Elastic Compute Cloud (Amazon EC2) instance.
/*!
\sa RunInstance()
\param instanceName: A name for the EC2 instance.
\param amiId: An Amazon Machine Image (AMI) identifier.
\param instanceID: String to return the instance ID.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
bool AwsDoc::EC2::RunInstance(const Aws::String &instanceName,
const Aws::String &amiId,
Aws::String &instanceID,
const Aws::Client::ClientConfiguration &clientConfiguration) {
// snippet-start:[ec2.cpp.create_instance.code]
// snippet-start:[cpp.example_code.ec2.create_instance.client]
Aws::EC2::EC2Client ec2Client(clientConfiguration);
// snippet-end:[cpp.example_code.ec2.create_instance.client]
// snippet-start:[cpp.example_code.ec2.RunInstances]
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;
}
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;
}
// snippet-end:[ec2.cpp.create_instance.code]
instanceID = instances[0].GetInstanceId();
// snippet-end:[cpp.example_code.ec2.RunInstances]
// snippet-start:[cpp.example_code.ec2.CreateTags]
Aws::EC2::Model::Tag nameTag;
nameTag.SetKey("Name");
nameTag.SetValue(instanceName);
Aws::EC2::Model::CreateTagsRequest createRequest;
createRequest.AddResources(instanceID);
createRequest.AddTags(nameTag);
Aws::EC2::Model::CreateTagsOutcome createOutcome = ec2Client.CreateTags(
createRequest);
if (!createOutcome.IsSuccess()) {
std::cerr << "Failed to tag ec2 instance " << instanceID <<
" with name " << instanceName << ":" <<
createOutcome.GetError().GetMessage() << std::endl;
return false;
}
// snippet-end:[cpp.example_code.ec2.CreateTags]
std::cout << "Successfully launched ec2 instance " << instanceName <<
" based on ami " << amiId << std::endl;
return true;
}
/*
*
* main function
*
* Usage: 'run_create_instance <instance_name> <ami_image_id>'
*
*/
#ifndef TESTING_BUILD
int main(int argc, char **argv) {
if (argc != 3) {
std::cout << "Usage: run_create_instance <instance_name> <ami_image_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 instanceName = argv[1];
Aws::String amiId = argv[2];
Aws::String instanceID;
AwsDoc::EC2::RunInstance(instanceName, amiId, instanceID, clientConfig);
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD