boto3搜索未使用的安全组 [英] boto3 searching unused security groups

查看:111
本文介绍了boto3搜索未使用的安全组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AWS Python SDK Boto3,并且试图了解未使用哪些安全组。使用boto2我做到了,但是我不知道该如何使用boto3。



boto.ec2.connection导入EC2Connection
从boto.ec2.regioninfo导入RegionInfo
导入boto.sns
导入sys
导入日志记录
从security_groups_config导入配置

#获取config.py中的设置
aws_access_key = config ['aws_access_key']
aws_secret_key = config ['aws_secret_key']
ec2_region_name = config ['ec2_region_name']
ec2_region_endpoint = config [' ec2_region_endpoint']

region = RegionInfo(name = ec2_region_name,endpoint = ec2_region_endpoint)

if aws_access_key:
conn = EC2Connection(aws_access_key,aws_secret_key,region = region)
else:
conn = EC2Connection(region = region)

sgs = conn.get_all_security_groups()

##如果实例号为SG,则搜索未使用的SG是0
def search_unused_sg(event,context):
for sg in sgs:
print sg.name,len(sg.in stances())


解决方案

首先,我建议您回顾一下boto3处理凭证。更好地使用通用的AWS凭证文件,因此在将来需要时,您可以切换到IAM角色基础凭证或AWS STS,而无需更改代码。

  import boto3 
#您应使用凭据配置文件
ec2 = boto3.client( ec2 )

#在boto3中,如果条目数超过1000,则需要使用NextToken参数处理分页
#,此处未显示。

all_instances = ec2.describe_instances()
all_sg = ec2.describe_security_groups()

instance_sg_set = set()
sg_set = set()

用于all_instances中的预订[ Reservations]:
用于预订[ Instances]中的实例:
代表sg in instance [ SecurityGroups]:
instance_sg_set。 add(sg [ GroupName])


是all_sg [ SecurityGroups]中的security_group:
sg_set.add(security_group [ GroupName])

idle_sg = sg_set-instance_sg_set

注意:代码未经测试。请根据需要对其进行调试。


I am using AWS Python SDK Boto3 and I am trying to know which security groups are unused. With boto2 I did it but I do not know how to do the same with boto3.

from boto.ec2.connection import EC2Connection
from boto.ec2.regioninfo import RegionInfo
import boto.sns
import sys
import logging
from security_groups_config import config

# Get settings from config.py
aws_access_key = config['aws_access_key']
aws_secret_key = config['aws_secret_key']    
ec2_region_name = config['ec2_region_name']
ec2_region_endpoint = config['ec2_region_endpoint']

region = RegionInfo(name=ec2_region_name, endpoint=ec2_region_endpoint)

if aws_access_key:
    conn = EC2Connection(aws_access_key, aws_secret_key, region=region)
else:
    conn = EC2Connection(region=region)

sgs = conn.get_all_security_groups()

## Searching unused SG if the instances number is 0
def search_unused_sg(event, context):
    for sg in sgs:
        print sg.name, len(sg.instances())

解决方案

First , I suggest you relook how boto3 deal with credential. Better use a genereic AWS credential file , so in the future when required, you can switch to IAM roles base credential or AWS STS without changing your code.

import boto3 
# You should use the credential profile file 
ec2 = boto3.client("ec2")

# In boto3, if you have more than 1000 entries, you need to handle the pagination
# using the NextToken parameter, which is not shown here.

all_instances = ec2.describe_instances() 
all_sg = ec2.describe_security_groups()

instance_sg_set = set()
sg_set = set()

for reservation in all_instances["Reservations"] :
  for instance in reservation["Instances"]: 
    for sg in instance["SecurityGroups"]:
      instance_sg_set.add(sg["GroupName"]) 


for security_group in all_sg["SecurityGroups"] :
  sg_set.add(security_group ["GroupName"])

idle_sg = sg_set - instance_sg_set

Note : code are not tested. Please debug it as required.

这篇关于boto3搜索未使用的安全组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆