User guide
Table Of Contents
- AWS SDK for .NET
- Table of Contents
- AWS SDK for .NET Developer Guide
- Getting Started with the AWS SDK for .NET
- Programming with the AWS SDK for .NET
- AWS SDK for .NET Tutorials and Examples
- Managing ASP.NET Session State with Amazon DynamoDB
- Tutorial: Creating Amazon EC2 Instances with the AWS SDK for .NET
- Tutorial: Grant Access Using an IAM Role and the AWS SDK for .NET
- Tutorial: Amazon EC2 Spot Instances
- Creating and Using an Amazon SQS Queue with the AWS SDK for .NET
- Creating an Amazon Route 53 Hosted Zone and Adding Resource Record Sets
- Additional Resources
- Document History

Create a Security Group Using the SDK for .NET
Create a security group, which acts as a virtual firewall that controls the network traffic for one or more
EC2 instances. By default, Amazon EC2 associates your instances with a security group that allows no
inbound traffic.You can create a security group that allows your EC2 instances to accept certain traffic.
For example, if you need to connect to an EC2 Windows instance, you must configure the security group
to allow RDP traffic.You can create a security group using the Amazon EC2 console or the SDK for .NET.
You create a security group for use in either EC2-Classic or EC2-VPC. For more information about EC2-
Classic and EC2-VPC, see Supported Platforms in the Amazon Elastic Compute Cloud User Guide for
Microsoft Windows.
Alternatively, you can create a security group using the Amazon EC2 console. For more information, see
Amazon EC2 Security Groups in the Amazon Elastic Compute Cloud User Guide for Microsoft Windows.
Contents
• Enumerating Your Security Groups (p. 37)
• Creating a Security Group (p. 38)
• Adding Rules to Your Security Group (p. 39)
Enumerating Your Security Groups
You can enumerate your security groups and check whether a particular security group exists.
To enumerate your security groups
Get the complete list of your security groups using DescribeSecurityGroups with no parameters. The
following example checks each security group to see whether its name is my-sample-sg.
string secGroupName = "my-sample-sg";
SecurityGroup mySG = null;
var dsgRequest = new DescribeSecurityGroupsRequest();
var dsgResponse = ec2Client.DescribeSecurityGroups(dsgRequest);
List<SecurityGroup> mySGs = dsgResponse.SecurityGroups;
foreach (SecurityGroup item in mySGs)
{
Console.WriteLine("Existing security group: " + item.GroupId);
if (item.GroupName == secGroupName)
{
mySG = item;
}
}
To enumerate your security groups for a VPC
To enumerate the security groups for a particular VPC, use DescribeSecurityGroups with a filter.The
following example checks each security group for a security group with the name my-sample-sg-vpc.
string secGroupName = "my-sample-sg-vpc";
SecurityGroup mySG = null;
string vpcID = "vpc-f1663d98";
Filter vpcFilter = new Filter
Version v2.0.0
37
AWS SDK for .NET Developer Guide
Create a Security Group