Published on

How to Deploy FastAPI on AWS App Runner in 15 Minutes

You can deploy a FastAPI application on AWS in under 15 minutes using AWS App Runner, which automates the setup of servers and security. This method allows you to go from local code to a live URL by simply connecting your GitHub repository or a container image. It is the most beginner-friendly path because it handles scaling and load balancing (distributing web traffic across multiple servers) automatically.

Why is AWS App Runner the best choice for beginners?

AWS App Runner is a fully managed service, which means AWS handles the "under the hood" server maintenance for you. You don't have to worry about configuring virtual machines or managing complex networking rules.

This service is specifically designed for web applications and APIs (Application Programming Interfaces). It connects directly to your code and creates a secure environment where your FastAPI app can run.

We've found that starting with App Runner prevents the "configuration fatigue" that often stops new developers from finishing their projects. It provides a clear path to production without requiring an advanced degree in cloud architecture.

What do you need before getting started?

Before you move your code to the cloud, you need a few basic tools ready on your computer. Make sure you have these specific versions or newer to ensure everything runs smoothly.

  • Python 3.12+: This is the programming language FastAPI is built on.
  • FastAPI 0.115+: The web framework you are using to build your API.
  • A GitHub Account: This is where you will store your code so AWS can find it.
  • An AWS Account: You will need an active account to access the App Runner service.

If you haven't installed FastAPI yet, you can do so by running pip install "fastapi[standard]" in your terminal. This command installs the core framework along with a production-ready web server called Uvicorn.

Step 1: How do you prepare your FastAPI code?

AWS needs to know exactly how to start your application and which "dependencies" (external libraries your code needs to run) to install. You must create two specific files in your project folder to make this work.

First, create a file named requirements.txt and list the libraries your app uses. At a minimum, it should look like this:

fastapi>=0.115.0
uvicorn>=0.30.0

Next, create your main application file, typically named main.py. This simple example creates a "Hello World" endpoint that you can test once the app is live.

from fastapi import FastAPI

# Create the main app instance
app = FastAPI()

# Define a simple route for the home page
@app.get("/")
def read_root():
    # Return a simple JSON message
    return {"Hello": "World"}

Step 2: How do you push your code to GitHub?

AWS App Runner works best when it can watch a GitHub repository for changes. You need to create a new repository on GitHub and upload your main.py and requirements.txt files there.

If you are new to Git, you can use the GitHub Desktop app or the web interface to upload these files. Ensure your repository is "Public" or that you give AWS permission to access your private repositories later in the process.

Once your files are uploaded, your repository structure should look like this:

  • main.py
  • requirements.txt

Step 3: How do you configure AWS App Runner?

Now that your code is on GitHub, log into your AWS Management Console and search for "App Runner" in the top search bar. Click "Create an App Runner service" to begin the setup wizard.

In the "Source" section, select "Source code repository" and click "Add new" to connect your GitHub account. Select your repository and the "main" branch from the dropdown menus.

In the "Deployment settings" section, choose "Automatic." This ensures that every time you update your code on GitHub, AWS will automatically rebuild and redeploy your application.

Step 4: How do you set the Build and Runtime settings?

This is where you tell AWS how to run Python. Choose "Python 3" as your runtime and enter the following commands in the configuration boxes provided.

For the "Build command," use: pip install -r requirements.txt This tells AWS to install all the libraries you listed earlier.

For the "Start command," use: uvicorn main:app --host 0.0.0.0 --port 8080 This command starts the Uvicorn server, tells it to listen on all network interfaces (0.0.0.0), and uses port 8080, which is the default for App Runner.

Step 5: How do you launch and test your API?

After entering your settings, click "Next," give your service a name like "my-fastapi-app," and click "Create & Deploy." AWS will now begin provisioning (setting up) the hardware and software for your app.

This process usually takes about 3 to 5 minutes. You can watch the "Logs" tab to see the progress of your deployment in real-time.

Once the status turns to a green "Running" checkmark, AWS will provide a "Default domain" URL. Click that link, and you should see {"Hello": "World"} appear in your browser window.

How do you update your application?

Updating your live website is as simple as changing the code on your computer. Because you selected "Automatic" deployment, AWS handles the rest for you.

Simply edit your main.py file, commit the changes, and push them to GitHub. App Runner will detect the new code and start a "Rolling Update."

A rolling update means AWS keeps your old version running until the new version is ready. This prevents your website from going offline while the new code is being installed.

What are the common deployment "gotchas"?

It is normal to run into a few hurdles during your first deployment. Most issues come from small configuration errors rather than your actual Python code.

The most common mistake is using the wrong port. FastAPI often defaults to port 8000 locally, but AWS App Runner expects port 8080 unless you manually change it in the settings.

Another frequent issue is forgetting to include a library in requirements.txt. If your code uses a library that isn't listed there, AWS will run into an "ImportError" and the deployment will fail.

Next Steps

Now that your API is live, you can start exploring more advanced features. You might want to connect a database like Amazon RDS (Relational Database Service) or add custom domain names to your URL.

Don't worry if your first few deployments fail; the logs will tell you exactly what went wrong. Building in the cloud is a skill that improves with every error message you solve.

For more detailed guides, visit the official FastAPI documentation.


Read the Deploy Documentation