2

I am using Terraform to deploy EC2 instance and install boostacks wiki on it. Bookstack offers a bash script that automatically installs the files on the ubuntu server however, when i run the script in the user_data nothing happens. I am not sure why? Here is the user_Data code

user_data= <<-EOF
      # Ensure you have read the above information about what this script does before executing these commands.
      sudo apt install wget
      
      # Download the script
      wget https://raw.githubusercontent.com/BookStackApp/devops/main/scripts/installation-ubuntu-18.04.sh

      # Make it executable
      chmod a+x installation-ubuntu-18.04.sh

      # Run the script with admin permissions
      sudo ./installation-ubuntu-18.04.sh 
            
            

      EOF
tags ={
  Name ="bookstacks_terraform"
}

When the script has run successfully, the user should be prompted to enter the domain. However, nothing happens and I dont know how to test it.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • `user_data` is just a raw data object passed to the software running in the EC2 instance. It's up to software in the EC2 instance to decide how to process it. – Martin Atkins Jan 26 '22 at 18:12
  • Since you mentioned Ubuntu Server I'm betting that the software interpreting your script here is [cloud-init](https://cloudinit.readthedocs.io/en/latest/), and so I added a tag for that so that your question might be visible to folks who have cloud-init knowledge. – Martin Atkins Jan 26 '22 at 18:13
  • However, the main point I'd make here is that `cloud-init` -- and therefore any other software it might run as part of executing your provided script -- is running in a context where it has no direct input or output. It launches like a server installed on your system would, where its only means of output is to write to a log file, and it has no means to prompt for input. – Martin Atkins Jan 26 '22 at 18:14
  • You will probably need to find a different strategy to get the needed result without any prompt. – Martin Atkins Jan 26 '22 at 18:14
  • Hopefully someone more familiar with cloud-init can suggest typical techniques for debugging it; I assume it's writing some useful information to [a log file](https://cloudinit.readthedocs.io/en/latest/topics/logging.html), but I don't know what logging settings Ubuntu Server AMIs include and thus where to find the resulting logs. – Martin Atkins Jan 26 '22 at 18:15
  • I'm not familiar with how terraform interfacts with cloud-init, but typically cloud-init userdata requires a header. For a simple shell script, that's just a `#!`. Have you tried adding something like `#!/bin/bash` to your script? https://cloudinit.readthedocs.io/en/latest/topics/format.html – falcojr Jan 26 '22 at 20:33
  • Thanks for getting back! You were correct, I checked /var/log/cloud-init-output.log and found that the script was actually running okay however, the script requires for me to somehow pass the domain info to it. As a result of not getting domain as an input it skipped the step. I now have to find a way to somehow pass the domain info that I have to the script itself. – Karthik Thovinakere Jan 28 '22 at 17:59

1 Answers1

1

Your apt will hang for your confirmation. You forgot about -y:

sudo apt install -y wget
Marcin
  • 215,873
  • 14
  • 235
  • 294