Creating security groups - Amazon SDK for .NET
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Creating security groups

This example shows you how to use the Amazon SDK for .NET to create a security group. You can provide the ID of an existing VPC to create a security group for EC2 in a VPC. If you don't supply such an ID, the new security group will be for EC2-Classic if your Amazon account supports this.

If you don't supply a VPC ID and your Amazon account doesn't support EC2-Classic, the new security group will belong to the default VPC of your account. For more information, see the references for EC2 in a VPC versus EC2-Classic in the parent section (Working with security groups in Amazon EC2).

The following sections provide snippets of this example. The complete code for the example is shown after that, and can be built and run as is.

Find existing security groups

The following snippet searches for existing security groups with the given name in the given VPC.

The example at the end of this topic shows this snippet in use.

// // Method to determine if a security group with the specified name // already exists in the VPC private static async Task<List<SecurityGroup>> FindSecurityGroups( IAmazonEC2 ec2Client, string groupName, string vpcID) { var request = new DescribeSecurityGroupsRequest(); request.Filters.Add(new Filter{ Name = "group-name", Values = new List<string>() { groupName } }); if(!string.IsNullOrEmpty(vpcID)) request.Filters.Add(new Filter{ Name = "vpc-id", Values = new List<string>() { vpcID } }); var response = await ec2Client.DescribeSecurityGroupsAsync(request); return response.SecurityGroups; }

Create a security group

The following snippet creates a new security group if a group with that name doesn't exist in the given VPC. If no VPC is given and one or more groups with that name exist, the snippet simply returns the list of groups.

The example at the end of this topic shows this snippet in use.

// // Method to create a new security group (either EC2-Classic or EC2-VPC) // If vpcID is empty, the security group will be for EC2-Classic private static async Task<List<SecurityGroup>> CreateSecurityGroup( IAmazonEC2 ec2Client, string groupName, string vpcID) { // See if one or more security groups with that name // already exist in the given VPC. If so, return the list of them. var securityGroups = await FindSecurityGroups(ec2Client, groupName, vpcID); if (securityGroups.Count > 0) { Console.WriteLine( $"\nOne or more security groups with name {groupName} already exist.\n"); return securityGroups; } // If the security group doesn't already exists, create it. var createRequest = new CreateSecurityGroupRequest{ GroupName = groupName }; if(string.IsNullOrEmpty(vpcID)) { createRequest.Description = "My .NET example security group for EC2-Classic"; } else { createRequest.VpcId = vpcID; createRequest.Description = "My .NET example security group for EC2-VPC"; } CreateSecurityGroupResponse createResponse = await ec2Client.CreateSecurityGroupAsync(createRequest); // Return the new security group DescribeSecurityGroupsResponse describeResponse = await ec2Client.DescribeSecurityGroupsAsync(new DescribeSecurityGroupsRequest{ GroupIds = new List<string>() { createResponse.GroupId } }); return describeResponse.SecurityGroups; }

Complete code

This section shows relevant references and the complete code for this example.

using System; using System.Threading.Tasks; using System.Collections.Generic; using Amazon.EC2; using Amazon.EC2.Model; namespace EC2CreateSecGroup { // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = // Class to create a security group class Program { private const int MaxArgs = 2; static async Task Main(string[] args) { // Parse the command line and show help if necessary var parsedArgs = CommandLine.Parse(args); if(parsedArgs.Count == 0) { PrintHelp(); return; } if(parsedArgs.Count > MaxArgs) CommandLine.ErrorExit("\nThe number of command-line arguments is incorrect." + "\nRun the command with no arguments to see help."); // Get the application arguments from the parsed list var groupName = CommandLine.GetArgument(parsedArgs, null, "-g", "--group-name"); var vpcID = CommandLine.GetArgument(parsedArgs, null, "-v", "--vpc-id"); if(string.IsNullOrEmpty(groupName)) CommandLine.ErrorExit("\nYou must supply a name for the new group." + "\nRun the command with no arguments to see help."); if(!string.IsNullOrEmpty(vpcID) && !vpcID.StartsWith("vpc-")) CommandLine.ErrorExit($"\nNot a valid VPC ID: {vpcID}"); // groupName has a value and vpcID either has a value or is null (which is fine) // Create the new security group and display information about it var securityGroups = await CreateSecurityGroup(new AmazonEC2Client(), groupName, vpcID); Console.WriteLine("Information about the security group(s):"); foreach(var group in securityGroups) { Console.WriteLine($"\nGroupName: {group.GroupName}"); Console.WriteLine($"GroupId: {group.GroupId}"); Console.WriteLine($"Description: {group.Description}"); Console.WriteLine($"VpcId (if any): {group.VpcId}"); } } // // Method to create a new security group (either EC2-Classic or EC2-VPC) // If vpcID is empty, the security group will be for EC2-Classic private static async Task<List<SecurityGroup>> CreateSecurityGroup( IAmazonEC2 ec2Client, string groupName, string vpcID) { // See if one or more security groups with that name // already exist in the given VPC. If so, return the list of them. var securityGroups = await FindSecurityGroups(ec2Client, groupName, vpcID); if (securityGroups.Count > 0) { Console.WriteLine( $"\nOne or more security groups with name {groupName} already exist.\n"); return securityGroups; } // If the security group doesn't already exists, create it. var createRequest = new CreateSecurityGroupRequest{ GroupName = groupName }; if(string.IsNullOrEmpty(vpcID)) { createRequest.Description = "Security group for .NET code example (no VPC specified)"; } else { createRequest.VpcId = vpcID; createRequest.Description = "Security group for .NET code example (VPC: " + vpcID + ")"; } CreateSecurityGroupResponse createResponse = await ec2Client.CreateSecurityGroupAsync(createRequest); // Return the new security group DescribeSecurityGroupsResponse describeResponse = await ec2Client.DescribeSecurityGroupsAsync(new DescribeSecurityGroupsRequest{ GroupIds = new List<string>() { createResponse.GroupId } }); return describeResponse.SecurityGroups; } // // Method to determine if a security group with the specified name // already exists in the VPC private static async Task<List<SecurityGroup>> FindSecurityGroups( IAmazonEC2 ec2Client, string groupName, string vpcID) { var request = new DescribeSecurityGroupsRequest(); request.Filters.Add(new Filter{ Name = "group-name", Values = new List<string>() { groupName } }); if(!string.IsNullOrEmpty(vpcID)) request.Filters.Add(new Filter{ Name = "vpc-id", Values = new List<string>() { vpcID } }); var response = await ec2Client.DescribeSecurityGroupsAsync(request); return response.SecurityGroups; } // // Command-line help private static void PrintHelp() { Console.WriteLine( "\nUsage: EC2CreateSecGroup -g <group-name> [-v <vpc-id>]" + "\n -g, --group-name: The name you would like the new security group to have." + "\n -v, --vpc-id: The ID of a VPC to which the new security group will belong." + "\n If vpc-id isn't present, the security group will be" + "\n for EC2-Classic (if your AWS account supports this)" + "\n or will use the default VCP for EC2-VPC."); } } // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = // Class that represents a command line on the console or terminal. // (This is the same for all examples. When you have seen it once, you can ignore it.) static class CommandLine { // // Method to parse a command line of the form: "--key value" or "-k value". // // Parameters: // - args: The command-line arguments passed into the application by the system. // // Returns: // A Dictionary with string Keys and Values. // // If a key is found without a matching value, Dictionary.Value is set to the key // (including the dashes). // If a value is found without a matching key, Dictionary.Key is set to "--NoKeyN", // where "N" represents sequential numbers. public static Dictionary<string,string> Parse(string[] args) { var parsedArgs = new Dictionary<string,string>(); int i = 0, n = 0; while(i < args.Length) { // If the first argument in this iteration starts with a dash it's an option. if(args[i].StartsWith("-")) { var key = args[i++]; var value = key; // Check to see if there's a value that goes with this option? if((i < args.Length) && (!args[i].StartsWith("-"))) value = args[i++]; parsedArgs.Add(key, value); } // If the first argument in this iteration doesn't start with a dash, it's a value else { parsedArgs.Add("--NoKey" + n.ToString(), args[i++]); n++; } } return parsedArgs; } // // Method to get an argument from the parsed command-line arguments // // Parameters: // - parsedArgs: The Dictionary object returned from the Parse() method (shown above). // - defaultValue: The default string to return if the specified key isn't in parsedArgs. // - keys: An array of keys to look for in parsedArgs. public static string GetArgument( Dictionary<string,string> parsedArgs, string defaultReturn, params string[] keys) { string retval = null; foreach(var key in keys) if(parsedArgs.TryGetValue(key, out retval)) break; return retval ?? defaultReturn; } // // Method to exit the application with an error. public static void ErrorExit(string msg, int code=1) { Console.WriteLine("\nError"); Console.WriteLine(msg); Environment.Exit(code); } } }