added nodejs8 aws lambda boilerplate (#790)

This commit is contained in:
Selvaganesh 2019-03-01 15:38:51 +05:30 committed by Shahidh K Muhammed
parent 039f16b906
commit 95587bc028
6 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,23 @@
# Setup tables
1. Create table:
```
notes:
id: int
note: text
```
# Setup AWS Lambda
Create a lambda function in AWS. This will be our webhook.
1. Create a function.
2. Select Node.js 8.10 as the runtime.
3. Select "start from scratch".
4. Add API gateway as a trigger.
5. Add an API to API gateway.
6. Add the code in `index.js`. The handler function of your lambda will be the `index.handler`.
# Add the trigger in Hasura GraphQL
1. In events tab, add a trigger
2. Select all insert, update, delete operations for the trigger.
3. Paste the API endpoint of your AWS lambda as the webhook.

View File

@ -0,0 +1,21 @@
exports.handler = async (event) => {
let response = {}
try {
let { table: { name }, event: { op, data } } = event.body;
response.statusCode = 200;
if (name === "notes" && op === "INSERT") {
response.body = `New note ${data.new.id} inserted, with data: ${data.new.note}`;
}
else if (name === "notes" && op === "UPDATE") {
response.body = `Note ${data.new.id} updated, with data: ${data.new.note}`;
}
else if (name === "notes" && op === "DELETE") {
response.body = `Note ${data.old.id} deleted, with data: ${data.old.note}`;
}
return response
} catch (e) {
response.statusCode = 400;
response.body = "cannot parse hasura event";
return response
}
};

View File

@ -0,0 +1,2 @@
node_modules
*.zip

View File

@ -0,0 +1,40 @@
# Setup tables
1. Create the table using the console:
```
Table name: notes
Columns:
id: Integer auto-increment
note: Text
Table name: note_revision
Columns:
id: Integer auto-increment
note: Text
note_id: Integer (foreign key to notes.id)
update_at: Timestamp, default `now()`
```
# Setup AWS Lambda
Create a lambda function in AWS. This will be our webhook.
1. In this folder, run `npm install`
2. Then create a zip: `zip -r hge-mutation.zip .`
3. Create a Lambda function.
4. Select Node.js 8.10 as the runtime.
5. Select "start from scratch".
6. Add API gateway as a trigger.
7. Add an API to API gateway.
8. Upload the zip from previous step. The handler function of your lambda will be `index.handler`.
9. Add the following enviroment variables in your lambda config:
1. `ACCESS_KEY`: this is the access key you configured when you setup HGE.
2. `HGE_ENDPOINT`: the URL on which you HGE instance is running.
# Add the trigger in Hasura GraphQL
1. In events tab, add a trigger
2. Select all insert, update, delete operations for the trigger.
3. Paste the API endpoint of your AWS lambda as the webhook.

View File

@ -0,0 +1,46 @@
// Lambda which gets triggered on insert, and in turns performs a mutation
const fetch = require('node-fetch');
const accessKey = process.env.ACCESS_KEY;
const hgeEndpoint = process.env.HGE_ENDPOINT;
const query = `
mutation updateNoteRevision ($noteId: Int!, $data: String!) {
insert_note_revision (objects: [
{
note_id: $noteId,
note: $data
}
]) {
affected_rows
}
}
`;
exports.handler = async (event) => {
try {
const qv = { noteId: event.body.event.data.old.id, data: event.body.event.data.old.note };
const result = await fetch(hgeEndpoint + '/v1alpha1/graphql', {
method: 'POST',
body: JSON.stringify({ query: query, variables: qv }),
headers: { 'Content-Type': 'application/json', 'x-hasura-access-key': accessKey },
});
const { errors, data } = await result.json();
if (errors) {
throw new Error(errors);
} else {
return {
statusCode: 200,
body: "success"
};
}
} catch (e) {
return {
statusCode: 400,
body: "cannot parse hasura event"
};
}
};

View File

@ -0,0 +1,13 @@
{
"name": "mutation-trigger",
"version": "1.0.0",
"description": "sample lambda to react to database updates",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC",
"dependencies": {
"node-fetch": "^2.2.0"
}
}