Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions samples/fastapi-postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,52 @@ This sample project demonstrates how to deploy FastAPI with PostgreSQL with Defa
1. Download <a href="https://github.com/defang-io/defang">Defang CLI</a>
2. (optional) If you are using <a href="https://docs.defang.io/docs/concepts/defang-byoc">Defang BYOC</a>, make sure you have properly <a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html">authenticated your AWS account</a>.

## Deploying
## Development

1. Open the terminal and type `defang login`
2. Type `defang compose up` in the CLI.
3. Your app will be running within a few minutes.
To run the development container(s) locally, do:

## Local Development
```bash
docker compose -f compose.dev.yaml up --build
```

1. Run `docker compose -f compose.dev.yaml up --build`
Or to run the production container(s) locally, do:

```bash
POSTGRES_PASSWORD=postgres docker compose up --build
```

## Configuration

For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):

> Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.

### `POSTGRES_PASSWORD`
``` bash
defang config set POSTGRES_PASSWORD
```

## Deployment

> [!NOTE]
> Download [Defang CLI](https://github.com/DefangLabs/defang)

### Defang Playground

Deploy your application to the Defang Playground by opening up your terminal and typing:
```bash
defang compose up
```

### BYOC (AWS)

If you want to deploy to your own cloud account, you can use Defang BYOC:

1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
2. Run in a terminal that has access to your AWS environment variables:
```bash
defang --provider=aws compose up
```

---

Expand Down
8 changes: 3 additions & 5 deletions samples/fastapi-postgres/compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ services:
extends:
file: compose.yaml
service: fastapi
restart: unless-stopped
ports:
- "8000:8000"
environment:
- DATABASE_URL=postgres://postgres:postgres@db:5432/postgres
volumes:
Expand All @@ -15,8 +12,9 @@ services:
command: fastapi dev main.py --host 0.0.0.0

db:
image: postgres:15
restart: unless-stopped
extends:
file: compose.yaml
service: db
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
Expand Down
16 changes: 15 additions & 1 deletion samples/fastapi-postgres/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@ services:
ports:
- mode: ingress
target: 8000
published: 8000
environment:
- DATABASE_URL
- DB_URL=postgres://postgres:${POSTGRES_PASSWORD}@db:5432/postgres
depends_on:
- db
#deploy:
# resources:
# reservations:
# memory: 256M

db:
image: postgres:15
restart: unless-stopped
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD
- POSTGRES_DB=postgres
ports:
- mode: host
target: 5432
4 changes: 2 additions & 2 deletions samples/fastapi-postgres/fastapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from databases import Database
from dotenv import load_dotenv

load_dotenv()
load_dotenv()

database_url = os.getenv("DATABASE_URL")
database_url = os.getenv("DB_URL")
print(f"Connecting to database at: {database_url}")
database = Database(database_url)

Expand Down
4 changes: 3 additions & 1 deletion samples/fastapi-postgres/fastapi/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
uwsgi
fastapi
fastapi[standard]
uvicorn
asyncpg
databases
python-dotenv
jinja2
python-multipart
45 changes: 35 additions & 10 deletions samples/fastapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,49 @@ This sample project demonstrates how to deploy FastAPI with Defang.

## Prerequisites

1. Download <a href="https://github.com/defang-io/defang">Defang CLI</a>
2. (optional) If you are using <a href="https://docs.defang.io/docs/concepts/defang-byoc">Defang BYOC</a>, make sure you have properly <a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html">authenticated your AWS account</a>.

## Deploying

1. Open the terminal and type `defang login`
2. Type `defang compose up` in the CLI.
3. Your app will be running within a few minutes.
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)

## Development

To run your FastAPI app locally, you'll need to have Docker installed on your machine. You can then run:
To run the application locally, you can use the following command:

```bash
docker compose -f compose.yaml -f compose.dev.yaml up --build
```

That will start your FastAPI app on `http://localhost:8000` with hot reloading enabled.
## Configuration

For this sample, you will not need to provide [configuration](https://docs.defang.io/docs/concepts/configuration).

If you wish to provide configuration, see below for an example of setting a configuration for a value named `API_KEY`.

```bash
defang config set API_KEY
```

## Deployment

> [!NOTE]
> Download [Defang CLI](https://github.com/DefangLabs/defang)

### Defang Playground

Deploy your application to the Defang Playground by opening up your terminal and typing:
```bash
defang compose up
```

### BYOC (AWS)

If you want to deploy to your own cloud account, you can use Defang BYOC:

1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
2. Run in a terminal that has access to your AWS environment variables:
```bash
defang --provider=aws compose up
```

---

Expand Down
Loading