Coder Social home page Coder Social logo

pamelafox / simple-fastapi-container Goto Github PK

View Code? Open in Web Editor NEW
14.0 2.0 8.0 200 KB

A simple FastAPI app that is containerized and designed for development on Azure Container Apps.

Dockerfile 0.11% Python 61.25% Bicep 38.64%
azd-templates azure azure-container-apps fastapi python

simple-fastapi-container's Introduction

This repository includes a simple Python FastAPI app with a single route that returns JSON. You can use this project as a starting point for your own APIs.

The repository is designed for use with Docker containers, both for local development and deployment, and includes infrastructure files for deployment to Azure Container Apps. ๐Ÿณ

The code istested with pytest, linted with ruff, and formatted with black. Code quality issues are all checked with both pre-commit and Github actions.

Opening the project

This project has Dev Container support, so it will be be setup automatically if you open it in Github Codespaces or in local VS Code with the Dev Containers extension.

If you're not using one of those options for opening the project, then you'll need to:

  1. Create a Python virtual environment and activate it.

  2. Install the requirements:

    python3 -m pip install -r requirements-dev.txt
  3. Install the pre-commit hooks:

    pre-commit install

Local development

  1. Run the local server:

    uvicorn src.api.main:app --port 3100 --reload
  2. Click 'http://127.0.0.1:3100' in the terminal, which should open a new tab in the browser.

  3. Try the API at '/generate_name' and try passing in a parameter at the end of the URL, like '/generate_name?starts_with=N'.

Local development with Docker

You can also run this app with Docker, thanks to the Dockerfile.

You need to either have Docker Desktop installed or have this open in Github Codespaces for these commands to work. โš ๏ธ If you're on an Apple M1/M2, you won't be able to run docker commands inside a Dev Container; either use Codespaces or do not open the Dev Container.

  1. Build the image:

    docker build --tag fastapi-app ./src
    
  2. Run the image:

    docker run --publish 3100:3100 fastapi-app
    

Deployment

This repo is set up for deployment on Azure Container Apps using the configuration files in the infra folder.

This diagram shows the architecture of the deployment:

Diagram of app architecture: Azure Container Apps environment, Azure Container App, Azure Container Registry, Container, and Key Vault

Steps for deployment:

  1. Sign up for a free Azure account and create an Azure Subscription.

  2. Install the Azure Developer CLI. (If you open this repository in Codespaces or with the VS Code Dev Containers extension, that part will be done for you.)

  3. Login to Azure:

    azd auth login
  4. Provision and deploy all the resources:

    azd up

    It will prompt you to provide an azd environment name (like "fastapi-app"), select a subscription from your Azure account, and select a location (like "eastus"). Then it will provision the resources in your account and deploy the latest code. If you get an error with deployment, changing the location can help, as there may be availability constraints for some of the resources.

  5. When azd has finished deploying, you'll see an endpoint URI in the command output. Visit that URI, and you should see the API output! ๐ŸŽ‰

  6. When you've made any changes to the app code, you can just run:

    azd deploy

Costs

Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage. The majority of the Azure resources used in this infrastructure are on usage-based pricing tiers. However, Azure Container Registry has a fixed cost per registry per day.

You can try the Azure pricing calculator for the resources:

  • Azure Container App: Consumption tier with 0.5 CPU, 1GiB memory/storage. Pricing is based on resource allocation, and each month allows for a certain amount of free usage. Pricing
  • Azure Container Registry: Basic tier. Pricing
  • Log analytics: Pay-as-you-go tier. Costs based on data ingested. Pricing

โš ๏ธ To avoid unnecessary costs, remember to take down your app if it's no longer in use, either by deleting the resource group in the Portal or running azd down.

Getting help

If you're working with this project and running into issues, please post in Discussions.

simple-fastapi-container's People

Contributors

dependabot[bot] avatar pamelafox avatar takekazuomi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

simple-fastapi-container's Issues

Container start fails

When using the container version, within Visual Studio code in MAC M2, the following error shows:

An error has occurred: FatalError: git failed. Is it installed, and are you in a Git repository directory? Check the log at /home/vscode/.cache/pre-commit/pre-commit.log [18551 ms] postCreateCommand failed with exit code 1. Skipping any further user-provided commands.

README issues

Describe the issue:
The port is incorrect.
Screenshot 2023-04-13 134624

Suggestion: Update to 3100.

Describe the issue:
failed to read dockerfile error occurs when execute the command below, for the Dockerfile is actually in the src folder.
Screenshot 2023-04-13 135849

Suggestion:
Add a step cd src, or update the command to docker build --tag fastapi-app .\src .

Describe the issue:
Miss the option --template .
Screenshot 2023-04-13 141525

Suggestion:
Please provide the --template option, update to azd init --template pamelafox/simple-fastapi-container

@pamelafox for notification.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.