Untitled
unknown
plain_text
a year ago
1.6 kB
12
Indexable
import boto3
import csv
def get_ssm_command_history(region, limit=100):
# Initialize a boto3 SSM client with the specified region
ssm_client = boto3.client('ssm', region_name=region)
unique_ssm_docs = set()
next_token = None
while len(unique_ssm_docs) < limit:
# Retrieve a batch of command invocations
response = ssm_client.list_command_invocations(
MaxResults=min(50, limit - len(unique_ssm_docs)),
NextToken=next_token,
Details=False
)
# Add document names to the set
unique_ssm_docs.update(invocation['DocumentName'] for invocation in response['CommandInvocations'])
# Check if there are more results to fetch
next_token = response.get('NextToken')
if not next_token:
break
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__":
# Specify the region
region = "us-east-1" # Replace with your desired region
# Query unique SSM document names
unique_docs = get_ssm_command_history(region, 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'.")
Editor is loading...
Leave a Comment