BlackSheep BlackSheep - 5 months ago 17
Python Question

how to associate a newly created EC2 security group with an instance

How do I add an EC2 instance to a newly created security group? The security group was created with

client = session.client("ec2")
group_id = client.create_security_group(
DryRun=False,
GroupName=group_name,
VpcId=vpc_id,
Description=description
)

Answer

You will want to modify the attributes of the instance:

modify_attribute(attribute, value, dry_run=False)

modify_attribute changes an attribute of this instance. Use the attribute groupSet - Set of Security Groups or IDs

i.modify_attribute('groupSet','sg-xxxx5678')

See boto.ec2.instance