はじめに
EC2の起動テンプレートにスクリプトを書いておくと、インスタンス作成時に自動的にスクリプトが実行されます。Deep Learning AMI (Ubuntu 18.04) Version 34.0 – ami-0302aadfa73a0d917を使用し、ディープラーニングが実行できるjupyter notebookを自動で起動する設定をします。
手順
起動テンプレート設定画面を開きます。

起動テンプレートを作成を選択。高度な詳細を開く。

ユーザーデータに下記のスクリプトを貼り付け。

#!/bin/bash
# write conf
cat << EOF > /home/ubuntu/.jupyter/jupyter_notebook_config.py
c = get_config()
c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8080
c.NotebookApp.token = ''
EOF
chown ubuntu:ubuntu /home/ubuntu/.jupyter/jupyter_notebook_config.py
# set auto start
echo "/home/ubuntu/anaconda3/envs/pytorch_p36/bin/jupyter-notebook" > /home/ubuntu/start_jupyter.sh
chmod 755 /home/ubuntu/start_jupyter.sh
chown ubuntu:ubuntu /home/ubuntu/start_jupyter.sh
bash -c 'cat > /etc/rc.local' << EOF
#!/bin/sh
# DLAMI Configurations
/opt/dlami/start/dlami_start
# jupyter
su - ubuntu /home/ubuntu/start_jupyter.sh &
exit 0
EOF
# reboot
shutdown -r now
設定の意味はJupyter事始めを読めば分かると思います。
ユーザーデータの実行ログは/var/log/cloud-init-output.logに出力されます。
Deep Learning AMI (Ubuntu 18.04) Version 34.0 – ami-0302aadfa73a0d917を前提にしているので、AMIを変えてOSやanaconda環境が変わると修正が必要になります。