Skip to content

Commit cb6b78c

Browse files
committed
Update with more instructions
1 parent 5300082 commit cb6b78c

4 files changed

Lines changed: 727 additions & 40 deletions

File tree

README.md

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# PostGraphile Lambda Example
2+
13
## Overview
24

3-
This project shows how to use PostGraphile on Lambda. It's a work in progress
4-
with the following aims:
5+
This project shows an example of how you might use PostGraphile on Lambda. It
6+
has the following aims:
57

68
- [x] Startup without needing to introspect database
79
- [x] Fast startup
@@ -12,9 +14,20 @@ with the following aims:
1214
- [x] No requirement for Node.js `http`-based libraries (such as Connect,
1315
Express, Koa)
1416

15-
### Method
17+
## Non-goals
18+
19+
Postgraphile-lambda-example does NOT intend to watch the schema for changes;
20+
this means that you *must* build and release a new version every time you
21+
change your database. (You only need to update the cache file though.)
22+
23+
Postgraphile-lambda-example does NOT intend to make this fully compatible with
24+
the `postgraphile` CLI - this will be a subset best suited to Lambda usage.
1625

17-
Here's some of the techniques we'll be using to achieve the goals:
26+
Postgraphile-lambda-example does NOT intend to support subscriptions.
27+
28+
## Method
29+
30+
We use the following tools:
1831

1932
- webpack - to bundle up the required code into as small a file as possible
2033
(no need for `node_modules` any more!)
@@ -23,14 +36,47 @@ Here's some of the techniques we'll be using to achieve the goals:
2336
bundle; then when the Lambda service starts up it can read from the cache
2437
rather than introspecting the database again.
2538

26-
### Non-goals
39+
## Building lambda.zip
40+
41+
First clone this repository locally, and install dependencies:
42+
43+
```
44+
yarn
45+
```
46+
47+
Next, set up a `.env` file matching your environment:
48+
49+
```
50+
cp .env.template .env
51+
```
52+
53+
And modify the `src/postgraphileOptions.js` file to your taste.
54+
55+
Finally run:
56+
57+
```
58+
yarn build
59+
```
60+
61+
This will result in a `lambda.zip` file that you can upload to Amazon Lambda.
62+
63+
## Setting up a Lambda endpoint
64+
65+
1. Visit https://console.aws.amazon.com/lambda/home and click 'Create function'
66+
2. Select "Author from scratch" and give your function a name, select the most recent Node.js release (at least 8.10+), create (or select) a role (I granted "Simple microservice permissions")
67+
3. Click "Create function" and wait about 15 seconds; you should be greeted with a "Congratulations" message.
68+
4. Scroll to "Function code", select "Upload a .zip file" from "Code entry type", press the "Upload" button and select the `lambda.zip` file you generated above; then click "Save"
69+
5. Scroll to "Environment variables" and enter your `DATABASE_SCHEMAS` and `DATABASE_URL` settings; then click "Save"
70+
6. Scroll to the top and select "API Gateway" under "Add triggers" in the "Designer", then scroll to "Configure triggers"
71+
7. Create a new API; if you like add 'image/gif', 'image/png', 'image/icon' and 'image/x-icon' to the list of binary media types; then press "Add" followed by "Save"
72+
8. Click the name of the API gateway to go to the API gateway config
73+
9. Select the `/` route under `Resources` and from the "Actions" dropdown, select "Create method" add create an `ANY` method
74+
10. Turn on "Lambda Proxy Integration" and enter your lambda function name in the relevant box, then press "Save"
75+
11. Finally, go to "Actions" again and "Deploy API"
76+
12. Copy the "Invoke URL" and paste it into your GraphQL client of choice - you can now talk to your Lambda PostGraphile API 😅
2777

28-
We do NOT intend to watch the schema for changes; this means that you *must*
29-
build and release a new version every time you change your database. (You only
30-
need to update the cache file though.)
3178

32-
We do NOT intend to make this fully compatible with the `postgraphile` CLI -
33-
this will be a subset best suited to Lambda usage.
79+
## How it works
3480

3581
### Phases
3682

@@ -72,7 +118,7 @@ Launch the bundle in the `sam local` test environment, and run a series of reque
72118

73119
Left as an exercise to the reader.
74120

75-
### Prerequisites
121+
### Test Prerequisites
76122

77123
* [docker](https://docs.docker.com/install/)
78124
* [aws sam cli](https://docs.aws.amazon.com/lambda/latest/dg/sam-cli-requirements.html) - `pip install aws-sam-cli`
@@ -160,4 +206,4 @@ Note that SAM unpacks the zip and reboots node for every single request, so you'
160206

161207
### Thanks
162208

163-
Improvements to PostGraphile's support for Lambda were sponsored by https://consciousconsumers.nz/
209+
Improvements to PostGraphile's support for Lambda were sponsored by [Connecting Good](https://cogo.co/)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
"phase2": ". ./.env && scripts/generate-cache",
66
"phase3": ". ./.env && scripts/bundle",
77
"phase4": ". ./.env && scripts/test",
8-
"prepare": ". ./.env && scripts/build && scripts/generate-cache && scripts/bundle",
8+
"build": ". ./.env && scripts/build && scripts/generate-cache && scripts/bundle",
99
"sam": ". ./.env && scripts/build && scripts/generate-cache && scripts/bundle && scripts/sam",
1010
"test": ". ./.env && scripts/build && scripts/generate-cache && scripts/bundle && scripts/test"
1111
},
1212
"dependencies": {
1313
"aws-serverless-express": "3.1.3",
1414
"pg": "7.4.1",
15-
"postgraphile": "^4.0.0-rc.5.1-ab3bbc5"
15+
"postgraphile": "^4.1.0-alpha.1"
1616
},
1717
"devDependencies": {
1818
"webpack": "4.17.2",

src/postgraphileOptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
exports.options = {
22
dynamicJson: true,
33
graphiql: false,
4+
cors: true,
45
jwtSecret: process.env.JWT_SECRET || String(Math.random()),
5-
jwtPgTypeIdentifier: 'forum_example.jwt_token',
66
};

0 commit comments

Comments
 (0)