hasura-aws-stack
Stack
- Hasura on ECS Fargate (auto-scale)
- Aurora Postgres or RDS Postgres
- Lambdas for remote schemas and event triggers
- Docker for local dev
- CircleCI for CI/CD
Setup Hasura on Fargate
- Go to ECS in AWS Console.
- Create a ECS cluster on Fargate with required specs.
- Create a service called
hasura
in your cluster. - Create a Task Definition with Hasura GraphQL Engine container and environment variables.
- Add the Task to your hasura service.
If you are want to use multiple-instances/auto-scale, you will need to choose an ALB as the load balancer.
Local Dev
- Git clone this repo:
$ git clone [email protected]:hasura/hasura-aws-stack.git
$ cd hasura-aws-stack
$ # You are now in the project directory
- Run Hasura using docker-compose ref :
In project directory:
$ cd local
$ docker-compose up -d
- Start local development API server ( first make sure all dependencies are met for local API server):
In project directory:
$ # install dependencies for local API server for e.g. all remote schemas and event triggers
$ # cd remote-schemas/account-schema
$ # npm i
$ cd local
$ node localDevelopment.js
- Apply migrations locally
In project directory;
$ cd hasura
$ hasura migrate apply
- Start the console
In project directory:
$ cd hasura
$ hasura console
Local Dev - Event Triggers
- Create a new folder in
event-triggers
folder:
In project directory:
$ cd event-triggers
$ mkdir echo
-
Write your function in
echo/index.js
. Make sure you export one function. Ref: echo -
Add corresponding endpoint in local development API server. Ref: localDevelopment
-
Start the local development API server:
In project directory:
$ cd local
$ node localDevelopment.js
-
Add event trigger URL as environment variable in
local/event-triggers.env
. Ref: event-triggers.env -
Restart Hasura (for refreshing environment variables):
In project directory:
$ cd local
$ docker-compose down
$ docker-compose up -d
- Add event trigger through Hasura console using the above environment variable as
WEBHOOK_URL
.
Local Dev - Remote Schemas
- Create a new folder in
remote-schemas
folder:
In project directory:
$ cd remote-schemas
$ mkdir hello-schema
-
Write your graphql functions in
hello-schema/index.js
. Make sure you export the typedefs and resolvers. Ref: hello -
Add corresponding server setup in local development API server. Ref: localDevelopment
-
Start the local development API server:
In project directory:
$ cd local
$ node localDevelopment.js
-
Add remote schema URL as environment variable in
local/remote-schemas.env
. Ref: remote-schemas.env -
Restart Hasura (for refreshing environment variables):
In project directory:
$ cd local
$ docker-compose down
$ docker-compose up -d
- Add remote schema through Hasura console using the above environment variable as
GraphQL Server URL
.
CI/CD with CircleCI
We want to keep 3 environments in the cloud:
- Dev
- Staging
- Prod
The CI/CD system will deploy the application to each environment based on the branch on which the code is pushed:
branch | environment |
---|---|
master | dev |
staging | staging |
prod | prod |
-
Start by creating a project in CircleCI.
-
Add your git repo in your CircleCI dashboard. This repo has all the CircleCI configuration in
.circleci/config.yml
file. -
Create an API Gateway per environment (copy the
REST_API_ID
). -
Create a Lambda Basic Execution Role (name it
lambda-basic-role
): ref -
Configure environment variables in your CircleCI project from the dashboard. This example requires the following environment variables:
AWS_ACCOUNT_ID
AWS_REGION
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_DEV_REST_API_ID
AWS_DEV_HASURA_ECS_CLUSTER
AWS_DEV_TASK_FAMILY
HASURA_DEV_ENDPOINT
HASURA_DEV_ACCESS_KEY
AWS_STG_REST_API_ID
AWS_STG_HASURA_ECS_CLUSTER
AWS_STG_TASK_FAMILY
HASURA_STG_ENDPOINT
HASURA_STG_ACCESS_KEY
AWS_PROD_REST_API_ID
AWS_PROD_HASURA_ECS_CLUSTER
AWS_PROD_TASK_FAMILY
HASURA_PROD_ENDPOINT
HASURA_PROD_ACCESS_KEY
-
Git push or merge PR to master branch. This will deploy to dev environment.
-
Once you have tested the dev environment, you can promote to staging and prod environments by merging dev with staging and staging with prod respectively.