ComfyUI Running on AWS EC2 Linux (Stable Diffusion)

LearnWithMe
5 min readFeb 3, 2024

--

Installing ComfyUI on AWS EC2

Step by Step to run ComfyUI on AWS.

Important Notice: Avoid Unnecessary AWS EC2 Charges

Please be aware that AWS EC2 instances incur ongoing charges as long as they are running. To prevent unexpected expenses, ensure you stop your EC2 instance immediately after use. Regularly monitor your instances and manage them wisely to optimize costs.

Requirements

Supported EC2: P5 Series, P4dn Series

For other EC2: P3, P3dn, G3, G5, G4dn, Please use Proprietary Nvidia Driver DLAMI.

For P5 EC2, it is recommended to use CUDA≥11.8.

Problem issue with vCPU capacity limit

If you are beginner to AWS Service, you may face to this issue. Go to http://aws.amazon.com/contact-us/ec2-request to request an adjustment to this limit.

  • Select Region(Ohio or Virginia)
  • Choose the instance type you need (All G, P instances).
  • Choose Instance Quota Value(1 or custom requirement).
  • Use Case Description: Machine Learning Practice.
  • Contact Options: Email, Chat, Phone

Step 1: Create an EC2

AMIs Explanation

  1. Deep Learning OSS Nvidia Driver AMI GPU PyTorch 2.0.1 (Amazon Linux 2)
  • This AMI is configured with Nvidia GPU drivers, making it suitable for GPT-accelerated deep learning task.
  • PyTorch version 2.0.1, which is a popular deep learning frame work often used for research and development.

2. Deep Learning OSS Nvidia Driver AMI GPT TensorFlow 2.13 (Amazon Linux2)

  • Instead of PyTorch, it comes with TensorFlow version 2.13. TensorFlow is another widely used deep learning framework.

EC2 Configuration

  • Name: ComfyUI-Stable-Diffusion (Custom)
  • AMI: Deep Learning AMI GPU PyTorch 2.0.1 (Amazon Linux2) 202440123
  • Instance Type: g4dn.xlarge
  • Key pair
  • name: comfyui-with-manager (Own Name)
  • Key pair type: RSA, .pem
  • Network Setting
  • Security Group Name: ComfyUI-Stable-Diffusion
  • Auto-assign public IP: Enable.
  • Description: ComfyUI-Stable-Diffusion
  • Inbound security group rules: ssh connection by static IP or custom.
  • Create Subnet:
  • No preference (Default subnet in any availability zone) or specific your requirement.

Step 1.1: Permission Control & Connect to EC2

  • Choose the Instance And Click Connect.
  • Open Terminal in your local computer. Go to the directory.
chmod 400 comfyui-random-diffusion.pem
  • Let’s connect to EC2 Instance
ssh -i "Key_Name.pem" ec2-user@{Number 4 Above}

Enable pre-built pytorch and conda.

To activate pre-built pytorch and auto python environment. Run the commands on the Terminal.

source activate pytorch
conda config --set auto_activate_base true

Clone the ComfyUI Repository

Create or Open the directory where you would like to install the repo. We will create a folder named ai in the root directory.

Create a directory for ComfyUI

mkdir ai

Clone the ComfyUI Repo using Git.

go to the created directory.

cd ai

Clone the ComfyUI

git clone https://github.com/comfyanonymous/ComfyUI.git

Go to the directory

cd ComfyUI

Install Dependencies for ComfyUI

Running a script to install other requirements for ComfyUI.

pip install -r requirements.txt

and update pip if it is required

pip install --upgrade pip

ComfyUI Manager

ComfyUI-Manager offers management functions to install, remove, disable, and enable various custom nodes of ComfyUI.

Open the terminal and go to ComfyUI → custom_nodes Folder

  1. Run the command
git clone https://github.com/ltdrdata/ComfyUI-Manager.git

Running ComfyUI

# start ComfyUI
python3 main.py

Running ComfyUI Easily

create a new text file and name it runcomfy.sh in the home directory, Inside, typing the following

vim runcomfy.sh

Press I and copy and paste this, and ESC , Type :wq to save.

#!/bin/bash

# go to the Diectory
cd /ai/ComfyUI/

# activate the pytorch
source activate pytorch

# start comfyui
python3 main.py

Next we need to make the file executable with the command:

chmod +x runcomfy.sh

To run it, simple type it:

./runcomfy.sh

Access the Web Interface

1 — Recommended: Use SSH Tunneling for Private Access

Secure way to access the web interface is to use SSH tunneling. This method forwards the port from the EC2 instance to your local machine over a secure SSH connection. Run the following command on your local machine:

  1. Stop & Close the original terminal.
  2. Re-connect EC2 with command below.
ssh -L 8188:localhost:8188 -i /path/to/your-key.pem ec2-user@your-ec2-public-ip

example

ssh -L 8188:localhost:8188 -i /random/random2/comfy_key.pem ec2-user@x.1xx.251.xxx

3. If you are facing the connection error, use sudo reboot and reconnect the server with 2.

# source activate pytorch
python3 main.py

2 — Open 8188 port

you'll use your EC2 instance's public IP address or public DNS name to access the application. You can find this information in the EC2 Dashboard under the "Instances" section.
The URL you'll use in your browser will be something like http://your-ec2-public-ip:8188 or http://your-ec2-public-dns-name:8188.

  1. Go to Security Group.
  2. Edit in-bounce rule.
  3. Custom TCP, port 8188, Range: 0.0.0.0/0

--

--