Last active
August 22, 2022 20:57
-
-
Save sblack4/720a8684e8ef659761032bd6179c0145 to your computer and use it in GitHub Desktop.
Script to turn all the SSM parameters into terraform resources. Uses Pagination. Can also output JSON.
This file contains hidden or 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
#!/bin/env python3 | |
""" | |
Script to turn all the SSM parameters in one region into terraform resources | |
it can also turn them into JSON with get_parameters_with_values_as_json() | |
Use different regions with the env var AWS_DEFAULT_REGION | |
ie for Northern Cali use: | |
export AWS_DEFAULT_REGION=us-west-1 | |
""" | |
import boto3 | |
import datetime | |
import json | |
from typing import List, Dict, Union, Any | |
session = boto3.Session() | |
client = session.client('ssm') | |
JSONType = Union[str, int, float, bool, None, Dict[str, Any], List[Any]] | |
def merge_dicts(dict1: dict, dict2: dict) -> dict: | |
"""Helper function to merge dictionaries with common key/values """ | |
return {**dict1, **dict2} | |
def default(o: Any) -> str: | |
"""Helper function to serialize datetimes | |
thnx https://stackoverflow.com/a/11875813/5568528 | |
""" | |
if isinstance(o, (datetime.date, datetime.datetime)): | |
return o.isoformat() | |
def get_parameter_descriptions() -> List[dict]: | |
"""describe-parametes from aws cli""" | |
# parameters_object = client.describe_parameters() | |
paginator = client.get_paginator('describe_parameters') | |
response_iterator = paginator.paginate() | |
parameter_descriptions = [] | |
for page in response_iterator: | |
parameter_descriptions += page["Parameters"] | |
return parameter_descriptions | |
def get_parameter_by_name(parameter_name: str) -> dict: | |
"""Gets parameters value and other attributes from AWS""" | |
parameter_object = client.get_parameter( | |
Name=parameter_name, | |
WithDecryption=True) | |
parameter = parameter_object["Parameter"] | |
return parameter | |
def get_parameters_with_values() -> List[dict]: | |
"""Loops through all the params | |
and merges descriptions with values""" | |
parameter_descriptions = get_parameter_descriptions() | |
updated_parameters = [] | |
for parameter_description in parameter_descriptions: | |
parameter_name = parameter_description["Name"] | |
parameter = get_parameter_by_name(parameter_name) | |
updated_parameter = merge_dicts(parameter_description, parameter) | |
updated_parameters.append(updated_parameter) | |
return updated_parameters | |
def get_parameters_with_values_as_json() -> JSONType: | |
"""Returns all our goodies in json format""" | |
parameters_with_values = get_parameters_with_values() | |
parameters_json = json.dumps( | |
parameters_with_values, | |
default=default) | |
return parameters_json | |
def get_parameter_as_terraform(parameter_dict: dict) -> str: | |
"""Turns a param dictionary into a terraform resource""" | |
resource_name = parameter_dict["Name"].replace('/', '_') | |
resource = f'resource "aws_ssm_parameter" "{resource_name}" {{\n' | |
resource += f' name = "{parameter_dict["Name"]}"\n' | |
resource += f' description = "{parameter_dict.get("Description", "")}"\n' | |
resource += f' type = "{parameter_dict["Type"]}"\n' | |
if "\n" in param_value: | |
resource += f' value = <<EOF\n{parameter_dict["Value"]}\nEOF\n' | |
else: | |
resource += f' value = "{parameter_dict["Value"]}"\n' | |
resource += f'}}\n' | |
return resource | |
def get_parameters_as_terraform() -> str: | |
"""Gets all the params and turns them into a big terraform string""" | |
parameters_with_values = get_parameters_with_values() | |
params_terraform = '' | |
for param in parameters_with_values: | |
this_resource = get_parameter_as_terraform(param) | |
params_terraform += this_resource + '\n' | |
return params_terraform | |
print(get_parameters_as_terraform()) | |
# print(get_parameters_with_values_as_json()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment