보안 그룹 생성 - AWS SDK for .NET

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

보안 그룹 생성

이 예제에서는 를 사용하여 보안 그룹을 생성하는 AWS SDK for .NET 방법을 보여줍니다. 기존 VPC의 ID를 제공하여 VPC의 EC2에 대한 보안 그룹을 생성할 수 있습니다. 이러한 ID를 제공하지 않으면 AWS 계정에서 이를 지원하는 경우 새 보안 그룹이 EC2-Classic용 보안 그룹이 됩니다.

VPC ID를 제공하지 않고 AWS 계정이 EC2-Classic을 지원하지 않는 경우 새 보안 그룹은 계정의 기본 VPC에 속하게 됩니다.

주의

EC2-Classic은 2022년 8월 15일에 사용 중지되었습니다. EC2-Classic에서 VPC로 마이그레이션하는 것이 좋습니다. 자세한 내용은 블로그 게시물 EC2-Classic 네트워킹 지원 중단 — 준비 방법을 참조하십시오.

다음 섹션에서는 이 예제의 코드 조각을 제공합니다. 예제의 전체 코드는 그 뒤에 표시되며, 그대로 빌드하고 실행할 수 있습니다.

기존 보안 그룹 찾기

다음 코드 조각은 지정된 VPC에서 지정된 이름을 가진 기존 보안 그룹을 검색합니다.

이 주제의 끝 부분에 있는 예제에서는 사용 중인 이 코드 조각을 보여줍니다.

// // 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; }

보안 그룹 생성

다음 코드 조각은 해당 이름을 가진 그룹이 지정된 VPC에 없는 경우 새 보안 그룹을 생성합니다. VPC가 제공되지 않고 같은 이름을 가진 그룹이 하나 이상 존재할 경우 코드 조각은 단순히 그룹 목록을 반환합니다.

이 주제의 끝 부분에 있는 예제에서는 사용 중인 이 코드 조각을 보여줍니다.

// // 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; }

전체 코드

이 섹션에는 이 예제에 대한 관련 참조와 전체 코드가 나와 있습니다.

NuGet 패키지:

프로그래밍 요소:

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); } } }