Creating EC2 Ingress rule in C#
I'm trying to create an ingress rule in C# and I'm getting an error at runtime. Here's the relevant code: `
///////////BEGIN Set Vars//////////////////////
///////////////////////////////////////////////
Amazon.EC2.AmazonEC2Client ec2Client = new Amazon.EC2.AmazonEC2Client();
Amazon.EC2.Model.AuthorizeSecurityGroupIngressRequest secRequest = new
**Amazon.EC2.Model.AuthorizeSecurityGroupIngressRequest();
Amazon.EC2.Model.IpPermission ipPerm = new Amazon.EC2.Model.IpPermission();
Amazon.EC2.Model.IpRange ipRange = new Amazon.EC2.Model.IpRange();
List<Amazon.EC2.Model.IpPermission> ipRangeList = new List<Amazon.EC2.Model.IpPermission>();
///////////////////////////////////////////////
///////////END Set Vars////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
///////////BEGIN IP Range//////////////////////
///////////////////////////////////////////////
ipRange.CidrIp = "5.5.5.10/32";
ipRange.Description = "My new IP rule";
ipRangeList.Add(ipPerm);
///////////////////////////////////////////////
///////////END IP Range////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
///////////BEGIN IP Perms//////////////////////
///////////////////////////////////////////////
ipPerm.IpProtocol = "tcp";
ipPerm.ToPort = 3389;
ipPerm.FromPort = 3389;
ipPerm.Ipv4Ranges.AddRange((IEnumerable<Amazon.EC2.Model.IpRange>)ipRangeList);
///////////////////////////////////////////////
///////////END IP Perms////////////////////////
///////////////////////////////////////////////`
If I just try to add ipRange as a range to ipPerm, the precompiler complains that it needs to be type of List<Amazon.EC2.Model.IpPermission>.
When I use the code above and cast it to List<Amazon.EC2.Model.IpPermission>, the precompiler gets happy, but I get a runtime error:
** Message=Unable to cast object of type 'System.Collections.Generic.List1[Amazon.EC2.Model.IpPermission]' to type 'System.Collections.Generic.IEnumerable
1[Amazon.EC2.Model.IpRange]'.
Source=System.Private.CoreLib
StackTrace:
at System.Runtime.CompilerServices.CastHelpers.ChkCastAny(Void* toTypeHnd, Object obj)
at AWSFirewall.Program.Main(String[] args) in C:\Users\SeanMcCown\source\repos\AWSFirewall\Program.cs:line 44**
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);
Relevant questions
Boto3 Lambda - Existing custom IoT Rule as event source
asked 4 months agohow to set a proper policy for the role attached to a republish rule in IoT
asked a year agoCan't get EventBridge rule to create a message in SQS
asked 3 months agoError on Dashboard "An error occurred" and unable to create instance due to "Error loading AMI data".
asked a month agoUWP C++ S3 PutObject (upload file) error: Unable to parse ExceptionName: BadRequest Message: An error occurred when parsing the HTTP request.
asked a year agoErrors at dimensions (empty value) in Timestream from an IoT Rule
Accepted Answerasked 6 months agoCan't connect to EC2 with chained security groups
asked 6 months agoCreating EC2 Ingress rule in C#
asked 11 days agoCan you create AWSBehavior API calls from C++?
Accepted Answerasked 4 years agoBuild an IOS Application Tutorial - Trigger Authentication at Run Time
asked 5 months ago