Skip to content

SSM

Utilities for working with AWS Systems Manager.


get_ssm_parameter

get_ssm_parameter(
    param_name: str, as_dict: Literal[True]
) -> Dict[str, Any]
get_ssm_parameter(
    param_name: str, as_dict: Literal[False] = False
) -> str
get_ssm_parameter(param_name, as_dict=False)

Retrieves a SSM parameter value

Parameters:

Name Type Description Default
param_name str

the SSM parameter key name

required
as_dict bool

Return as dict. Defaults to False.

False

Raises:

Type Description
ValueError

If there is no such key

Returns:

Type Description
Union[str, Dict[str, Any]]

The parameter value stored at the key name

Source code in src/aibs_informatics_aws_utils/ssm.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def get_ssm_parameter(param_name: str, as_dict: bool = False) -> Union[str, Dict[str, Any]]:
    """Retrieves a SSM parameter value

    Args:
        param_name (str): the SSM parameter key name
        as_dict (bool, optional): Return as dict. Defaults to False.

    Raises:
        ValueError: If there is no such key

    Returns:
        The parameter value stored at the key name
    """
    ssm = get_ssm_client()

    response = ssm.get_parameter(Name=param_name, WithDecryption=True)

    param = response["Parameter"]

    param_value = param.get("Value", None)

    if param_value is None:
        raise ValueError(f"Error obtaining param {param_name} from parameter store.")

    if as_dict:
        return json.loads(param_value)
    return param_value

has_ssm_parameter

has_ssm_parameter(param_name)

Check if an SSM parameter exists.

Parameters:

Name Type Description Default
param_name str

The SSM parameter key name to check.

required

Returns:

Type Description
bool

True if the parameter exists, False otherwise.

Source code in src/aibs_informatics_aws_utils/ssm.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def has_ssm_parameter(param_name: str) -> bool:
    """Check if an SSM parameter exists.

    Args:
        param_name (str): The SSM parameter key name to check.

    Returns:
        True if the parameter exists, False otherwise.
    """
    try:
        get_ssm_parameter(param_name)
    except (ValueError, ClientError):
        return False
    else:
        return True

put_ssm_parameter

put_ssm_parameter(
    param_name,
    param_value,
    param_type="String",
    exists_ok=True,
)

Put or update parameter

Parameters:

Name Type Description Default
param_name str

Name of parameter

required
param_value str

New value of parameter

required
param_type ParameterTypeType

Type of parameter. Defaults to "String".

'String'
exists_ok bool

Allow overwrite if value exists. Defaults to True.

True

Returns:

Type Description
int

The version of the parameter

Source code in src/aibs_informatics_aws_utils/ssm.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def put_ssm_parameter(
    param_name: str,
    param_value: str,
    param_type: ParameterTypeType = "String",
    exists_ok: bool = True,
) -> int:
    """Put or update parameter

    Args:
        param_name (str): Name of parameter
        param_value (str): New value of parameter
        param_type (ParameterTypeType, optional): Type of parameter. Defaults to "String".
        exists_ok (bool, optional): Allow overwrite if value exists. Defaults to True.

    Returns:
        The version of the parameter
    """
    ssm = get_ssm_client()
    return ssm.put_parameter(
        Name=param_name, Value=param_value, Type=param_type, Overwrite=exists_ok
    )["Version"]