Created
March 22, 2024 05:23
-
-
Save kenpropel/710c470b18aed129e971d4d71461ca2f to your computer and use it in GitHub Desktop.
Using AWS SSM Parameter Store to load configuration in Ruby
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
require 'aws-sdk-ssm' | |
require_relative './neko-logger' | |
L = Neko::Logger.logger | |
SSMPS_KEY_PATH = '/config/path/' | |
def config | |
# Skip if config was loaded within the last hour | |
if @config_loaded.nil? || (Time.now - @config_loaded > 3600) | |
L.info('Loading config from SSMPS') | |
@config = {} | |
ssm = Aws::SSM::Client.new | |
ps_path = { | |
path: SSMPS_KEY_PATH, | |
recursive: true, | |
with_decryption: true, | |
} | |
ssm.get_parameters_by_path(ps_path).parameters.each do |prm| | |
k = prm[:name][SSMPS_KEY_PATH.length..].gsub(/\//,'_').to_sym | |
@config[k] = prm[:value] | |
end | |
@config_loaded = Time.now | |
L.debug("Config loaded #{@config_loaded}") | |
end | |
@config | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment