If you’re eager to venture into the innovative world of text-to-image generation, you’re in for a treat! In this guide, we will walk you through the process of setting up a production-ready Stable Diffusion service using BentoML and deploying it to AWS EC2. So, roll up your sleeves and let’s get started!
Prepare the Environment
The first step in our journey involves preparing the environment. If you would rather not start from scratch, you can download pre-built Bentos. Here’s how to clone the repository and set up your virtual environment:
git clone https://github.com/bentoml/stable-diffusion-bentoml.git
cd stable-diffusion-bentoml
python3 -m venv venv
. venv/bin/activate
pip install -U pip
pip install -r requirements.txt
And that’s it! Your environment is now ready!
Create the Stable Diffusion Bento
At this stage, you have two choices: either download pre-built Stable Diffusion Bentos or build your own from the Stable Diffusion models.
Download Pre-built Stable Diffusion Bentos
- For CPU or GPU with more than 10GB VRAM:
curl -O https://s3.us-west-2.amazonaws.com/bentoml.com/stable_diffusion_bentoml/sd_fp32.bento bentoml import ./sd_fp32.bento - For GPU with less than 10GB VRAM:
curl -O https://s3.us-west-2.amazonaws.com/bentoml.com/stable_diffusion_bentoml/sd_fp16.bento bentoml import ./sd_fp16.bento
Build from Stable Diffusion Models
You can choose one of the two models:
- FP32 model: Navigate to fp32 directory using
cd fp32 - FP16 model: Navigate to fp16 directory using
cd fp16
Download the Stable Diffusion Model
Depending on the model you choose, use the respective command:
# For FP32 model (if tar and gzip is available)
curl https://s3.us-west-2.amazonaws.com/bentoml.com/stable_diffusion_bentoml/sd_model_v1_4.tgz
tar zxf - -C models
# For FP32 model (if unzip is available)
curl -O https://s3.us-west-2.amazonaws.com/bentoml.com/stable_diffusion_bentoml/sd_model_v1_4.zip
unzip -d models sd_model_v1_4.zip
Repeat for the FP16 model by replacing the model URL. Now you’re ready to run and test the BentoML service!
Run and Test the BentoML Service
To bring up the service, simply use the following command:
BENTOML_CONFIG=configuration.yaml bentoml serve service:svc --production
Then you can run one of the scripts to test the service:
./txt2img_test.sh
./img2img_test.sh
Deploy the Stable Diffusion Bento to EC2
We will leverage bentoctl for deploying the bento to EC2. Start by installing the AWS EC2 operator:
bentoctl operator install aws-ec2
Configuring deployment is made seamless with the deployment_config.yaml file. It is pre-configured for deployment on a g4dn.xlarge instance with the Deep Learning AMI GPU PyTorch 1.12.0 in the us-west-1 region.
Generate the Terraform files with:
# In the bentoctl directory
bentoctl generate -f deployment_config.yaml
This will create necessary files like main.tf and bentoctl.tfvars to help with deployment. Build the Docker image and push it to AWS ECR:
bentoctl build -b stable_diffusion_fp32:latest -f deployment_config.yaml
Your image will now be pushed! You can run the experimental command to create specified resources:
bentoctl apply
Finally, see your Stable Diffusion service live through the endpoint URL displayed after applying the configuration. You can run test prompts to ensure everything is working smoothly.
Troubleshooting Tips
Should you encounter any bumps along the road, here’s what to consider:
- Check your AWS credentials to ensure they are correctly configured.
- Ensure that the appropriate permissions and security group rules are set on AWS EC2 instances.
- Watch for any issues with dependencies in your virtual environment.
- Verify that you are in the correct directory when running commands.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
By following these steps, you’ll have a fully functioning Stable Diffusion service running in the cloud! Explore the vast possibilities of creating stunning visuals from your imaginative prompts.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

