Search code examples
bashjupyter-notebookterraformterraform-provider-oci

how to pass params to bash file from terraform


I am using cloud-init to run commands on my instance using user-data. I want to create password for jupyter notebook that I want to start as systemd.

this is my terraform main.tf

resource "oci_core_instance" "jupyterlab_instance" {
  metadata = {
    ssh_authorized_keys = var.ssh_public_key_file
    user_data           = base64encode(file(var.bootstrap_file) "123456")
  }

bootstrap.sh


function systemd_jupyter_instance() {
  echo "setting up systemd for jupyter at localhost:8888"

#  password=$(python -c "from IPython.lib.security import passwd; print(passwd('PASS'))")
cat <<EOF > /etc/systemd/system/jupyterInst.service
[Unit]
Description=Jupyter instance

[Service]
User=opc
Group=opc
WorkingDirectory=/etc/jupyter/
ExecStart=/usr/local/bin/jupyter-notebook --ip=0.0.0.0 --port 8888 --NotebookApp.password=$1

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl is-active --quiet jupyterInst && sudo systemctl stop jupyterInst
sudo systemctl enable --now jupyterInst
sudo systemctl status jupyterInst

}

function main() {
  systemd_jupyter_instance
}

main

I have tried this and I cannot have space while passing user_data. I have tried remote-exec provisioner but having issues connecting to my instance because I will have to pass my private key in .tf which I want to avoid.

Any help is appreciated. Thanks


Solution

  • In this case I would suggest using the templatefile built-in function [1]. In order to do that, there has to be a slight modification in both the resource and the user data script. In the resource block, it has to be changed to:

    resource "oci_core_instance" "jupyterlab_instance" {
      metadata = {
        ssh_authorized_keys = var.ssh_public_key_file
        user_data           = base64encode(tamplatefile(var.bootstrap_file, 
           password = "123456"
       ))
      }
    

    Then, in the file itself, you would have to remove $1 and add the following:

    #!/bin/bash
    
    systemd_jupyter_instance() {
      echo "setting up systemd for jupyter at localhost:8888"
    
    #  password=$(python -c "from IPython.lib.security import passwd; print(passwd('PASS'))")
    cat <<EOF > /etc/systemd/system/jupyterInst.service
    [Unit]
    Description=Jupyter instance
    
    [Service]
    User=opc
    Group=opc
    WorkingDirectory=/etc/jupyter/
    ExecStart=/usr/local/bin/jupyter-notebook --ip=0.0.0.0 --port 8888 --NotebookApp.password=${password}
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
    sudo systemctl daemon-reload
    sudo systemctl is-active --quiet jupyterInst && sudo systemctl stop jupyterInst
    sudo systemctl enable --now jupyterInst
    sudo systemctl status jupyterInst
    
    }
    
    main() {
      systemd_jupyter_instance
    }
    
    main
    

    Usually, the filename would have a .tpl extension to indicate it is a template.

    EDIT: As per comments, added the shebang at the start of the template.


    [1] https://www.terraform.io/language/functions/templatefile