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

{
Name = "vpc-id",
Values = new List<string>() {vpcID}
};
var dsgRequest = new DescribeSecurityGroupsRequest();
dsgRequest.Filters.Add(vpcFilter);
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;
}
}
Creating a Security Group
The examples in this section follow from the examples in the previous section. If the security group doesn't
already exist, create it. Note that if you were to specify the same name as an existing security group,
CreateSecurityGroup throws an exception.
To create a security group for EC2-Classic
Create and initialize a CreateSecurityGroupRequest object. Assign a name and description to the
GroupName and Description properties, respectively.
The CreateSecurityGroup method returns a CreateSecurityGroupResponse object.You can get the ID
of the new security group from the response and then use DescribeSecurityGroups with the security
group ID to get the SecurityGroup object for the security group.
if (mySG == null)
{
var newSGRequest = new CreateSecurityGroupRequest()
{
GroupName = secGroupName,
Description = "My sample security group for EC2-Classic"
};
var csgResponse = ec2Client.CreateSecurityGroup(newSGRequest);
Console.WriteLine();
Console.WriteLine("New security group: " + csgResponse.GroupId);
List<string> Groups = new List<string>() { csgResponse.GroupId };
var newSgRequest = new DescribeSecurityGroupsRequest() { GroupIds = Groups
};
var newSgResponse = ec2Client.DescribeSecurityGroups(newSgRequest);
mySG = newSgResponse.SecurityGroups[0];
}
To create a security group for EC2-VPC
Create and initialize a CreateSecurityGroupRequest object. Assign values to the GroupName, Description,
and VpcId properties.
Version v2.0.0
38
AWS SDK for .NET Developer Guide
Create a Security Group