Skip to content

Instantly share code, notes, and snippets.

@danielpsf
Created June 6, 2023 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielpsf/4e02ce239bcd23424e42c923b26838e2 to your computer and use it in GitHub Desktop.
Save danielpsf/4e02ce239bcd23424e42c923b26838e2 to your computer and use it in GitHub Desktop.
List SSM parameters and its value in CSV mode (simple print)
import time
from typing import List, Tuple
from boto3 import session
from mypy_boto3_ssm import SSMClient
_PROFILE = "YOUR_PROFILE"
_REGION = "YOUR_AWS_REGION"
_MAX_RESULT=50
def ssm_params(aws_session: session.Session = None) -> List[dict]:
"""
Return a detailed list of all the SSM parameters.
"""
# -------------------------------------------------------------
#
#
# -------------------------------------------------------------
def get_parameter_values(ssm_client: SSMClient, ssm_details: dict) -> Tuple[list, str]:
"""
Retrieve additional attributes for the SSM parameters contained in the 'ssm_details'
dictionary passed in.
"""
# Get the details
ssm_param_details = ssm_details['Parameters']
# Just the names, ma'am
param_names = [result['Name'] for result in ssm_param_details]
# Get the parames, including the values
ssm_params_with_values = ssm_client.get_parameters(Names=param_names,
WithDecryption=True)
resources = []
result: dict
for result in ssm_params_with_values['Parameters']:
# Get the matching parameter from the `ssm_details` dict since this has some of the fields
# that aren't in the `ssm_params_with_values` returned from "get_arameters".
param_details = next((zz for zz in ssm_param_details if zz.get('Name', None) == result['Name']), {})
param_policy = param_details.get('Policies', None)
if len(param_policy) == 0:
param_policy = None
resources.append({
'Name': result['Name'],
'LastModifiedDate': result['LastModifiedDate'],
'LastModifiedUser': param_details.get('LastModifiedUser', None),
'Version': result['Version'],
'Tier': param_details.get('Tier', None),
'Policies': param_policy,
'ARN': result['ARN'],
'DataType': result.get('DataType', None),
'Type': result.get('Type', None),
'Value': result.get('Value', None)
})
next_token = ssm_details.get('NextToken', None)
return resources, next_token
# -------------------------------------------------------------
#
#
# -------------------------------------------------------------
if aws_session is None:
raise ValueError('No session.')
# Create SSM client
aws_ssm_client = aws_session.client('ssm')
next_token = ' '
ssm_resources = []
while next_token is not None:
# The "describe_parameters" call gets a whole lot of info on the defined SSM params,
# except their actual values. Due to this limitation let's call the nested function
# to get the values, and a few other details.
ssm_descriptions = aws_ssm_client.describe_parameters(MaxResults=_MAX_RESULT,
NextToken=next_token,
# In case you want to filter by a specific Path criteria
# ParameterFilters=[
# {
# 'Key': 'Name',
# 'Option': 'Contains',
# 'Values': ['SHOULD_CONTAIN']
# },
# ])
)
# This will get additional details for the params, including values.
current_batch, next_token = get_parameter_values(ssm_client=aws_ssm_client,
ssm_details=ssm_descriptions)
ssm_resources += current_batch
# SSM has a very strict rate limit
time.sleep(5)
print(f'SSM Parameters: {len(ssm_resources)}')
return ssm_resources
params = ssm_params(session.Session(profile_name=_PROFILE, region_name=_REGION))
print("Path;Value")
for param in params:
print("{};{}".format(param['Name'], param['Value']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment