---
title: "Hosting an Ngrok Remote Web App on AWS"
description: "Learn how to run the Ngrok remote web application on AWS with the FastAPI Python endpoint in Linux through this ATA Learning tutorial!"
canonical: "https://adamtheautomator.com/ngrok-remote/"
---

# Hosting an Ngrok Remote Web App on AWS

> Learn how to run the Ngrok remote web application on AWS with the FastAPI Python endpoint in Linux through this ATA Learning tutorial!

Source: https://adamtheautomator.com/ngrok-remote/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Hosting an Ngrok Remote Web App on AWS](https://adamtheautomator.com/wp-content/uploads/2023/11/ngrok-remote.jpg)

# Hosting an Ngrok Remote Web App on AWS

[![](https://secure.gravatar.com/avatar/a3e6f24b6b657f0acd2615f1802513a18663dea2e04d24e1df9c1530290e9b48?s=192&d=mm&r=g)Uzma Younas](https://adamtheautomator.com/author/uzma-younas/)17 November 20237 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[AWS](/tag/aws/)[Ngrok](/tag/ngrok/)

Table of Contents

*   [Table of Contents](#table-of-contents)
*   [Prerequisites](#prerequisites)
*   [Creating and Deploying an ngrok Remote Web App](#creating-and-deploying-an-ngrok-remote-web-app)
*   [Installing and Authenticating ngrok](#installing-and-authenticating-ngrok)
*   [Exposing the FastAPI Endpoint with ngrok](#exposing-the-fast-api-endpoint-with-ngrok)
*   [Conclusion](#conclusion)

As web development continues to advance rapidly, having a reliable and flexible hosting platform is key for any successful web application. With cloud services like Amazon [AWS](https://aws.amazon.com/), Google [GCP](https://cloud.google.com/) or Microsoft [Azure](https://azure.microsoft.com/en-us), you can quickly spin up virtual servers to host your apps and access them from anywhere.

One incredibly useful service for testing and sharing web apps hosted on remote servers is Ngrok. Ngrok creates a secure tunnel from the public internet to your local machine or server. This allows you to expose a web server running on your local machine or on a remote server to the internet.

In this article, you will walk through the steps for hosting a basic web app on a remote AWS EC2 instance and making it accessible over the internet with Ngrok and SSH.

## Table of Contents

*   [Prerequisites](#prerequisites)
*   [Creating and Deploying an ngrok Remote Web App](#creating-and-deploying-an-ngrok-remote-web-app)
*   [Installing and Authenticating ngrok](#installing-and-authenticating-ngrok)
*   [Exposing the FastAPI Endpoint with ngrok](#exposing-the-fast-api-endpoint-with-ngrok)
*   [Conclusion](#conclusion)

## Prerequisites

Prior to hosting your ngrok remote web app on AWS, ensure that you have the following in place:

*   A local machine that supports AWS operations – This tutorial uses an Ubuntu 22.04.2 LTS.

Related:[Install Ubuntu Server 20.04: A Step-by-Step Walkthrough](https://adamtheautomator.com/install-ubuntu/)

*   An [AWS](https://aws.amazon.com/) account with EC2 access configured on your local machine.
*   An [EC2 Linux instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html) running with [SSH](https://www.clickittech.com/aws/connect-ec2-instance-using-ssh/) access setup – Amazon Linux 2 or Ubuntu both work well.
*   [Python](https://www.python.org/downloads/) and [FastAPI](https://pypi.org/project/fastapi/) installed on your EC2 Linux instance.

Related: [Python 3.6 Installation Guide for Windows, macOS, and Linux](https://adamtheautomator.com/install-python-36/)

*   An [ngrok account](https://dashboard.ngrok.com/signup) with an authentication token.

## Creating and Deploying an ngrok Remote Web App

Before lunging into hosting a web app on AWS, you first need to have one. You’ll create a FastAPI endpoint, deploy it to an AWS virtual machine (VM), and expose the endpoint to the internet using ngrok.

To create and deploy a ngrok remote web app, follow these steps:

1\. Create an _app.py_ file with your preferred editor and populate the following code.

Related: [Essential Tips for Installing & Using Sublime Text on Ubuntu](https://adamtheautomator.com/sublime-text-on-ubuntu/)

This code enforces the FastAPI framework to create a web application that returns the current time date and time.

```python
from fastapi import FastAPI
from datetime import datetime
app = FastAPI()

@app.get("/")
def  root():
   return  {"time": datetime.now()}
```

2\. Next, create another file called _requirements.txt_ that lists the Python packages needed as follows, which helps ensure consistency between dev and production environments.

Having a _requirements.txt file_ is a best practice that helps automate dependency installation rather than manual `pip` commands.

```
fastapi
uvicorn
```

3\. Open a terminal and execute the command below to silently (no output) `zip` the files (`app.py` and `requirements.txt`) inside the _`FastApi.zip`_ file.

Note that if the [files to zip](https://adamtheautomator.com/unzip-files-in-linux/) are not located in the working directory, you must specify their full paths instead.

```bash
zip FastApi.zip app.py requirements.txt
```

Related:[Learn How to Zip and Unzip Files in Linux and be a Zip Master](https://adamtheautomator.com/unzip-files-in-linux/)

4\. Once zipped, run the below `scp` command to copy the ZIP file (`*FastApi.zip*`) to your AWS EC2 instance, where you can deploy and run your ngrok remote web app.

Ensure you replace the following placeholders before running the command:

*   `<your-key.pem>` – The key file associated with the SSH key pair (i.e., `*myKey.pm*`) used to connect to your AWS EC2 instance.
*   `EC2-user` – The username of your EC2 instance (i.e., `ubuntu`).
*   `EC2-IP` – The public IP address (or hostname) of your EC2 instance.
*   `<remote-dir>` – The destination path on your AWS EC2 instance (i.e., `*/home/ubuntu*`) to copy the ZIP file.

```bash
scp -i <your-key.pem> FastApi.zip EC2-user@EC2-IP:<remote-dir>
```

![Copying the ZIP file to the AWS EC2 instance ](https://adamtheautomator.com/wp-content/uploads/2023/11/image-95.png)

Copying the ZIP file to the AWS EC2 instance

Related:[Securely Copy Files With the SCP Command](https://adamtheautomator.com/scp-command/)

5\. After copying, run each command to SSH into your AWS EC2 instance and start the [Uvicorn](https://www.uvicorn.org/) [Asynchronous Server Gateway Interface (ASGI)](https://asgi.readthedocs.io/en/latest/) server.

Like in step four, replace the placeholders below accordingly.

```bash
# SSH into the AWS EC2 instance
ssh -i <your-key.pem> EC2-user@EC2-IP
# Change the directory to where you copied the FastApi.zip file (i.e., /home/ubuntu)
cd <remote-dir>
# Start the Uvicorn ASGI server.
uvicorn app:app --host 0.0.0.0 --port 8000
```

![Connecting to the AWS EC instance via SSH and starting the Uvicorn ASGI server](https://adamtheautomator.com/wp-content/uploads/2023/11/image-94.png)

Connecting to the AWS EC instance via SSH and starting the Uvicorn ASGI server

![Starting Uvicorn Server ](https://adamtheautomator.com/wp-content/uploads/2023/11/image-93.png)

Starting Uvicorn Server

> _💡 By default Uvicorn does not start as a service, you will need to [run a process manager](https://www.uvicorn.org/deployment/) if you want to run Uvicorn as a service._

6\. Next, execute the following commands to `unzip` the `FastApi.zip` file and `install` the necessary packages listed in the _`requirements.txt`_ file.

```bash
# Extract the files in the FastApi.zip file
unzip FastApi.zip
# Change the working directory to FastApi
cd FastApi
# Install the necessary packages listed in the requirements.txt file via pip
pip install -r requirements.txt
```

![Extracting files and installing necessary packages](https://adamtheautomator.com/wp-content/uploads/2023/11/image-92.png)

Extracting files and installing necessary packages

7\. Afterward, invoke the following `uvicorn` command to run the code in the _app.py_ file while also enabling automatic reloading of the Uvicorn ASGI server.

```bash
uvicorn app:app --reload
```

Copy the highlighted URL, as shown below, as you will use it to access the FastAPI in the following step.

![Running the code in app.py file using uvicorn](https://adamtheautomator.com/wp-content/uploads/2023/11/image-91.png)

Running the code in _app.py_ file using uvicorn

8\. Open your preferred web browser on your local machine, and visit the URL you copied in step seven to access the FastAPI endpoint.

You’ll see the displayed interface below (127.0.01 refused to connect) since you’re accessing the Uvicorn server on your local machine, not on your AWS EC2 instance. But no worries! Jump to the following section to fix this connection error.

![Attempting to access the API endpoint on the local machine](https://adamtheautomator.com/wp-content/uploads/2023/11/image-90.png)

Attempting to access the API endpoint on the local machine

## Installing and Authenticating ngrok

Accessing the API directly on the EC2 instance fails without the proper networking and security config. The EC2 instance only has a private IP address by default, making the instance unreachable from the public internet.

But worry not! In such cases, ngrok can seamlessly showcase and test your Uvicorn server over the internet.

To expose your FastAPI over the internet, proceed with the following:

1\. Execute each command below to download (`curl`) and add the ngrok GPG key to the trusted keyring, and add the ngrok repository information to the system’s sources list.

This process allows you to install and update ngrok via the system’s package manager (i.e., **`apt`** on Debian-based systems).

```bash
# Download the ngrok GPG public key
curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc > /dev/null
# Echoes the ngrok repo information to the terminal
echo "deb https://ngrok-agent.s3.amazonaws.com buster main" | sudo tee /etc/apt/sources.list.d/ngrok.list
# Update the system's package lists and install ngrok
sudo apt update && sudo apt install ngrok
```

![](https://adamtheautomator.com/wp-content/uploads/2023/11/image-98.png)

Related:[How to Setup Ngrok for Local Application Development](https://adamtheautomator.com/ngrok/)

But why choose ngrok, though? ngrok provides an encrypted tunnel and automated port forwarding to avoid these problems.

You could assign a public IP, but that poses security risks by exposing the server. The default security group only allows SSH connections, blocking your HTTP API traffic.

Opening the firewall to HTTP access is also insecure. Even if HTTP was allowed, you’d have to handle complex port mapping between the internal and external ports.

2\. Next, run the following command to authenticate (`authtoken`) ngrok with your account. Ensure you replace `AUTH_TOKEN` with your actual [authentication token](https://dashboard.ngrok.com/get-started/your-authtoken).

```bash
ngrok authtoken AUTH_TOKEN
```

![Authenticate the ngrok agent](https://adamtheautomator.com/wp-content/uploads/2023/11/image-97.png)

Authenticate the ngrok agent

3\. Once authenticated, execute the below `ngrok` command to view ngrok’s `status` and verify the authentication is successful.

```bash
ngrok status
```

If all goes well, you’ll see your account information in the output, indicating that ngrok is ready for use.

![ Checking ngrok’s status](https://adamtheautomator.com/wp-content/uploads/2023/11/image-96.png)

Checking ngrok’s status

## Exposing the FastAPI Endpoint with ngrok

Whether you are using ngrok or any other service requiring uninterrupted access to a remote machine, launching that service within a dedicated screen is a best practice. Doing so keeps your process running smoothly even with a disrupted local connectivity.

But before starting the ngrok tunnel, you’ll first create two screen sessions to persist the process in the background. Running ngrok and uvicorn on their own screens gives more flexibility.

To expose your FastAPI endpoint with ngrok, complete the steps below:

1\. Execute the following command, which has no direct output, but starts a new `screen` session named `uvicorn` for running the FastAPI application.

```bash
screen -S uvicorn
```

2\. Next, invoke the below `uvicorn` command to run the FastAPI app once you’re in the session.

```bash
uvicorn app:app --host 0.0.0.0 --port 8000
```

![Running the FastAPI app](https://adamtheautomator.com/wp-content/uploads/2023/11/image-103.png)

Running the FastAPI app

3\. Now, press Ctrl+A then D to detach from the session, and the server will keep running.

At this point, you already have a separate uvicorn screen running the FastAPI server.

![Disconnecting from the current screen session](https://adamtheautomator.com/wp-content/uploads/2023/11/image-102.png)

Disconnecting from the current screen session

4\. Consequently, run the following command, which has not output, but creates a new `screen` called `ngrok`.

```bash
screen -S ngrok
```

5\. Once created, execute the below `ngrok` command inside the ngrok screen session to create a secure public tunnel to your web app running on port `8000`.

```bash
ngrok http 8000
```

If successful, a screen called ngrok starts running on your tunnel, and ngrok generates a public URL that points to your FastAPI endpoint.

Copy Forwarding URL as you will need it to test your endpoint’s public accessibility in the following step.

![Public URL next to web Interface is the api endpoint visible over the internet](https://adamtheautomator.com/wp-content/uploads/2023/11/image-100.png)

Public URL next to web Interface is the api endpoint visible over the internet

6\. Navigate to the URL you noted in step five on a new browser tab.

You’ll see a page similar to the one below, and ngrok will now run within this screen.

![Endpoint accessible publically](https://adamtheautomator.com/wp-content/uploads/2023/11/image-101.png)

Endpoint accessible publically

7\. Switch back to your terminal, and detach from the session (Ctrl+A then D), and the tunnel will keep running.

8\. Finally, execute the following command to reattach to a screen session of your choice, like uvicorn.

Reattaching to a screen session allows you to monitor the logs and traffic, so ensure you replace `screen_name` with the actual screen session’s name.

```bash
screen -r screen_name
```

By using screen, you ensure that ngrok and uvicorn remain active even after you close the terminal, providing persistent access to your FastAPI endpoint.

![Reattaching to a screen session to monitor logs](https://adamtheautomator.com/wp-content/uploads/2023/11/image-99.png)

Reattaching to a screen session to monitor logs

## Conclusion

Congratulations! You’ve successfully created a basic FastAPI endpoint, deployed it to an AWS VM, and exposed it to the internet using Ngrok. This sets up a solid foundation for rapidly developing and deploying web backends. For those looking to streamline development workflows, consider diving into continuous integration and deployment (CI/CD). Set up pipelines using tools like Jenkins or AWS CodePipeline to automate testing and deployment processes.

You can achieve this by pushing code in GitHub and configuring GitHub Actions workflows to run on each push. The workflows would first validate the code via linting and tests before deploying to AWS. Ngrok running on the server would then expose the latest revision.

Related:[How to Create a Jenkins CI CD Pipeline](https://adamtheautomator.com/jenkins-ci-cd/)

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fngrok-remote%2F&text=Hosting%20an%20Ngrok%20Remote%20Web%20App%20on%20AWS)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fngrok-remote%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fngrok-remote%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/05/HMaster-Your-Data-with-AWS-Quicksight.jpg)

### [Make Data-Driven Decisions with AWS QuickSight Analytics](/aws-quicksight/)

Struggling to make sense of your data? Discover how AWS QuickSight transforms data into actionable insights for smarter decision-making.

![](https://adamtheautomator.com/wp-content/uploads/2023/11/aws-network-access-analyzer-2.jpg)

### [Getting Started with AWS Network Access Analyzer](/aws-network-access-analyzer/)

Unlock the power of AWS Network Access Analyzer. Master network security with step-by-step guidance in this tutorial today!

![](https://adamtheautomator.com/wp-content/uploads/2023/08/aws-codepipeline.jpg)

### [Deploying a Static Website on AWS S3 with AWS CodePipeline](/aws-codepipeline/)

Learn to leverage the power of AWS CodePipeline to deploy static websites with AWS S3 in this in-depth ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
