User guide
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.
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-VPC",
VpcId = vpcID
};
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];
}
Version v2.0.0
71
AWS SDK for .NET Developer Guide
Tutorial: Creating Amazon EC2 Instances