0

I want to write a ruby script using fog (a wrapper for aws-sdk). Of course I could hard code my aws secret key and ID but would like to be able to have it set dynamically to my shell env variable since I manage multiple accounts.

require 'fog'
require 'json'
require 'logger'

aws_key_id = $aws_key_id
aws_secret_key = $aws_secret_key
queue_url = $sqs_queue_url

Would this work?

steenslag
  • 79,051
  • 16
  • 138
  • 171
brenguy
  • 21
  • 2
  • sorry I didn't get.. do you you want to read from the env variables or write to it from a ruby script? – zekus May 26 '15 at 20:36
  • Yeah I would want to have my bash env variables be changing on the cli... and then when I run the script the ruby variables would change accordingly. So read from the env variables. – brenguy May 26 '15 at 20:42

1 Answers1

6

In ruby you use ENV to access those values.

require 'fog'
require 'json'
require 'logger'

aws_key_id = ENV['aws_key_id']
aws_secret_key = ENV['aws_secret_key']
queue_url = ENV['sqs_queue_url']

If you want to provide default values, you can use ENV.fetch('key', [default]): ENV.fetch('sqs_queue_url', 'http://localhost')

kevinthompson
  • 574
  • 3
  • 15