Skip to content

Instantly share code, notes, and snippets.

@mlconnor
Last active December 14, 2023 02:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlconnor/d648e9c3a2b79e234764cc6079d84b9e to your computer and use it in GitHub Desktop.
Save mlconnor/d648e9c3a2b79e234764cc6079d84b9e to your computer and use it in GitHub Desktop.
Cloudformation Template for Stable Diffusion
# aws cloudformation create-stack --stack-name sd-webui-stack --template-body file://vpc-sf.yaml --parameters ParameterKey=KeyPair,ParameterValue=StableDiffusion
# ssh -i StableDiffusion.pem ubuntu@ip
# https://towardsdatascience.com/create-your-own-stable-diffusion-ui-on-aws-in-minutes-35480dfcde6a
# ssh -N -L 7860:127.0.0.1:7860 ubuntu@IP_ADDR
# cd /home/ubuntu/stable-diffusion-webui && nohup /home/ubuntu/stable-diffusion-webui/webui.sh --listen --theme dark --enable-insecure-extension-access --gradio-auth UNAME:PWD &
# systemctl - https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/6049
# sdxl - a1111 https://github.com/FurkanGozukara/Stable-Diffusion/blob/main/Tutorials/How-To-Use-Stable-Diffusion-SDXL-Locally-And-Also-In-Google-Colab.md
AWSTemplateFormatVersion: "2010-09-09"
Description: StableDiffusion server
Parameters:
KeyPair:
Description: Amazon EC2 Key Pair
Type: "AWS::EC2::KeyPair::KeyName"
Resources:
StableDiffusionVPC:
Type: "AWS::EC2::VPC"
Properties:
CidrBlock: "10.0.0.0/16"
StableDiffusionSubnet:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref StableDiffusionVPC
CidrBlock: "10.0.0.0/24"
StableDiffusionIGW:
Type: "AWS::EC2::InternetGateway"
StableDiffusionVPCGW:
Type: "AWS::EC2::VPCGatewayAttachment"
Properties:
VpcId: !Ref StableDiffusionVPC
InternetGatewayId: !Ref StableDiffusionIGW
StableDiffusionRouteTable:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref StableDiffusionVPC
DefaultRoute:
Type: "AWS::EC2::Route"
Properties:
RouteTableId: !Ref StableDiffusionRouteTable
DestinationCidrBlock: "0.0.0.0/0"
GatewayId: !Ref StableDiffusionIGW
StableDiffusionSubnetRouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref StableDiffusionSubnet
RouteTableId: !Ref StableDiffusionRouteTable
SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for SD WebUI EC2 instance
VpcId:
Ref: StableDiffusionVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 7860
ToPort: 7860
CidrIp: 0.0.0.0/0
StableDiffusionEC2Instance:
Type: AWS::EC2::Instance
Metadata:
Properties:
InstanceType: g4dn.xlarge
ImageId: ami-0d60b9becafb5eac6
KeyName: !Ref KeyPair
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
VolumeSize: 600
VolumeType: gp2
"Tags" : [
{"Key" : "Name", "Value" : "stable-diffusion-web-ui-cf"}
]
NetworkInterfaces:
- GroupSet:
- Ref: SecurityGroup
AssociatePublicIpAddress: 'true'
DeviceIndex: '0'
DeleteOnTermination: 'true'
SubnetId:
Ref: StableDiffusionSubnet
UserData:
'Fn::Base64': |
#!/bin/bash
cd /home/ubuntu
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
#bash stable-diffusion-webui/setup.sh -y
sudo systemctl start sdwebui
sudo systemctl enable sdwebui
Metadata:
"AWS::CloudFormation::Init":
config:
files:
"/etc/systemd/system/sdwebui.service":
content: |
Description=systemd service start stable-diffusion
[Service]
ExecStart=/bin/bash /home/ubuntu/stable-diffusion-webui/webui.sh --listen --theme dark --enable-insecure-extension-access --gradio-auth UNAME:PWD
User=ubuntu
WorkingDirectory=/home/ubuntu/stable-diffusion-webui
Restart=always
RestartSec=1
[Install]
WantedBy=multi-user.target
"/home/ubuntu/setup.sh"
content: |
WORKING_DIR="/home/ubuntu/stable-diffusion-webui"
sudo apt-get update
sudo apt install wget git python3 python3-venv build-essential net-tools awscli -y
# install CUDA (from https://developer.nvidia.com/cuda-downloads)
wget https://developer.download.nvidia.com/compute/cuda/12.0.0/local_installers/cuda_12.0.0_525.60.13_linux.run
sudo sh cuda_12.0.0_525.60.13_linux.run --silent
# install git-lfs
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs
sudo -u ubuntu git lfs install --skip-smudge
# download the SD model v2.1 and move it to the SD model directory
sudo -u ubuntu git clone --depth 1 https://huggingface.co/stabilityai/stable-diffusion-2-1-base
cd /home/ubuntu/stable-diffusion-2-1-base/
sudo -u ubuntu git lfs pull --include "v2-1_512-ema-pruned.ckpt"
sudo -u ubuntu git lfs install --force
sudo mv /home/ubuntu/stable-diffusion-2-1-base/v2-1_512-ema-pruned.ckpt $WORKING_DIR/models/Stable-diffusion/
# install control net
#git clone https://github.com/Mikubill/sd-webui-controlnet $WORKING_DIR/extensions/controlnet
#wget -P $WORKING_DIR/models/Stable-diffusion https://huggingface.co/lllyasviel/ControlNet/resolve/main/models/control_sd15_openpose.pth
#wget -P $WORKING_DIR/models/Stable-diffusion https://huggingface.co/lllyasviel/ControlNet/resolve/main/models/control_sd15_scribble.pth
#wget -P $WORKING_DIR/models/Stable-diffusion https://huggingface.co/lllyasviel/ControlNet/resolve/main/models/control_sd15_depth.pth
#wget -P $WORKING_DIR/models/Stable-diffusion https://huggingface.co/lllyasviel/ControlNet/resolve/main/models/control_sd15_canny.pth
#wget -P $WORKING_DIR/models/Stable-diffusion https://huggingface.co/lllyasviel/ControlNet/resolve/main/models/control_sd15_openpose.yaml
#wget -P $WORKING_DIR/models/Stable-diffusion https://huggingface.co/lllyasviel/ControlNet/resolve/main/models/control_sd15_scribble.yaml
#wget -P $WORKING_DIR/models/Stable-diffusion https://huggingface.co/lllyasviel/ControlNet/resolve/main/models/control_sd15_depth.yaml
#wget -P $WORKING_DIR/models/Stable-diffusion https://huggingface.co/lllyasviel/ControlNet/resolve/main/models/control_sd15_canny.yaml
#wget -P $WORKING_DIR/extensions/controlnet/models https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_canny.safetensors
#wget -P $WORKING_DIR/extensions/controlnet/models https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_depth.safetensors
#wget -P $WORKING_DIR/extensions/controlnet/models https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_hed.safetensors
#wget -P $WORKING_DIR/extensions/controlnet/models https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_openposev2.safetensors
#wget -P $WORKING_DIR/extensions/controlnet/models https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_scribble.safetensors
#wget -P $WORKING_DIR/extensions/controlnet/models https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_canny.yaml
#wget -P $WORKING_DIR/extensions/controlnet/models https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_depth.yaml
#wget -P $WORKING_DIR/extensions/controlnet/models https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_hed.yaml
#wget -P $WORKING_DIR/extensions/controlnet/models https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_openposev2.yaml
#wget -P $WORKING_DIR/extensions/controlnet/models https://huggingface.co/thibaud/controlnet-sd21/resolve/main/control_v11p_sd21_scribble.yaml
# download the corresponding config file and move it also to the model directory (make sure the name matches the model name)
wget https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/v2-inference.yaml
sudo cp v2-inference.yaml $WORKING_DIR//models/Stable-diffusion/v2-1_512-ema-pruned.yaml
# change ownership of the web UI so that a regular user can start the server
sudo chown -R ubuntu:ubuntu $WORKING_DIR/
StableDiffusionEIP:
Type: 'AWS::EC2::EIP'
Properties:
InstanceId: !Ref StableDiffusionEC2Instance
Outputs:
VPC:
Value: !Ref StableDiffusionVPC
Subnet:
Value: !Ref StableDiffusionSubnet
InternetGateway:
Value: !Ref StableDiffusionIGW
EC2Instance:
Value: !Ref StableDiffusionEC2Instance
ElasticIP:
Value: !Ref StableDiffusionEIP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment