User guide
To enumerate existing security groups
1. Obtain a list of your account's security groups.
DescribeSecurityGroupsRequest secGroupRequest =
new DescribeSecurityGroupsRequest();
DescribeSecurityGroupsResponse secGroupResponse =
ec2Client.DescribeSecurityGroups(secGroupRequest);
List<SecurityGroup> secGroups = secGroupResponse.SecurityGroups;
This example but does not specify a group name, which directs DescribeSecurityGroups to return a
list containing all of the account's security groups.
2. Enumerate the requested security groups and select the desired group by name.
SecurityGroup secGroup = null;
foreach(SecurityGroup item in secGroups)
{
if (item.GroupName == "GroupName")
{
secGroup = item;
break;
}
}
If your account does not have a suitable security group, you can create a new one, as follows:
Important
Use this procedure only for new security groups. If you attempt to create a new security group
with the same name as one of your account's existing groups, CreateSecurityGroup throws
an exception.
To create a new Amazon EC2 security group
1. Create and initialize a CreateSecurityGroupRequest object.
var newGroupRequest = new CreateSecurityGroupRequest()
{
GroupName = "YourSecurityGroupName",
Description = "YourSecurityGroupDescription"
};
Assign the group's name and description to the object's GroupName and Description properties,
respectively. The two strings must contain only US-ASCII characters and the group name must be
unique within the AWS region in which you initialized your Amazon EC2 client.
2. Create the new security group.
Version v2.0.0
45
AWS SDK for .NET Developer Guide
Specify an Amazon EC2 Security Group