Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.1 kB
11
Indexable
Never
import boto3
import csv

def get_ssm_command_history(limit=100):
    # Initialize a boto3 SSM client
    ssm_client = boto3.client('ssm')

    # Retrieve the last 'limit' command invocations
    response = ssm_client.list_command_invocations(
        MaxResults=limit,
        Details=False
    )

    # Extract the unique SSM document names
    unique_ssm_docs = set(invocation['DocumentName'] for invocation in response['CommandInvocations'])

    return unique_ssm_docs

def save_to_csv(document_names, filename="ssm_documents.csv"):
    # Write the unique document names to a CSV file
    with open(filename, mode='w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(['DocumentName'])  # Write header
        for doc_name in document_names:
            writer.writerow([doc_name])

if __name__ == "__main__":
    # Query unique SSM document names
    unique_docs = get_ssm_command_history(100)
    
    # Save to CSV file
    save_to_csv(unique_docs, "ssm_documents.csv")

    print(f"Unique SSM document names have been saved to 'ssm_documents.csv'.")
Leave a Comment