Search code examples
pythonamazon-web-servicesboto3aws-security-group

AWS Boto3 - Add security group without removing existing security group


I have an use case to add security group to a bulk of ec2 instances in AWS, I chose python boto3 API to perform this action. However when using the below method, it removes the existing security group and then adds the new group. I would just like to add new one to the existing SG.

ec2 = session.client('ec2')
ec2.modify_instance_attribute(InstanceId=xxxx, Groups= ['xxx']]

Any suggestions to do add SG without removing the existing security group?

ec2 = session.client('ec2')
ec2.modify_instance_attribute(InstanceId=xxxx, Groups= ['xxx']]

I understand its modify, but I don't see an option to just add SG.


Solution

  • You have to get the list of existing SGs, add the new one, and then set them all again to the instance.