Created
January 26, 2025 10:40
-
-
Save resatarikan/209b429a933d5c51ebc97e844b09d483 to your computer and use it in GitHub Desktop.
This Python script automates the migration of AWS Systems Manager (SSM) parameters from one AWS region to another. It retrieves all parameters from the source region, including their values (with decryption for secure strings), and replicates them in the target region. The script ensures parameter integrity by preserving their type, description,…
This file contains 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
import boto3 | |
import json | |
def migrate_ssm_parameters(source_region, target_region): | |
source_client = boto3.client('ssm', region_name=source_region) | |
target_client = boto3.client('ssm', region_name=target_region) | |
next_token = None | |
while True: | |
if next_token: | |
response = source_client.describe_parameters(NextToken=next_token) | |
else: | |
response = source_client.describe_parameters() | |
parameters = response.get('Parameters', []) | |
for param in parameters: | |
param_name = param['Name'] | |
param_details = source_client.get_parameter(Name=param_name, WithDecryption=True) | |
value = param_details['Parameter']['Value'] | |
param_type = param_details['Parameter']['Type'] | |
description = param.get('Description', '') | |
key_id = param.get('KeyId', None) | |
put_param_args = { | |
'Name': param_name, | |
'Value': value, | |
'Type': param_type, | |
'Overwrite': True | |
} | |
if description: | |
put_param_args['Description'] = description | |
if key_id and param_type == 'SecureString': | |
put_param_args['KeyId'] = key_id | |
target_client.put_parameter(**put_param_args) | |
print(f"Migrated: {param_name}") | |
next_token = response.get('NextToken') | |
if not next_token: | |
break | |
print("SSM Parameter migration completed successfully.") | |
if __name__ == "__main__": | |
source_region = "us-east-1" # Change this to your source region | |
target_region = "us-west-1" # Change this to your target region | |
migrate_ssm_parameters(source_region, target_region) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment