// 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_security_group.inc] #include #include // snippet-end:[ec2.cpp.create_security_group.inc] #include #include "ec2_samples.h" // snippet-start:[cpp.example_code.ec2.CreateSecurityGroup] //! Create a security group. /*! \param groupName: A security group name. \param description: A description. \param vpcID: A virtual private cloud (VPC) ID. \param[out] groupIDResult: A string to receive the group ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::createSecurityGroup(const Aws::String &groupName, const Aws::String &description, const Aws::String &vpcID, Aws::String &groupIDResult, const Aws::Client::ClientConfiguration &clientConfiguration) { // snippet-start:[ec2.cpp.create_security_group.code] Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::CreateSecurityGroupRequest request; request.SetGroupName(groupName); request.SetDescription(description); request.SetVpcId(vpcID); const Aws::EC2::Model::CreateSecurityGroupOutcome outcome = ec2Client.CreateSecurityGroup(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to create security group:" << outcome.GetError().GetMessage() << std::endl; return false; } std::cout << "Successfully created security group named " << groupName << std::endl; // snippet-end:[ec2.cpp.create_security_group.code] groupIDResult = outcome.GetResult().GetGroupId(); return true; } // snippet-end:[cpp.example_code.ec2.CreateSecurityGroup] /* * * main function * * Usage: run_create_security_group * * Prerequisites: A VPC ID. * */ #ifndef TESTING_BUILD int main(int argc, char **argv) { if (argc != 4) { std::cout << "Usage: run_create_security_group " << " " << std::endl; return 1; } Aws::SDKOptions options; Aws::InitAPI(options); { Aws::String group_name = argv[1]; Aws::String group_desc = argv[2]; Aws::String vpc_id = argv[3]; Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::String groupIDResult; AwsDoc::EC2::createSecurityGroup(group_name, group_desc, vpc_id, groupIDResult, clientConfig); } Aws::ShutdownAPI(options); return 0; } #endif // TESTING_BUILD