Attach / Detach Security Group from EC2 instance using boto3
<p>How can I go about disassociating a particular security group from all EC2 instances and then associate it with a new EC2 instance, with <code>boto3</code>?</p>
<p>I'm trying something like:</p>
<pre><code>ec2 = boto3.resource('ec2')
instances = ec2.instances.filter()
for instance in instances:
    print(instance.id, instance.instance_type)
    for sg in instance.security_groups:
    if sg['GroupId'] == sg_id:
        instance.modify_attribute ???
</code></pre>