1 Answer
- Newest
- Most votes
- Most comments
1
You are trying to cast incompatible types, so the .NET runtime generates an exception. You want to add a list of IpPermission
objects into a list of IpRange
objects, which is not possible.
Please refer to the AWS SDK documentation about Updating security groups.
There, you can find a code snippet that shows you how to add an ingress rule to a security group:
async Task AddIngressRuleAsync(IAmazonEC2 eC2Client, string groupID, string ipAddress, int port)
{
// Create an object to hold the request information for the rule.
// It uses an IpPermission object to hold the IP information for the rule.
var ingressRequest = new AuthorizeSecurityGroupIngressRequest { GroupId = groupID };
ingressRequest.IpPermissions.Add(new IpPermission
{
IpProtocol = "tcp",
FromPort = port,
ToPort = port,
Ipv4Ranges = new List<IpRange> { new IpRange { CidrIp = ipAddress } }
});
// Create the inbound rule for the security group
var responseIngress = await eC2Client.AuthorizeSecurityGroupIngressAsync(ingressRequest);
Console.WriteLine($"\nNew RDP rule was written in {groupID} for {ipAddress}.");
Console.WriteLine($"Result: {responseIngress.HttpStatusCode}");
}
You can then use this method to update the security group with your desired permissions:
var ec2Client = new Amazon.EC2.AmazonEC2Client();
var sgId = "your-security-group-id";
var cidr = "5.5.5.10/32";
var port = 3389;
await AddIngressRuleAsync(ec2Client, sgId, cidr, port);
answered 2 years ago
Relevant content
- asked 9 months ago
- asked 4 months ago
- Accepted Answerasked 6 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a month ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago