AWSTemplateFormatVersion: '2010-09-09' Description: A CloudFormation template to deploy the Stable Diffusion Web UI by Automatic1111 Parameters: SubnetId: Description: The ID of the subnet where the EC2 instance will be launched. Type: AWS::EC2::Subnet::Id Ec2ImageId: Type: String Default: ami-08d56feea76b6f38b Description: Enter appropriate AMI ID in your region. Tested with "Deep Learning AMI GPU PyTorch 1.13.1 (Ubuntu 20.04) 20230510" in us-east-1. Ec2InstanceType: Type: String Default: g4dn.xlarge EC2InstanceProfileName: Type: String Description: Name of the existing IAM instance profile whic has access to S3 (arn:aws:iam:::instance-profile/) S3BucketName: Type: String Description: Name of the S3 bucket to mount SDWebUIVersion: Type: String Description: Branch of AUTOMATIC1111/stable-diffusion-webui Default: v1.3.2 KohyaSSVersion: Type: String Description: Branch of bmaltais/kohya_ss Default: v21.7.7 Resources: EC2Instance: Type: AWS::EC2::Instance Properties: InstanceType: !Ref Ec2InstanceType ImageId: !Ref Ec2ImageId IamInstanceProfile: !Ref EC2InstanceProfileName SubnetId: !Ref SubnetId BlockDeviceMappings: - DeviceName: /dev/sda1 Ebs: VolumeSize: 300 VolumeType: gp2 "Tags" : [ {"Key" : "Name", "Value" : "sd-web-ui-cf"}, ] UserData: 'Fn::Base64': !Sub | Content-Type: multipart/mixed; boundary="//" MIME-Version: 1.0 --// Content-Type: text/cloud-config; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="cloud-config.txt" #cloud-config cloud_final_modules: - [scripts-user, always] --// Content-Type: text/x-shellscript; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="userdata.txt" #!/bin/bash # Install packages sudo apt update sudo add-apt-repository ppa:deadsnakes/ppa -y sudo apt -y install wget git s3fs sudo apt -y install python3 python-is-python3 python3-pip python3-venv sudo apt -y install python3.10 python3.10-distutils python3.10-venv python3.10-tk curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10 python3.10 -m pip install --upgrade pip sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 # Mount S3 cd /home/ubuntu sudo -u ubuntu mkdir -p s3 sudo -u ubuntu s3fs -o iam_role=auto -o use_cache=/tmp -o uid=1000 -o gid=1000 ${S3BucketName} /home/ubuntu/s3 # Launch Kohya cd /home/ubuntu sudo -u ubuntu git clone https://github.com/bmaltais/kohya_ss.git -b ${KohyaSSVersion} cd kohya_ss # Needs to specify branch in setup. Feel free to change version here. ./setup.sh -b ${KohyaSSVersion} -v sudo -u ubuntu nohup bash -c './gui.sh --listen 127.0.0.1 --server_port 7861 --headless &> ../kohya-log.txt' & # Launch Stable Diffusion Web UI cd /home/ubuntu # setup script uses existing folder if it exists. Feel free to change version here. sudo -u ubuntu git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git -b ${SDWebUIVersion} sudo -u ubuntu nohup bash -c 'bash <(wget -qO- https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/webui.sh) --xformers &> sd-webui-log.txt' & # Launch File Browser cd /home/ubuntu sudo -u ubuntu curl -fsSL https://raw.githubusercontent.com/filebrowser/get/master/get.sh | bash sudo -u ubuntu nohup bash -c 'filebrowser -r /home/ubuntu &> ./browser-log.txt' & --// Outputs: InstanceID: Description: EC2Instance ID Value: !Ref EC2Instance