<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>unused security groups &#8211; Blog of Kliment Andreev &#8211; A place so I won&#039;t forget things</title>
	<atom:link href="https://blog.andreev.it/tag/unused-security-groups/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.andreev.it</link>
	<description></description>
	<lastBuildDate>Sat, 24 Oct 2020 13:29:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>AWS, python: Find unused security groups and volumes using boto</title>
		<link>https://blog.andreev.it/2017/01/105-aws-find-unused-security-groups-volumes/</link>
					<comments>https://blog.andreev.it/2017/01/105-aws-find-unused-security-groups-volumes/#respond</comments>
		
		<dc:creator><![CDATA[Kliment Andreev]]></dc:creator>
		<pubDate>Sun, 15 Jan 2017 01:04:59 +0000</pubDate>
				<category><![CDATA[AWS]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[boto]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unused security groups]]></category>
		<category><![CDATA[unused volumes]]></category>
		<guid isPermaLink="false">http://blog.iandreev.com/?p=3009</guid>

					<description><![CDATA[I made two scripts using Python and boto that will list all unused security&#8230;]]></description>
										<content:encoded><![CDATA[<div id="bsf_rt_marker"></div><p>I made two scripts using Python and <a href="https://github.com/boto/boto" target="_blank" rel="noopener noreferrer">boto</a> that will list all unused security groups and volumes for your account. Make sure you have aws cli already installed and configured (see my other <a href="https://blog.andreev.it/?p=1905" target="_blank" rel="noopener noreferrer">post</a>). In addition, you&#8217;ll need python 2.x and boto (pip install boto). It won&#8217;t work with python 3.x and boto3. Also, you&#8217;ll need SES configured for sending e-mails. If you just want to print unused security groups and volumes, you don&#8217;t need SES. Just uncomment the print statement in lines #35 and #33. </p>
<p>Script for unused security groups.</p>
<pre class="brush: python; title: ; notranslate">
##################################################################
# Lists all unused security groups and sends an e-mail through SES
# K.Andreev - 2017 - FreeBSD license
##################################################################

# Import boto modules for ec2 and ses
import boto.ec2
import boto.ses

# Define variables here
sender = 'someone@domain.com'
# Max 50 recipients, use DLs instead of personal emails
recipients = &#x5B;'rec1@d1.com', 'rec2@d2.com']
subject = 'Unused EC2 security groups'
body = 'This is an automated e-mail. For any errors please contact klimenta@futurebit.com\n\n'
ses_region = 'us-east-1'
SES_KEY_ID = 'YOUR_KEY_ID'
SES_SECRET = 'YOUR_SECRET'

# Connect to AWS/EC2
ec2 = boto.connect_ec2()

# Get all security groups
sgs = ec2.get_all_security_groups()

# Loop through all security groups
# Empty string that contains all unused security groups
sglist = ''
for sg in sgs:
    # Get the instance count where the security group is attached
    sglen = len(sg.instances())
    # If the security group is not attached (0) it means it's not attached to an instance
    if sglen == 0 and sg.name != 'default':
        # Use the print statement below for debugging purposes only
        # print sg.name, len(sg.instances())
        # Concatenate security groups to the string
        sglist = sglist + sg.name + '\n'

# Establish a connection with SES
conn = boto.ses.connect_to_region(
        ses_region,
        aws_access_key_id = SES_KEY_ID,
        aws_secret_access_key = SES_SECRET)

# Send an email
conn.send_email(
        sender,
        subject,
        body + sglist,
        recipients)
</pre>
<p>Script for unused volumes. </p>
<pre class="brush: python; title: ; notranslate">
##########################################################
# Lists all unused volumes and sends an e-mail through SES
# K.Andreev - 2017 - FreeBSD license
##########################################################

# Import boto modules for ec2 and ses
import boto.ec2
import boto.ses

# Define variables here
sender = 'sender@whatever.com'
# Max 50 recipients, use DLs instead of personal emails
recipients = &#x5B;'rec1@d1.com', 'rec2@d2.com']
subject = 'Unused EC2 volumes'
body = 'This is an automated e-mail. For any errors please contact klimenta@futurebit.com\n\n'
ses_region = 'us-east-1'
SES_KEY_ID = 'YOUR_KEY_ID'
SES_SECRET = 'YOUR_SECRET'

# Connect to AWS/EC2
ec2 = boto.connect_ec2()

# Get all volumes
vols = ec2.get_all_volumes()

# Loop through all volumes
# Empty string that contains all unused volumes
svols = ''
for vol in vols:
    # If the volume status is available it means it's not attached to an instance
    if vol.status == 'available':
        # Use the print statement below for debugging purposes only
        # print vol.id + &quot; is unused&quot;
        # Concatenate the volume ids to the string
        svols = svols + vol.id + '\n'

# Establish a connection with SES
conn = boto.ses.connect_to_region(
        ses_region,
        aws_access_key_id = SES_KEY_ID,
        aws_secret_access_key = SES_SECRET)

# Send an email
conn.send_email(
        sender,
        subject,
        body + svols,
        recipients)
</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.andreev.it/2017/01/105-aws-find-unused-security-groups-volumes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
