보안 그룹 열거 - AWS SDK for .NET

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

보안 그룹 열거

이 예제에서는 를 사용하여 보안 그룹을 열거 AWS SDK for .NET 하는 방법을 보여줍니다. Amazon Virtual Private Cloud ID를 제공하는 경우 애플리케이션은 특정 에 대한 보안 그룹을 열거합니다VPC. 그렇지 않으면 애플리케이션은 사용 가능한 모든 보안 그룹 목록만 표시합니다.

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

보안 그룹 열거

다음 코드 조각은 보안 그룹을 열거합니다. 지정된 VPC 경우 특정 에 대한 모든 그룹 또는 그룹을 열거합니다.

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

// // Method to enumerate the security groups private static async Task EnumerateGroups(IAmazonEC2 ec2Client, string vpcID) { // A request object, in case we need it. var request = new DescribeSecurityGroupsRequest(); // Put together the properties, if needed if(!string.IsNullOrEmpty(vpcID)) { // We have a VPC ID. Find the security groups for just that VPC. Console.WriteLine($"\nGetting security groups for VPC {vpcID}...\n"); request.Filters.Add(new Filter { Name = "vpc-id", Values = new List<string>() { vpcID } }); } // Get the list of security groups DescribeSecurityGroupsResponse response = await ec2Client.DescribeSecurityGroupsAsync(request); // Display the list of security groups. foreach (SecurityGroup item in response.SecurityGroups) { Console.WriteLine("Security group: " + item.GroupId); Console.WriteLine("\tGroupId: " + item.GroupId); Console.WriteLine("\tGroupName: " + item.GroupName); Console.WriteLine("\tVpcId: " + item.VpcId); Console.WriteLine(); } }

전체 코드

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

NuGet 패키지:

프로그래밍 요소:

using System; using System.Threading.Tasks; using System.Collections.Generic; using Amazon.EC2; using Amazon.EC2.Model; namespace EC2EnumerateSecGroups { class Program { static async Task Main(string[] args) { // Parse the command line string vpcID = string.Empty; if(args.Length == 0) { Console.WriteLine("\nEC2EnumerateSecGroups [vpc_id]"); Console.WriteLine(" vpc_id - The ID of the VPC for which you want to see security groups."); Console.WriteLine("\nSince you specified no arguments, showing all available security groups."); } else { vpcID = args[0]; } if(vpcID.StartsWith("vpc-") || string.IsNullOrEmpty(vpcID)) { // Create an EC2 client object var ec2Client = new AmazonEC2Client(); // Enumerate the security groups await EnumerateGroups(ec2Client, vpcID); } else { Console.WriteLine("Could not find a valid VPC ID in the command-line arguments:"); Console.WriteLine($"{args[0]}"); } } // // Method to enumerate the security groups private static async Task EnumerateGroups(IAmazonEC2 ec2Client, string vpcID) { // A request object, in case we need it. var request = new DescribeSecurityGroupsRequest(); // Put together the properties, if needed if(!string.IsNullOrEmpty(vpcID)) { // We have a VPC ID. Find the security groups for just that VPC. Console.WriteLine($"\nGetting security groups for VPC {vpcID}...\n"); request.Filters.Add(new Filter { Name = "vpc-id", Values = new List<string>() { vpcID } }); } // Get the list of security groups DescribeSecurityGroupsResponse response = await ec2Client.DescribeSecurityGroupsAsync(request); // Display the list of security groups. foreach (SecurityGroup item in response.SecurityGroups) { Console.WriteLine("Security group: " + item.GroupId); Console.WriteLine("\tGroupId: " + item.GroupId); Console.WriteLine("\tGroupName: " + item.GroupName); Console.WriteLine("\tVpcId: " + item.VpcId); Console.WriteLine(); } } } }

추가 고려 사항

  • 필터가 이름-값 페어의 Name 일부가 'vpc-id'로 설정된 상태로 구성된 VPC 경우를 확인합니다. 이 이름은 DescribeSecurityGroupsRequest 클래스 Filters 속성에 대한 설명에서 가져옵니다.

  • Amazon EC2 콘솔 에서 보안 그룹 목록을 확인하여 결과를 확인할 수 있습니다.