This is my first attempt to open source something, please be kind and roast me softly (?) Any feedback is welcome welcome the repo of the technique used in the CFN and scripting.
I do a lot of CFN (though I’ve never used Stable Diffusion) so I’ll give it a quick review:
KeyName parameter
Vague variable name. I’d rename it to EC2KeyPairName or similar.
AmiId parameter
You can pull a lot of these from SSM parmeter store in a lot of cases. For this on in particular Canonical has instructions on how to retrieve the ID here:
https://discourse.ubuntu.com/t/finding-ubuntu-images-with-the-aws-ssm-parameter-store/15507
This will always give you the latest AMI. However DLAMI in particular doesn’t appear to be publishing to SSM Parameter Store so I guess you’re out of luck. I’d link the instructions on how to get the latest ID here instead: https://aws.amazon.com/releasenotes/aws-deep-learning-ami-catalog/
SecurityGroup resource
SSH open to the world isn’t great, though sometimes necessary. Is using SSM Session Manager a viable alternative for this use case?
Auto-delete Never
What’s that tag?
UserData
At a minimum put set -x at the top for bash debug logging. UserData launch failures tend to be a giant pain to troubleshoot otherwise. Setting that will give you a lot more ouptput in the EC2 System Log in case there’s a failure Also consider set -euo pipefail.
sudo -u ubuntu tmux new-session -d -s "stable-diffusion-session" watch "df -h --output=source,pcent | head -4; echo ""; cat progress.log; echo ""; echo ""; tmux list-sessions" \; split-window -h -p 75 nvtop\; split-window -v "git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git stable-diffusion-webui; bash $INSTALL_DIRECTORY/stable-diffusion-webui/webui.sh --listen --xformers --enable-insecure-extension-access --skip-torch-cuda-test; read" \;
Not a fan of having this as a one-liner, it’s hard to read. I’m not entirely following what you’re doing here. Why are you messing with tmux for setting up the listener? Why not just background it with &? If you want to improve things further you could create a proper systemd service file and use that. (Or is Ubuntu still using Upstart? Same idea)
To launch again
You can probably set this to execute on boot as part of the userdata script.
Wait for 500 seconds for webui to install before continuing to install tweaks
Is there not a more deterministic way to tell when it’s complete? A hard 500 second wait is a bit heavy handed and prone to issues if it takes longer than expected - and wasteful if it’s a lot faster.
EIP
The EIP could be made optional with some conditionals in the template. Think a boolean input parameter called “RegisterEIP” or similar. You could also do something like automatically creating an R53 DNS entry that points to the public IP of the instance. Which should remove the need for an EIP entirely if you’re willing to wait for DNS propagation times (set a low TTL).
Overall a pretty solid template. The stuff above is pretty much all nitpicky stuff that wouldn’t matter from a functional standpoint.