Pavel Zagalsky
Pavel Zagalsky

Reputation: 1636

Boto3 --> Modifying EC2's instance to have multiple Security Groups

I have a couple of Security Groups I'd like to attach to an EC2 instance. I tried the following but failed:

sg_1 = 'sg-something'
sg_2 = 'sg-else'
response = instance.modify_attribute(Groups=sg_1, sg_2)

And something like this:

response = instance.modify_attribute(Groups=[sg_1, sg_2])

And something like this:

for sg in sg_1, sg_2:
    response = instance.modify_attribute(Groups=[sg_1, sg_2])

It seems like it can only accept one sg at a time but when I pass the second one it overwrites the previous one.

Any ideas? Thanks

Upvotes: 2

Views: 2311

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269340

This worked fine for me:

import boto3

client=boto3('ec2')

response = client.modify_instance_attribute(InstanceId='i-1234',Groups=['sg-1111','sg-2222'])

Or using the resource version:

import boto3

ec2 = boto3.resource('ec2')

instance = ec2.Instance('i-1234')
instance.modify_attribute(Groups=['sg-1111','sg-2222'])

Upvotes: 4

Related Questions