aws-samples / deploy-streamlit-app

MIT No Attribution
43 stars 15 forks source link

deploy-streamlit-app

This app can be used as a starting point to easily create and deploy a GenAI demo, with web interface and user authentication. It is written in python only, with cdk template to deploy on AWS.

It deploys a basic Streamlit app, and contains the following components:

By default, the Streamlit app has the following features:

Architecture diagram

Architecture diagram

Usage

In the docker_app folder, you will find the streamlit app. You can run it locally or with docker.

Note: for the docker version to run, you will need to give appropriate permissions to the container for bedrock access. This is not implemented yet.

In the main folder, you will find a cdk template to deploy the app on ECS / ALB.

Prerequisites:

To deploy:

  1. Edit docker_app/config_file.py, choose a STACK_NAME and a CUSTOM_HEADER_VALUE.

  2. Install dependencies

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
  1. Deploy the cdk template
cdk bootstrap
cdk deploy

The deployment takes 5 to 10 minutes.

Make a note of the output, in which you will find the CloudFront distribution URL and the Cognito user pool id.

  1. Create a user in the Cognito UserPool that has been created. You can perform this action from your AWS Console.
  2. From your browser, connect to the CloudFront distribution url.
  3. Log in to the Streamlit app with the user you have created in Cognito.

Testing and developing in Cloud9

After deployment of the cdk template containing the Cognito user pool required for authentication, you can test the Streamlit app directly from Cloud9. You can either use docker, but this would require setting up a role with appropriate permissions, or run the Streamlit app directly in your terminal after having installed the required python dependencies.

To run the Streamlit app directly:

  1. If you have activated a virtual env for deploying the cdk template, deactivate it:
deactivate
  1. cd into the streamlit-docker directory, create a new virtual env, and install dependencies:
cd docker_app
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
  1. Launch the streamlit server
streamlit run app.py --server.port 8080
  1. Click on the Preview/Preview running application button in Cloud9, and click on the button to Pop out the browser in a new window, as the Cloud9 embedded browser does not keep session cookies, which prevents the authentication mechanism to work properly. If the new window does not display the app, you may need to configure your browser to accept cross-site tracking cookies.

  2. You can now modify the streamlit app to build your own demo!

Some limitations

Acknowledgments

This code is inspired from:

Security

See CONTRIBUTING for more information.

License

This application is licensed under the MIT-0 License. See the LICENSE file.