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.
*
**/
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.create_security_group.inc]
# include <aws/ec2/EC2Client.h>
# include <aws/ec2/model/CreateSecurityGroupRequest.h>
// snippet-end:[ec2.cpp.create_security_group.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.CreateSecurityGroup]
2023-03-15 16:14:29 -04:00
//! Create a security group.
/*!
\param groupName: A security group name.
\param description: A description.
\param vpcID: A virtual private cloud (VPC) ID.
2024-08-02 10:24:50 -04:00
\param[out] groupIDResult: A string to receive the group ID.
2023-03-15 16:14:29 -04:00
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
2024-08-02 10:24:50 -04:00
bool AwsDoc : : EC2 : : createSecurityGroup ( const Aws : : String & groupName ,
2023-03-15 16:14:29 -04:00
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 ) ;
2024-08-02 10:24:50 -04:00
2023-03-15 16:14:29 -04:00
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]
2024-08-02 10:24:50 -04:00
groupIDResult = outcome . GetResult ( ) . GetGroupId ( ) ;
2023-03-15 16:14:29 -04:00
return true ;
}
2024-08-02 10:24:50 -04:00
// snippet-end:[cpp.example_code.ec2.CreateSecurityGroup]
2021-11-15 13:15:43 +00:00
2023-03-15 16:14:29 -04:00
/*
*
* main function
*
* Usage: run_create_security_group <group_name> <group_description> <vpc_id>
*
* Prerequisites: A VPC ID.
*
*/
2021-11-15 13:15:43 +00:00
2023-03-15 16:14:29 -04:00
# ifndef TESTING_BUILD
2021-11-15 13:15:43 +00:00
2023-03-15 16:14:29 -04:00
int main ( int argc , char * * argv ) {
if ( argc ! = 4 ) {
std : : cout < < " Usage: run_create_security_group <group_name> " < <
" <group_description> <vpc_id> " < < std : : endl ;
2021-11-15 13:15:43 +00:00
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 ] ;
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";
Aws : : String groupIDResult ;
2024-08-02 10:24:50 -04:00
AwsDoc : : EC2 : : createSecurityGroup ( group_name , group_desc , vpc_id ,
2023-03-15 16:14:29 -04:00
groupIDResult , 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