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
4 changes: 4 additions & 0 deletions samples/hasura/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM mcr.microsoft.com/devcontainers/typescript-node:22-bookworm

# Install hasura cli
RUN curl -L https://github.com/hasura/graphql-engine/raw/stable/cli/get.sh | bash
11 changes: 11 additions & 0 deletions samples/hasura/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"features": {
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
"ghcr.io/devcontainers/features/aws-cli:1": {}
}
}
48 changes: 35 additions & 13 deletions samples/hasura/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,51 @@ This sample project demonstrates how to deploy Hasura with Defang and connect it
## Prerequisites

1. Download [Defang CLI](https://github.com/DefangLabs/defang)
2. Have a managed database service configured and have the connection string ready.
3. (Optional) If you are using [Defang BYOC](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) authenticated with your AWS account
4. (Optional - for local development) [Docker CLI](https://docs.docker.com/engine/install/)
5. (Optional) [Install the Hasura CLI](https://hasura.io/docs/latest/hasura-cli/install-hasura-cli/) to create migrations and update metadata for your Hasura GraphQL api.
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/)
4. (Optional) [Install the Hasura CLI](https://hasura.io/docs/latest/hasura-cli/install-hasura-cli/) to create migrations and update metadata for your Hasura GraphQL api.

## Development

For development, we use a Postgres container. The Postgres container is defined in the `compose.dev.yaml` file. The Hasura container is defined in the `compose.yaml` file, with some overrides in the `compose.dev.yaml` file so it can correctly connect to the development database container.

To start the development environment, run `docker compose -f ./compose.yaml -f ./compose.dev.yaml up`. This will start the Postgres container and the Hasura container. The Hasura console will be available at `http://localhost:8080` with the password `password`.
To start the development environment, run `docker -f ./compose.dev.yaml up`. This will start the Postgres container and the Hasura container. The Hasura console will be available at `http://localhost:8080` with the password `password`.
**Note:** _If you want to make changes to your database, permissions, etc. you should use the Hasura console and the Hasura CLI to make those changes. See the next section for more information._

### Editing the database/permissions etc.

If you want to edit the database, permissions, or any other Hasura settings such that you can deploy them to production, you should [install the Hasura CLI](https://hasura.io/docs/latest/hasura-cli/install-hasura-cli/). Then, after starting the development environment, you can run `hasura console` _inside the `./hasura` directory_. This will open the Hasura console in your browser. Any changes you make in the console will be saved to the `migrations` and `metadata` directories. When you run `defang compose up` these changes will be applied to the production environment.

## Deploying
**NOTE**: If you are using the [Dev Container](https://containers.dev/) defined in [.devcontainer/devcontainer.json](.devcontainer/devcontainer.json), the Hasura CLI will already be installed.

## 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.

### `HASURA_GRAPHQL_ADMIN_SECRET`
This password will be used to allow you to access the hasura console.


### `POSTGRES_PASSWORD`
This password will be used to initialize the PostgreSQL database and to connect to it. You could use a command like `openssl rand -base64 16` to generate a random 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 `defang up`.

**Keep in mind that the playground does not support [managed Postgres](https://docs.defang.io/docs/concepts/managed-storage/managed-postgres).**

### 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 `defang --provider=aws up` in a terminal that has access to your AWS environment variables.

1. Open the terminal and type `defang login`
2. Add your connection string as a defang config value by typing `defang config set HASURA_GRAPHQL_DATABASE_URL` and pasting your connection string (which should be in the format `postgres://username:password@host:port/dbname`)
3. Setup a password for hasura by typing `defang config set HASURA_GRAPHQL_ADMIN_SECRET` and adding a password you would like to login with.
4. Type `defang compose up` in the CLI.
5. Your app will be running within a few minutes.

---

Expand Down
26 changes: 13 additions & 13 deletions samples/hasura/compose.dev.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
services:
postgres:
image: postgres:16
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
volumes:
- postgres_data:/var/lib/postgresql/data
hasura:
depends_on:
- postgres
restart: on-failure
extends:
file: compose.yaml
service: hasura
environment:
- HASURA_GRAPHQL_DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres
- HASURA_GRAPHQL_DATABASE_URL=postgres://postgres:password@database:5432/postgres
- HASURA_GRAPHQL_ADMIN_SECRET=password
volumes:
- ./hasura:/hasura
database:
extends:
file: compose.yaml
service: database
environment:
- POSTGRES_PASSWORD=password
volumes:
- database_data:/var/lib/postgresql/data

volumes:
postgres_data:
database_data:
18 changes: 13 additions & 5 deletions samples/hasura/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ services:
published: 8080
mode: ingress
environment:
- HASURA_GRAPHQL_DATABASE_URL
- HASURA_GRAPHQL_ADMIN_SECRET
- HASURA_GRAPHQL_DATABASE_URL=postgres://postgres:${POSTGRES_PASSWORD}@database:5432/postgres
- HASURA_GRAPHQL_ENABLE_CONSOLE=true
- HASURA_GRAPHQL_UNAUTHORIZED_ROLE=public
- HASURA_GRAPHQL_EXPERIMENTAL_FEATURES=naming_convention
- HASURA_GRAPHQL_DEFAULT_NAMING_CONVENTION=graphql-default
- HASURA_GRAPHQL_MIGRATIONS_DIR=/hasura/migrations
- HASURA_GRAPHQL_METADATA_DIR=/hasura/metadata
#deploy:
# resources:
# reservations:
# memory: 256M
- POSTGRES_PASSWORD

database:
image: postgres:16
x-defang-postgres: true
ports:
- target: 5432
mode: host
environment:
- POSTGRES_USER=postgres
- POSTGRES_DB=postgres
- POSTGRES_PASSWORD
Loading