'How to automate modify security groups inbound rules in aws ec2 using java sdk?

I am trying to find the documentation to update aws ec2 security groups using aws java sdk since 2 days, I am able to find the documentation for creating and deleting the security groups using aws java sdk, but not able to find the same for adding,removing,updating the security group inbound rules using java sdk, if anyone can give me the link to the documentation, or any working sample code in java, it will be really helpful to me.



Solution 1:[1]

Hopefully you found the solution to your question since it was posted a month ago. I am currently working on the same thing and I found that using AuthorizeSecurityGroupIngressRequest allows you to update already existing Security Groups instead of creating new ones every time.

IpRange ipRange = IpRange.builder()
                .cidrIp("0.0.0.0/0").build();

            IpPermission ipPerm = IpPermission.builder()
                .ipProtocol("tcp")
                .toPort(22)
                .fromPort(22)
                .ipRanges(ipRange)
                .build();

            AuthorizeSecurityGroupIngressRequest authRequest =
                AuthorizeSecurityGroupIngressRequest.builder()
                        .groupName(YOUR_GROUP_NAME) // can also use .groupId
                        .ipPermissions(ipPerm)
                        .build();

            ec2.authorizeSecurityGroupIngress(authRequest);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 selimczaouali