Skip to content

Instantly share code, notes, and snippets.

@mondaini
Created March 21, 2023 19:07
Show Gist options
  • Save mondaini/dc4581605591571cdc5cc4089b5f9e3b to your computer and use it in GitHub Desktop.
Save mondaini/dc4581605591571cdc5cc4089b5f9e3b to your computer and use it in GitHub Desktop.
Get SSM Parameters and set them as Environment Variables
import boto3
import os
# Please replace with your AWS access key and secret key
aws_access_key = "access_key"
aws_secret_key = "secret_key"
# Please replace with your desired region
region_name = "us-east-1"
# Please replace with your desired path
path = "/path/to/parameters/"
def get_ssm_values(path) -> dict:
print(f"Retrieving parameters from {path} path")
data = {}
ssm = boto3.client(
'ssm',
region_name=region_name,
aws_access_key_id=aws_access_key,
aws_secret_access_key=aws_secret_key
)
paginator = ssm.get_paginator('get_parameters_by_path')
for page in paginator.paginate(Path=path, Recursive=True, WithDecryption=True):
for param in page['Parameters']:
name = param['Name'].split(path)[1] # removes path from name
data[name] = param['Value']
return data
def save_parameters_as_env_vars(parameters):
for key,value in parameters.items():
print(f"Saving {key} as environment variable")
os.environ[key] = value
# Retrieve parameters and save as environment variables
parameters = get_ssm_values(path)
save_parameters_as_env_vars(parameters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment