Clone a Private GitHub Repository on AWS EC2 & Run a Streamlit App
A step-by-step guide to securely authenticating your EC2 instance with GitHub using Deploy Keys and deploying a Python Streamlit application.
Create a Deploy Key on EC2
First, SSH into your EC2 instance. We need to generate a secure SSH key pair that will identify this specific server to GitHub.
ssh-keygen -t ed25519 -a 100 -C "your-email@example.com"When prompted for a file path, press Enter to accept the default. When prompted for a passphrase, you can leave it empty (press Enter twice) for automated scripts, or add one for extra security.
Next, display the public key so you can copy it:
cat ~/.ssh/id_ed25519.pubOutput Example:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your-email@example.com
Add the Deploy Key to GitHub
- Navigate to your repository on GitHub.
- Go to Settings > Deploy keys (in the sidebar).
- Click Add deploy key.
- Provide a Title (e.g., "AWS EC2 Instance").
- Paste the public key you copied in Step 1 into the Key field.
- (Optional) Check "Allow write access" only if your app needs to push code back to the repo.
- Click Add key.
Test Connection & Install Git
Back on your EC2 terminal, verify that GitHub accepts your new key:
ssh -T git@github.comType yes if prompted to verify the host fingerprint. You should see a success message like:
Hi username! You've successfully authenticated...
Now, ensure your package manager is updated and Git is installed:
sudo yum update -y
sudo yum install git -yClone the Repository & Run
Use the SSH URL (not HTTPS) to clone your repo. This allows Git to use the SSH key we just set up.
# Replace with your actual repo SSH URL
git clone git@github.com:OrgName/your-repo.git
cd your-repoInstall your Python dependencies:
pip install -r requirements.txtFinally, launch your Streamlit application:
streamlit run streamlit_app.pyTo Keep it Running
Running streamlit run directly will stop the app if you close your SSH terminal. To keep it running in the background, use tmux or nohup:
Need help?
If you encounter any issues with permission denied errors, ensure your key is added to the ssh-agent using ssh-add ~/.ssh/id_ed25519.
Feel free to reach out: its.royniloy@gmail.com