Skip to content

Instantly share code, notes, and snippets.

@fabiensebban
Created June 6, 2023 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiensebban/a2d4b1fa86586609043f9b09d0df71d5 to your computer and use it in GitHub Desktop.
Save fabiensebban/a2d4b1fa86586609043f9b09d0df71d5 to your computer and use it in GitHub Desktop.
This script checks if SSM variables declared in the `template.yaml` file exist. If they do not exist, this script will create it for you.
require 'aws-sdk-ssm'
module Tasks::CheckEnvVariables
AVAILABLE_ENVS = %w[qa production].freeze
def self.perform
template = YAML.load_file('./template.yaml')
ssm_client = Aws::SSM::Client.new(
region: 'eu-west-3',
credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
)
template['Globals']['Function'].each do |resource_name, resource|
if resource_name == 'Environment'
env_variables = resource['Variables']
ssm_keys = []
next if env_variables.nil?
env_variables.each do |key, value|
AVAILABLE_ENVS.each do |env|
if value.include?('x-crypteia-ssm:')
ssm_keys << value.gsub("x-crypteia-ssm:","").gsub("${RailsEnv}",env)
end
end
end
ssm_keys.each do |key|
begin
resp = ssm_client.get_parameter({
name: key,
})
rescue Aws::SSM::Errors::ParameterNotFound => e
puts "Creating SSM parameter #{key}....."
ssm_client.put_parameter(
name: key,
value: "TODO",
type: 'String',
overwrite: true
)
end
puts "Created SSM parameter: #{key} !"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment