How to add or remove security groups from ENI using Boto?

I use the Boto Python interface to manage the network defined by EC2 software, and I am writing a method to manage the security of the Group on elastic network interfaces (ENI).

I do not know how to tell EC2 about adding or removing security groups to / from ENI.

My approach is still essentially:

import boto
conn = boto.connect_ec2()

my_eni = conn.get_all_network_interfaces(['eni-xxxx1234'])[0]
my_eni_groups = my_eni.groups
my_eni_sg_ids = [ x.id for x in my_eni_groups ]

desired_sg_state = ['sg-xxxx1234','sg-xxxx5678']

# if present, do nothing, else, add it somehow..
for sg in desired_sg_state:
    if sg in my_eni_sg_ids:
        print('Okay: ', sg)
    else:
        # not sure what method to use here!

I looked through the documentation and could not find anything about association / disassembly of security groups in boto.ec2.securitygroupor objects boto.ec2.networkinterface. I am sure there is a way to do this, but this is not obvious to me.

+4
source share
1 answer

boto.ec2.connection - modify_network_interface_attribute, Elastic Network:

import boto

sg_string_list = ['sg-xxxx1234', 'sg-xxxx5678']

conn = boto.connect_ec2()
conn.modify_network_interface_attribute(interface_id=eni_id,
                                        attr='groupSet',
                                        value=sg_string_list)

: modify_network_interface_attribute(interface_id, attr, value, attachment_id=None, dry_run=False).

, , ENI, / SG modify_network_interface_attribute; , SG, SG:

import boto
conn = boto.connect_ec2()

my_eni_groups = conn.get_all_network_interfaces(['eni-1582af5d'])[0].groups
my_eni_sg_ids = [ x.id for x in my_eni_groups ]

add_sg = 'sg-xxxx1234'

if add_sg not in my_eni_sg_ids:
    my_eni_sg_ids.append(add_sg)

    #only need to call this if we modified the list
    conn.modify_network_interface_attribute(interface_id=eni_id,
                                            attr='groupSet',
                                            value=my_eni_sg_ids)

EC2 Boto, Boto ( , (, , )).

+4

Source: https://habr.com/ru/post/1589295/


All Articles