dgraph-lambda

by Helm

Helm chartv0.0.2App v1.2.4

Serverless platform for running JavaScript on Slash GraphQL (or Dgraph).

About this chart

dgraph-lambda helm chart

TL;DR

helm repo add dgraph https://charts.dgraph.io
helm install "my-lambda" dgraph/dgraph-lambda

Introduction

Prerequisites

  • Kubernetes 1.16+
  • Helm 3.0+
  • Dgraph v20.11.0 or greater
    • configured with graphql_lambda_url

Configuration

In order to use dgraph-lambda, you must do the following:

  1. deploy Dgraph Alpha configured to point to dgraph-lambda
  2. configure lambda script helm override values for dgraph-lambda
  3. deploy Dgraph Lambda service configured to point to Dgraph Alpha services
  4. add GraphQL schema for the resolvers added by the lambda script

Environment

For these steps, set up the following environment variables:

export NS=default
export LAMBDA_REL=my-lambda
export DGRAPH_REL=my-release

Deploy Dgraph Alpha

Dgraph Alpha needs to be configured with the --graphql lambda-url=<url> argument (see: lambda server) to support dgraph-lambda. You can use environment variables to configure this:

helm install $DGRAPH_REL dgraph/dgraph \
  --namespace $NS \
  --set alpha.extraEnvs[0].name=DGRAPH_ALPHA_GRAPHQL \
  --set alpha.extraEnvs[0].value=lambda-url=http://$LAMBDA_REL-dgraph-lambda.$NS.svc:80/graphql-worker

The DGRAPH_ALPHA_GRAPHQL_LAMBDA_URL environment variable points to Dgraph Lambda service that will be deployed in the next step. The format Dgraph Lambda domain name is formatted as follows:

http://<helm-chart-release-name>-dgraph-lambda.<namesapce>.svc/graphql-worker

Configure Lambda script

First we can create a Helm override values with the lambda script embedded into it like the example below:

# my-lambda.yaml
script:
  enabled: true
  script: |
    async function authorsByName({args, dql}) {
        const results = await dql.query(`query queryAuthor($name: string) {
            queryAuthor(func: type(Author)) @filter(eq(Author.name, $name)) {
                name: Author.name
                dob: Author.dob
                reputation: Author.reputation
            }
        }`, {"$name": args.name})
        return results.data.queryAuthor
    }

    async function newAuthor({args, graphql}) {
        // lets give every new author a reputation of 3 by default
        const results = await graphql(`mutation ($name: String!) {
            addAuthor(input: [{name: $name, reputation: 3.0 }]) {
                author {
                    id
                    reputation
                }
            }
        }`, {"name": args.name})
        return results.data.addAuthor.author[0].id
    }

    self.addGraphQLResolvers({
        "Query.authorsByName": authorsByName,
        "Mutation.newAuthor": newAuthor
    })

Deploy Dgraph Lambda

We can deploy the example above, named my-lambda.yaml, with the follwing:

helm install $LAMBDA_REL dgraph/dgraph-lambda \
  --namespace $NS \
  --values my-lambda.yaml \
  --set env[0].name=DGRAPH_URL \
  --set env[0].value=http://$DGRAPH_REL-dgraph-alpha-headless.$NS.svc:8080

The DGRAPH_URL environment variable will point to Dgraph Alpha service deployed in the previous step. The format Dgraph Alpha domain name is formatted as the following:

http://<helm-chart-release-name>-dgraph-alpha-headless.<namespace>.svc:8080

Add GraphQL schema

After deploying both services with the lambda script, upload a schema:

## create schema file
cat <<-EOF > my-schema.graphql
type Author {
    id: ID!
    name: String! @search(by: [hash, trigram])
    dob: DateTime
    reputation: Float
}

type Query {
    authorsByName(name: String!): [Author] @lambda
}

type Mutation {
    newAuthor(name: String!): ID! @lambda
}
EOF

## port-forward Dgraph Alpha
kubectl port-forward --namespace $NS $DGRAPH_REL-dgraph-alpha-0 8080:8080

## upload schema file
curl http://localhost:8080/admin/schema --upload-file my-schema.graphql

Configuration

The following table lists the configurable parameters of the dgraph chart and their default values.

ParameterDescriptionDefault
replicaCountnumber of Kubernetes replicas1
image.repositoryContainer repository namedgraph/dgraph-lambda
image.pullPolicyContainer image pull policyIfNotPresent
image.tagContainer image tagv1.2.4
imagePullSecretsImage pull secrets auth tokens used to access a private registry[]
nameOverrideName override of the default chart name""
fullnameOverrideFull Name override of the release name + chart name""
script.enabledEnable adding a lambda scriptfalse
script.scriptEmbedded lambda script stored in a config map""
envEnvironment variablessee values.yaml
serviceAccount.createSpecifies if service account should be createdtrue
serviceAccount.annotationsService Account annotations{}
serviceAccount.nameService Account name""
podAnnotationsAdditional pod annotations{}
podSecurityContextPod Security context to define privilege and access control{}
securityContextContainer Security context to define privilege and access control{}
service.typeService type (ClusterIP, NodePort, LoadBalancer)ClusterIP
service.portService inbound port80
service.targetPortService targetPort of dgraph-lambda service8686
ingress.enabledIngress enabledfalse
ingress.annotationsIngress annotations{}
ingress.hostsIngress hosts list to configure virtual hosts + routes to the servicesee values.yaml
ingress.tlsIngress tls configuration[]
resourcesResource limites and requests{}
nodeSelectorNode selection constraints{}
tolerationsAllow scheduling pods onto nodes matching specified taints[]
affinityAffinity configuration to allow nodes to scheduled on desired nodes{}

Deploy dgraph-lambda with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has dgraph-lambda on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartdgraph-lambda is pinned at v0.0.2 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply