get unused security list from all regions thru boto3











up vote
0
down vote

favorite












i m trying to get unused SG from all regions but its not working .



i tried below code



#!/usr/bin/env python
import boto3

ec2 = boto3.resource('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']

sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())

all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs

print 'Total SGs:', len(all_sgs)
print 'SGS attached to instances:', len(all_inst_sgs)
print 'Orphaned SGs:', len(unused_sgs)
print 'Unattached SG names:', unused_sgs









share|improve this question
























  • You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
    – mootmoot
    Nov 13 at 9:00















up vote
0
down vote

favorite












i m trying to get unused SG from all regions but its not working .



i tried below code



#!/usr/bin/env python
import boto3

ec2 = boto3.resource('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']

sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())

all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs

print 'Total SGs:', len(all_sgs)
print 'SGS attached to instances:', len(all_inst_sgs)
print 'Orphaned SGs:', len(unused_sgs)
print 'Unattached SG names:', unused_sgs









share|improve this question
























  • You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
    – mootmoot
    Nov 13 at 9:00













up vote
0
down vote

favorite









up vote
0
down vote

favorite











i m trying to get unused SG from all regions but its not working .



i tried below code



#!/usr/bin/env python
import boto3

ec2 = boto3.resource('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']

sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())

all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs

print 'Total SGs:', len(all_sgs)
print 'SGS attached to instances:', len(all_inst_sgs)
print 'Orphaned SGs:', len(unused_sgs)
print 'Unattached SG names:', unused_sgs









share|improve this question















i m trying to get unused SG from all regions but its not working .



i tried below code



#!/usr/bin/env python
import boto3

ec2 = boto3.resource('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']

sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())

all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs

print 'Total SGs:', len(all_sgs)
print 'SGS attached to instances:', len(all_inst_sgs)
print 'Orphaned SGs:', len(unused_sgs)
print 'Unattached SG names:', unused_sgs






boto3 aws-security-group






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 8:21









mootmoot

5,91822034




5,91822034










asked Nov 11 at 7:33









arushi

75




75












  • You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
    – mootmoot
    Nov 13 at 9:00


















  • You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
    – mootmoot
    Nov 13 at 9:00
















You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
– mootmoot
Nov 13 at 9:00




You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
– mootmoot
Nov 13 at 9:00












1 Answer
1






active

oldest

votes

















up vote
0
down vote













describe_regions is a function of the ec2 client, not the ec2 resource. Try this:



#!/usr/bin/env python
import boto3

ec2 = boto3.client('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']


However you're not using the region name in any way. The following code iterates across the regions, sets up a new ec2 resource in each region, and repeats your scans.



#!/usr/bin/env python
import boto3


ec2Client = boto3.client('ec2')
regions = ec2Client.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
print ('Checking region {}'.format(reg))

ec2 = boto3.resource('ec2', region_name=reg)

sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())

all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs

print (' Total SGs:', len(all_sgs))
print (' SGS attached to instances:', len(all_inst_sgs))
print (' Orphaned SGs:', len(unused_sgs))
print (' Unattached SG names:', unused_sgs)


While testing I found I have a lot of unused SGs, thanks.






share|improve this answer





















  • Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
    – arushi
    Nov 13 at 12:55










  • hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
    – arushi
    Nov 15 at 12:18












  • response = boto3.client('rds').describe_db_security_groups()
    – weegolo
    Nov 16 at 13:00












  • response = boto3.client('elb').describe_load_balancers()
    – weegolo
    Nov 16 at 13:06










  • Thanks , how can I put my output in one csv file please advise me ?
    – arushi
    Nov 26 at 6:36











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246720%2fget-unused-security-list-from-all-regions-thru-boto3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













describe_regions is a function of the ec2 client, not the ec2 resource. Try this:



#!/usr/bin/env python
import boto3

ec2 = boto3.client('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']


However you're not using the region name in any way. The following code iterates across the regions, sets up a new ec2 resource in each region, and repeats your scans.



#!/usr/bin/env python
import boto3


ec2Client = boto3.client('ec2')
regions = ec2Client.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
print ('Checking region {}'.format(reg))

ec2 = boto3.resource('ec2', region_name=reg)

sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())

all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs

print (' Total SGs:', len(all_sgs))
print (' SGS attached to instances:', len(all_inst_sgs))
print (' Orphaned SGs:', len(unused_sgs))
print (' Unattached SG names:', unused_sgs)


While testing I found I have a lot of unused SGs, thanks.






share|improve this answer





















  • Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
    – arushi
    Nov 13 at 12:55










  • hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
    – arushi
    Nov 15 at 12:18












  • response = boto3.client('rds').describe_db_security_groups()
    – weegolo
    Nov 16 at 13:00












  • response = boto3.client('elb').describe_load_balancers()
    – weegolo
    Nov 16 at 13:06










  • Thanks , how can I put my output in one csv file please advise me ?
    – arushi
    Nov 26 at 6:36















up vote
0
down vote













describe_regions is a function of the ec2 client, not the ec2 resource. Try this:



#!/usr/bin/env python
import boto3

ec2 = boto3.client('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']


However you're not using the region name in any way. The following code iterates across the regions, sets up a new ec2 resource in each region, and repeats your scans.



#!/usr/bin/env python
import boto3


ec2Client = boto3.client('ec2')
regions = ec2Client.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
print ('Checking region {}'.format(reg))

ec2 = boto3.resource('ec2', region_name=reg)

sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())

all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs

print (' Total SGs:', len(all_sgs))
print (' SGS attached to instances:', len(all_inst_sgs))
print (' Orphaned SGs:', len(unused_sgs))
print (' Unattached SG names:', unused_sgs)


While testing I found I have a lot of unused SGs, thanks.






share|improve this answer





















  • Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
    – arushi
    Nov 13 at 12:55










  • hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
    – arushi
    Nov 15 at 12:18












  • response = boto3.client('rds').describe_db_security_groups()
    – weegolo
    Nov 16 at 13:00












  • response = boto3.client('elb').describe_load_balancers()
    – weegolo
    Nov 16 at 13:06










  • Thanks , how can I put my output in one csv file please advise me ?
    – arushi
    Nov 26 at 6:36













up vote
0
down vote










up vote
0
down vote









describe_regions is a function of the ec2 client, not the ec2 resource. Try this:



#!/usr/bin/env python
import boto3

ec2 = boto3.client('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']


However you're not using the region name in any way. The following code iterates across the regions, sets up a new ec2 resource in each region, and repeats your scans.



#!/usr/bin/env python
import boto3


ec2Client = boto3.client('ec2')
regions = ec2Client.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
print ('Checking region {}'.format(reg))

ec2 = boto3.resource('ec2', region_name=reg)

sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())

all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs

print (' Total SGs:', len(all_sgs))
print (' SGS attached to instances:', len(all_inst_sgs))
print (' Orphaned SGs:', len(unused_sgs))
print (' Unattached SG names:', unused_sgs)


While testing I found I have a lot of unused SGs, thanks.






share|improve this answer












describe_regions is a function of the ec2 client, not the ec2 resource. Try this:



#!/usr/bin/env python
import boto3

ec2 = boto3.client('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']


However you're not using the region name in any way. The following code iterates across the regions, sets up a new ec2 resource in each region, and repeats your scans.



#!/usr/bin/env python
import boto3


ec2Client = boto3.client('ec2')
regions = ec2Client.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
print ('Checking region {}'.format(reg))

ec2 = boto3.resource('ec2', region_name=reg)

sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())

all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs

print (' Total SGs:', len(all_sgs))
print (' SGS attached to instances:', len(all_inst_sgs))
print (' Orphaned SGs:', len(unused_sgs))
print (' Unattached SG names:', unused_sgs)


While testing I found I have a lot of unused SGs, thanks.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 9:24









weegolo

394




394












  • Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
    – arushi
    Nov 13 at 12:55










  • hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
    – arushi
    Nov 15 at 12:18












  • response = boto3.client('rds').describe_db_security_groups()
    – weegolo
    Nov 16 at 13:00












  • response = boto3.client('elb').describe_load_balancers()
    – weegolo
    Nov 16 at 13:06










  • Thanks , how can I put my output in one csv file please advise me ?
    – arushi
    Nov 26 at 6:36


















  • Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
    – arushi
    Nov 13 at 12:55










  • hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
    – arushi
    Nov 15 at 12:18












  • response = boto3.client('rds').describe_db_security_groups()
    – weegolo
    Nov 16 at 13:00












  • response = boto3.client('elb').describe_load_balancers()
    – weegolo
    Nov 16 at 13:06










  • Thanks , how can I put my output in one csv file please advise me ?
    – arushi
    Nov 26 at 6:36
















Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
– arushi
Nov 13 at 12:55




Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
– arushi
Nov 13 at 12:55












hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
– arushi
Nov 15 at 12:18






hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
– arushi
Nov 15 at 12:18














response = boto3.client('rds').describe_db_security_groups()
– weegolo
Nov 16 at 13:00






response = boto3.client('rds').describe_db_security_groups()
– weegolo
Nov 16 at 13:00














response = boto3.client('elb').describe_load_balancers()
– weegolo
Nov 16 at 13:06




response = boto3.client('elb').describe_load_balancers()
– weegolo
Nov 16 at 13:06












Thanks , how can I put my output in one csv file please advise me ?
– arushi
Nov 26 at 6:36




Thanks , how can I put my output in one csv file please advise me ?
– arushi
Nov 26 at 6:36


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246720%2fget-unused-security-list-from-all-regions-thru-boto3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation