Created
April 17, 2024 06:22
-
-
Save made2591/10675b33d3508228f421eaf6cc91dbc9 to your computer and use it in GitHub Desktop.
List all unencrypted volumes using all the available profile configured and write them to a CSV file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import csv | |
def list_unencrypted_volumes(session): | |
# Initialize the EC2 client using the provided session | |
ec2_client = session.client('ec2') | |
unencrypted_volumes = [] | |
next_token = None | |
while True: | |
# Parameters for describe_volumes | |
params = {} | |
if next_token: | |
params['NextToken'] = next_token | |
# Get a list of all EBS volumes | |
response = ec2_client.describe_volumes(**params) | |
# Iterate over each volume | |
for volume in response['Volumes']: | |
# Check if encryption is not enabled | |
if 'Encrypted' in volume and not volume['Encrypted']: | |
# Extract required information | |
volume_id = volume['VolumeId'] | |
volume_name = "" | |
volume_creation_date = volume['CreateTime'] | |
if 'Tags' in volume: | |
for tag in volume['Tags']: | |
if tag['Key'] == 'Name': | |
volume_name = tag['Value'] | |
# Add volume details to the list | |
unencrypted_volumes.append({ | |
'Profile': session.profile_name, | |
'VolumeId': volume_id, | |
'Name': volume_name, | |
'CreationDate': volume_creation_date | |
}) | |
# Check if there are more volumes to retrieve | |
if 'NextToken' in response: | |
next_token = response['NextToken'] | |
else: | |
break | |
return unencrypted_volumes | |
def write_to_csv(volumes): | |
# Define the CSV file name | |
csv_file = 'unencrypted_volumes.csv' | |
# Write the volumes data to a CSV file | |
with open(csv_file, mode='w', newline='') as file: | |
fieldnames = ['Profile', 'VolumeId', 'Name', 'CreationDate'] | |
writer = csv.DictWriter(file, fieldnames=fieldnames) | |
# Write header | |
writer.writeheader() | |
# Write each volume's details | |
for volume in volumes: | |
writer.writerow(volume) | |
print(f"Data written to {csv_file}") | |
def main(): | |
# Get the list of available profiles from the AWS config file | |
session = boto3.Session() | |
available_profiles = session.available_profiles | |
# Iterate over each profile | |
all_unencrypted_volumes = [] | |
for profile_name in available_profiles: | |
print(f"Processing profile: {profile_name}") | |
# Create a session using the current profile | |
session = boto3.Session(profile_name=profile_name) | |
# Call function to list unencrypted volumes for the current profile | |
unencrypted_volumes = list_unencrypted_volumes(session) | |
# Add volumes of this profile to the list | |
all_unencrypted_volumes.extend(unencrypted_volumes) | |
# Write all unencrypted volumes to CSV | |
write_to_csv(all_unencrypted_volumes) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment