Skip to content

Latest commit

 

History

History

stable-diffusion-webui-on-dlami

Creating Stabel Diffusion WebUI Server (AUTOMATIC1111) using Amazon Deep Learning AMI (DLAMI)

The cdk.json file tells the CDK Toolkit how to execute your app.

This project is set up like a standard Python project. The initialization process also creates a virtualenv within this project, stored under the .venv directory. To create the virtualenv it assumes that there is a python3 (or python for Windows) executable in your path with access to the venv package. If for any reason the automatic creation of the virtualenv fails, you can create the virtualenv manually.

To manually create a virtualenv on MacOS and Linux:

$ python3 -m venv .venv

After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.

$ source .venv/bin/activate

If you are a Windows platform, you would activate the virtualenv like this:

% .venv\Scripts\activate.bat

Once the virtualenv is activated, you can install the required dependencies.

(.venv) $ pip install -r requirements.txt

Before to synthesize the CloudFormation template for this code, you should update cdk.context.json file.

For example,

{
  "vpc_name": "default",
  "sd_webui_instance_type": "g4dn.2xlarge",
  "dlami_name": "Deep Learning Proprietary Nvidia Driver AMI GPU PyTorch 2.0.1 (Amazon Linux 2) 20240206",
  "ec2_key_pair_name": "your-ec2-key-pair-name(exclude .pem extension)"
}

Now you are ready to synthesize the CloudFormation template for this code.

(.venv) $ cdk synth --all

ℹ️ You can find out the latest Deep learning AMI by runing the following command:

  aws ec2 describe-images --region us-east-1 \
      --owners amazon \
      --filters 'Name=name,Values=Deep Learning Proprietary Nvidia Driver AMI GPU PyTorch 2.0.1 (Amazon Linux 2) ????????' 'Name=state,Values=available' \
      --query 'reverse(sort_by(Images, &CreationDate))[:1].Name'

Use cdk deploy command to create the stack shown above.

(.venv) $ cdk deploy --all

Or, we can provision each CDK stack one at a time like this:

(.venv) $ cdk list
SDWebUIonDLAMIVpcStack
SDWebUIonDLAMIStack

(.venv) $ cdk deploy SDWebUIonDLAMIVpcStack

(.venv) $ cdk deploy SDWebUIonDLAMIStack

To add additional dependencies, for example other CDK libraries, just add them to your setup.py file and rerun the pip install -r requirements.txt command.

Connect to the Stable Diffusion WebUI server

  1. Find out the Stable Diffusion WebUI URL by running the following command:
    $ aws cloudformation describe-stacks \
         --stack-name SDWebUIonDLAMIStack | \
         jq -r '.Stacks[0].Outputs | .[] | select(.OutputKey | endswith("StableDiffusionWebUIURL")) | .OutputValue'
    
  2. In the address bar of your browser, type the URL above, or click on the link. Stable_Diffusion_WebUI_Screenshot

Clean Up

Delete the CloudFormation stacks by running the below command.

(.venv) $ cdk destroy --force --all

Useful commands

  • cdk ls list all stacks in the app
  • cdk synth emits the synthesized CloudFormation template
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk docs open CDK documentation

Enjoy!

References