You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+58-12Lines changed: 58 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,9 @@
1
+
# PostGraphile Lambda Example
2
+
1
3
## Overview
2
4
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:
5
7
6
8
-[x] Startup without needing to introspect database
7
9
-[x] Fast startup
@@ -12,9 +14,20 @@ with the following aims:
12
14
-[x] No requirement for Node.js `http`-based libraries (such as Connect,
13
15
Express, Koa)
14
16
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.
16
25
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:
18
31
19
32
- webpack - to bundle up the required code into as small a file as possible
20
33
(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:
23
36
bundle; then when the Lambda service starts up it can read from the cache
24
37
rather than introspecting the database again.
25
38
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 😅
27
77
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.)
31
78
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
34
80
35
81
### Phases
36
82
@@ -72,7 +118,7 @@ Launch the bundle in the `sam local` test environment, and run a series of reque
72
118
73
119
Left as an exercise to the reader.
74
120
75
-
### Prerequisites
121
+
### Test Prerequisites
76
122
77
123
*[docker](https://docs.docker.com/install/)
78
124
*[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'
160
206
161
207
### Thanks
162
208
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/)
0 commit comments