AWS Public Sector Blog

Generating and editing AI images using Amazon Lightsail for Research

AWS branded background design with text overlay that says "Generating and editing AI images using Amazon Lightsail for Research"

Researchers and students face the problem of trying to perform analyses and run simulations from their laptops, which often are not up to the task. They need the compute power of the cloud, but access can be a challenge, and they may not have the expertise necessary to use larger, more complex computing environments. Amazon Lightsail for Research is an Amazon Web Services (AWS) service that allows individuals to quickly access cloud resources without having specialized knowledge of the cloud or an enterprise-size budget.

Lightsail for Research provides built-in cost control rules and usage tracking to help customers maximize their budget. Long-running jobs can be performed without the worry of any additional cost due to idle resources or tying up local hardware needed for other tasks.

In this post, we show how Lightsail for Research can help you successfully deploy an artificial intelligence (AI) image generation model using open source software. We walk through deploying a Lightsail for Research GPU instance using a simplified console experience. We cover how to set up cost control rules in order to prevent or manage unnecessary costs. We show how you can open network ports on your Lightsail for Research instance to allow others to access the software you are running.

Solution overview: Deploy an AI image generation model with Amazon Lightsail for Research

For this solution, we install the open source software Fooocus, which you can find in the Illyasviel GitHub repo, to run customized Stable Diffusion models and checkpoints. Fooocus makes it straightforward for you to use the GPU hardware on your cloud instance virtual computer. It comes with many built-in presets so that you can concentrate on your image prompt rather than adjusting technical parameters. It also provides a seamless web UI to work in.

To deploy an AI image generation model with Amazon Lightsail for Research, follow these high-level steps:

  1. Create your Lightsail for Research virtual computer.
  2. Create a cost control rule.
  3. Connect to your virtual computer and install Fooocus.
  4. Allow remote access to your software (optional).
  5. Add authentication to prevent unauthorized use (optional).

Prerequisites

To implement this solution, you should have the following prerequisites:

  1. An AWS account.
  2. Access to the Lightsail for Research service.
  3. Access to Lightsail for Research GPU instances. A service quota request is required. To request a service quota, open a support ticket in the Support Center.

Solution walkthrough: Deploy an AI image generation model with Amazon Lightsail for Research

To deploy an AI image generation model with Amazon Lightsail for Research, follow the steps in the next five sections.

Create your Lightsail for Research virtual computer

To get started with using Amazon Lightsail for Research for AI image generation, first you need to launch your instance.

  1. Sign in to the Lightsail for Research console.
  2. Choose Create virtual computer.
  3. From the dropdown menu, select the Region in which you’d like to deploy your virtual computer.
  4. In the Application section, select Choose next to Ubuntu. Fooocus will be installed on top of the base Ubuntu OS.
  5. In the Name your virtual computer field, enter a name for your virtual computer.
  6. Select a plan, for example, GPU 4XL.
  7. Choose Create virtual computer.

The following screenshot shows four virtual computers created in Lightsail for Research, with one in a running state.

Figure 1. Screenshot of Virtual computers screen with four sample virtual computers created and one in a running state.

Create a cost control rule

To ensure that your virtual computer automatically shuts down when not in use, create a cost control rule. This is a powerful feature that helps you avoid the unnecessary cost of idle resources.

  1. In the Lightsail for Research console, under Virtual computers, select the name of your virtual computer.
  2. At the bottom of your virtual computer page, choose Create cost control rule.
  3. The default rule will stop your virtual computer when CPU utilization is below 5 percent for a period of 10 minutes. Proceed with creating the rule by clicking the Create rule button.

The virtual computer now monitors CPU usage and automatically shuts down when not in use, as shown in the following screenshot.

Figure 2. Screenshot of a sample cost control rule that automatically shuts down the virtual computer if CPU usage is under 10 percent for 10 minutes.

Connect to your virtual computer and install Fooocus

Now you need to connect to your instance and then install Fooocus in order to be able to generate AI images.

  1. In the Lightsail for Research console, next to the virtual computer you created and named, choose Launch Ubuntu.
  2. After a few moments, your session will be ready, and a new tab should open, displaying your virtual computer’s Upon the first launch, Ubuntu displays an initial setup screen asking for keyboard and location information. Select the appropriate options and then choose Start using Ubuntu.
  3. Open a terminal by pressing Ctrl+Alt+T within your session. Alternatively, you can select Activities from the top left and enter “Terminal” in the search box.
  4. Python should already be at the required version of 3.10, but you can verify this by running the following command.
    python3 –version
  5. Install python3.10-venv and python3-pip by running the following command.
    sudo apt install python3-pip python3.10-venv
  6. Clone the Fooocus repository and install the necessary Python requirements by running the following command.
    git clone https://github.com/lllyasviel/Fooocus.git
    cd Fooocus
    python3 -m venv fooocus_env
    source fooocus_env/bin/activate
    pip install -r requirements_versions.txt
  7. Now that you have the Fooocus software downloaded and installed onto your virtual computer, you can run it with the following commands.
    source fooocus_env/bin/activate
    python entry_with_update.py –listen

The following image shows the Fooocus UI with a prompt in the prompt field and the generated image.

Figure 3. Screenshot of the Fooocus web UI with a sample prompt and generated images.

Fooocus is now running locally on your virtual computer within Lightsail for Research. Any work you perform is stored locally, and your prompts or images are not used to train commercial models.

Allow remote web access to your software (optional)

  1. Sign in to the AWS Management Console and select CloudShell to open AWS CloudShell. Alternatively, you can go to the CloudShell console.
  2. To allow remote access to the Fooocus web UI, run the following AWS CLI command.
    aws lightsail open-instance-public-ports --port-info 
    fromPort=7865,toPort=7865,protocol=TCP,cidrs="<YOUR PUBLIC
     IP/32>" --region=<REGION OF VIRTUAL COMPUTER> --instance-name
     "<VIRTUAL COMPUTER NAME>"

Your virtual computer is now allowing remote connectivity to the port Fooocus is running on (port 7865). It will only allow access from the public IP address that you have provided. You can now use a browser to connect to the Fooocus web UI without having to sign in to Lightsail for Research and open a local console. For production environments, you should consider private network connectivity.

To find your virtual computer’s current IP address so you can connect, follow these steps.

  1. In the Lightsail for Research console, under Virtual Computers, select the name of your virtual computer.
  2. The current IP address of your Virtual Computer will be listed under the Public IP Copy this into your web browser and add the 7865 port to specify where Fooocus is running.

Example: http://12.34.56.789:7865

Make sure that your web browser does not convert your URL to HTTPS because Fooocus does not use this by default.

Add authentication to prevent unauthorized use (optional)

Fooocus provides a built-in authentication layer that you can use to help prevent unauthorized use of your virtual computer’s resources. To enable this authentication, perform the following steps.

  1. In the Lightsail for Research console, select Launch Ubuntu under your virtual computer to open a console.
  2. Open a terminal by pressing Ctrl+Alt+T within your session. Alternatively, you can select Activities from the top left and enter “Terminal” in the search box.
  3. Assuming you cloned the Fooocus software into the default lightsail-user home directory, go into the Fooocus directory and make a copy of the example authentication config file by running the following command.
    cd Fooocus 
    cp auth-example.json auth.json
  4. Use your preferred Linux text editor to update the example user and password located within the auth.json file is updated, users will need to provide the username and password you have configured.

Clean up

In this post, we created a virtual computer in the Lightsail for Research service. To avoid any additional costs when you’re done, delete the virtual computer that you created for this solution. This can be done by selecting your Virtual computer from the Lightsail for Research console page, and then choosing Delete virtual computer from the Actions menu.

Conclusion

In this post, we walked through setting up a Lightsail for Research virtual computer, installed open source software that used the GPU hardware, and opened remote access to the software running on our virtual computer. We created a cost control rule to avoid incurring unneeded costs due to idle resources. We used the Fooocus AI image generation software in this post, but this could be done with any software that requires the use of GPU hardware.

Additional resources: