Search code examples
amazon-ec2ec2-amiec2-api-tools

Performing rake tasks in a user data script on AWS ec2


I am trying to fully launch my Rails application as part of the user data script passed to the launch instance EC2 API. The script sets proper keys, downloads the app from my git repo, and then it should run some initialization scripts, starting with the:

bundle install

which should install all the missing gems. However, it fails. Other commands work just fine.

Original failure was that it could not find the bundle command, I have fixed that by manually setting environment variables, but still Ruby wasn't able to find the bundler gem.

My problem here is that when I ssh (once the user data script has failed) to the launched instance, bundler work with no problem. This gives me no way to debug the original problem.

Under which credentials is user data being executed in ec2? Can this be the problem? Is there some special reason why bundler cannot be called from a bash script?


Solution

  • Did you change directories to rails app?

    You might try just calling bundle as well. If you haven't already found a solution, I'd try something like this.

    #Pipe stdout and stderr to a log file.   
    exec &> /home/ubuntu/startup.log
    
    DIR=/home/ubuntu/[rails app]
    
    echo "[START UP] change directory: " $DIR
    cd $DIR
    
    echo "[START UP] Update code" 
    sudo -u ubuntu git pull origin master
    
    echo "[START UP] Bundling latest codebase"
    bundle