Skip to content

Instantly share code, notes, and snippets.

@Efrat19
Forked from phill-tornroth/cp-parameter-group.py
Last active November 23, 2020 07:11
Show Gist options
  • Save Efrat19/07f3fadf28a8e6f904d2d39c44f4b516 to your computer and use it in GitHub Desktop.
Save Efrat19/07f3fadf28a8e6f904d2d39c44f4b516 to your computer and use it in GitHub Desktop.
Copies an rds parameter group with support for cross-region copying. Quick bashed out script because AWS cli doesn't support.
"""
Copies a parameter group in RDS, particularly useful for doing cross-region copies (which AWS documents,
but doesn't actually support at the time of this writing.
Usage: python cp-parameter-group.py us-west-1:lookerdb-56 us-west-2:lookerdb-56
"""
import boto3
import sys
def chunks(sequence, chunk_size):
"""
Yields the sequence as a sequence of sequences size chunk_size (or fewer,
in the case of the last chunk). Guarantees delivery of everything (as
opposed to strategies that leave elements off of the end when
len(sequence) % chunk_size != 0
"""
start = 0
while start < len(sequence):
end = start + chunk_size
yield sequence[start : start + chunk_size]
start = end
# region:parameter_name
source_region, source_name = sys.argv[1].split(":")
source_client = boto3.client("rds", region_name=source_region)
source_summary = source_client.describe_db_parameter_groups(
DBParameterGroupName=source_name
)
source_family = source_summary["DBParameterGroups"][0]["DBParameterGroupFamily"]
source_description = source_summary["DBParameterGroups"][0]["Description"]
source_parameters = source_client.describe_db_parameters(
DBParameterGroupName=source_name
)["Parameters"]
source_parameters = [
p for p in source_parameters if p["IsModifiable"] and "ParameterValue" in p
]
target_region, target_name = sys.argv[2].split(":")
target_client = boto3.client("rds", region_name=target_region)
groups_in_target_region = set(
[
g["DBParameterGroupName"]
for g in target_client.describe_db_parameter_groups()["DBParameterGroups"]
]
)
if target_name in groups_in_target_region:
raise ValueError(
"This group (%s) already exists in region %s" % (target_name, target_region)
)
print ("Created %s in %s" % (target_name, target_region))
target_client.create_db_parameter_group(
DBParameterGroupName=target_name,
DBParameterGroupFamily=source_family,
Description=source_description,
)
# AWS limits parameter modifications to 20 at a time
for parameters in chunks(source_parameters, 20):
target_client.modify_db_parameter_group(
DBParameterGroupName=target_name, Parameters=parameters
)
for parameter in parameters:
print ("%s = %s" % (parameter["ParameterName"], parameter["ParameterValue"]))
print ("Complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment