0

I have an instance that I would like to run a user script when it starts. So I put this code in the userdata:

Content-Type: multipart/mixed; boundary="===============zCWTbYbXsHmnlc1kxsQr93CA=="
MIME-Version: 1.0

--===============zCWTbYbXsHmnlc1kxsQr93CA==
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config"

output: { all: '|tee -a /var/log/cloud-init-output.log' }

--===============zCWTbYbXsHmnlc1kxsQr93CA==
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="zz_bootstrap.rb"

#!/usr/bin/ruby

require 'rubygems'
require 'json'

system "sudo gem install aws-sdk"

require 'aws-sdk'

r53=nil
Retrier.new(3,1).run do
  r53 = AWS::Route53.new(   :access_key_id => creds["AccessKeyId"],
                              :secret_access_key => creds["SecretAccessKey"],
                              :region => region)
end

Some_ruby_stuff_that_needs_r53

However in the log I get this:

Successfully installed mini_portile-0.6.2
Building native extensions.  This could take a while...
Successfully installed nokogiri-1.6.6.2
Successfully installed aws-sdk-v1-1.61.0
Successfully installed aws-sdk-1.61.0
Parsing documentation for aws-sdk-1.61.0
Installing ri documentation for aws-sdk-1.61.0
Parsing documentation for aws-sdk-v1-1.61.0
Installing ri documentation for aws-sdk-v1-1.61.0
Parsing documentation for mini_portile-0.6.2
Installing ri documentation for mini_portile-0.6.2
Parsing documentation for nokogiri-1.6.6.2
Installing ri documentation for nokogiri-1.6.6.2
Done installing documentation for aws-sdk, aws-sdk-v1, mini_portile, nokogiri after 55 seconds
4 gems installed
/usr/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- aws-sdk (LoadError)
    from /usr/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /var/lib/cloud/instance/scripts/zz_bootstrap.rb:8:in `<main>'
2015-01-27 13:49:46,859 - util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/zz_bootstrap.rb [1]
2015-01-27 13:49:46,875 - cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
2015-01-27 13:49:46,875 - util.py[WARNING]: Running scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
Cloud-init v. 0.7.5 finished at Tue, 27 Jan 2015 13:49:47 +0000. Datasource DataSourceEc2.  Up 389.95 seconds

So I decided to check outside the scope of userdata:

gilz@AAAA:~$ which ruby
/usr/bin/ruby
gilz@AAAA:~$ ruby -v
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux-gnu]
gilz@AAAA:~$ gem list

*** LOCAL GEMS ***

aws-sdk (1.61.0)
aws-sdk-v1 (1.61.0)
bigdecimal (1.2.4)
celluloid (0.15.2)
celluloid-io (0.15.0)
eye (0.6.3)
io-console (0.4.2)
json (1.8.1)
mini_portile (0.6.2)
minitest (4.7.5)
nio4r (1.0.1)
nokogiri (1.6.6.2)
psych (2.0.5)
rake (10.1.0)
rdoc (4.1.0)
sigar (0.7.3)
state_machine (1.2.0)
test-unit (2.1.5.0)
thor (0.19.1)
timers (1.1.0)

So even if I call "sudo gem install" from within the script, it installs the gem in the wrong ruby version. this is without RVM. I don't care which ruby version (>1.93) this runs in, I just need to use the aws-sdk gem. Ideas?

Thanks!

1 Answer 1

0

Installing within the preliminary phase solves this:

Content-Type: multipart/mixed; boundary="===============zCWTbYbXsHmnlc1kxsQr93CA=="
MIME-Version: 1.0

--===============zCWTbYbXsHmnlc1kxsQr93CA==
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config"

runcmd: - "sudo gem install aws-sdk"

output: { all: '|tee -a /var/log/cloud-init-output.log' }

--===============zCWTbYbXsHmnlc1kxsQr93CA==
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="zz_bootstrap.rb"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.