Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ serverless install -u https://github.com/serverless/examples/tree/master/folder-
| [Serverless Github Check](https://github.com/serverless/examples/tree/master/aws-node-github-check) | nodeJS |
| [Aws Github Webhook Listener](https://github.com/serverless/examples/tree/master/aws-node-github-webhook-listener) <br/> Extend your github repositories with this github webhook listener | nodeJS |
| [Aws Node Graphql Api With Dynamodb](https://github.com/serverless/examples/tree/master/aws-node-graphql-api-with-dynamodb) <br/> A single-module GraphQL endpoint with query and mutation functionality. | nodeJS |
| [Aws Lambda And Heroku Postgres](https://github.com/serverless/examples/tree/master/aws-node-heroku-postgres) <br/> Shows how to connect AWS Lambda to Heroku Postgres. Uses an api:release Heroku webhook and the Heroku API to handle automatic Heroku Postgres credential rotation. | nodeJS |
| [Aws Iot Event](https://github.com/serverless/examples/tree/master/aws-node-iot-event) <br/> Example on how to setup a AWS IoT Rule to send events to a Lambda function | nodeJS |
| [Dropbox](https://github.com/serverless/examples/tree/master/aws-node-oauth-dropbox-api) <br/> dropbox integration | nodeJS |
| [Aws Node Puppeteer](https://github.com/serverless/examples/tree/master/aws-node-puppeteer) <br/> When it comes to AWS Lambda function , they have their own limits as follows ![AWS Limits](./images/aws_limits.png) So , When you try to use Puppeteer your deployment package size(unzipped) easily go's above 250 mb because When you install Puppeteer, it downloads a recent version of Chromium (~170MB Mac, ~282MB Linux, ~280MB Win) that is guaranteed to work with the API. | nodeJS |
Expand Down
6 changes: 6 additions & 0 deletions aws-node-heroku-postgres/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless
14 changes: 14 additions & 0 deletions aws-node-heroku-postgres/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--
title: 'Node.js AWS Lambda connecting to Heroku Postgres'
description: 'Shows how to connect AWS Lambda to Heroku Postgres. Uses an api:release Heroku webhook and the Heroku API to handle automatic Heroku Postgres credential rotation.'
layout: Doc
framework: v1
platform: AWS
language: nodeJS
authorLink: 'https://github.com/welkie'
authorName: 'Matt Welke'
authorAvatar: 'https://avatars0.githubusercontent.com/u/7719209'
-->
# aws-node-heroku-postgres

An example app, created in this [blog post](https://mattwelke.com/2019/01/06/free-tier-managed-sql-with-aws-lambda-and-heroku-postgres.html), showing how to connect AWS Lambda to Heroku Postgres. Uses an `api:release` Heroku webhook and the Heroku API to handle automatic Heroku Postgres credential rotation.
86 changes: 86 additions & 0 deletions aws-node-heroku-postgres/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// handler.js
'use strict';

const express = require('express');
const serverless = require('serverless-http');
const pg = require('pg');
const axios = require('axios');
const parsePgConnStr = require('pg-connection-string').parse;

// Replace with your token or API key and your Heroku Postgres resource name
const herokuApiKey = '';
const herokuPostgresId = '';
const herokuClient = axios.create({
baseURL: 'https://api.heroku.com/',
headers: {
'Authorization': `Bearer ${herokuApiKey}`,
'Accept': 'application/vnd.heroku+json; version=3',
},
});

let pgConfig;
let pgPool;

const app = express();

const createConn = async () => {
console.log('Creating PG connection.');

const credsResponse = await herokuClient.get(`addons/${herokuPostgresId}/config`);
const pgConnStr = credsResponse.data[0]['value'];

pgConfig = {
...parsePgConnStr(pgConnStr), ...{
max: 1,
ssl: true,
},
};

pgPool = new pg.Pool(pgConfig);
};

const performQuery = async () => {
const client = await pgPool.connect();
const result = await client.query('SELECT now()');
client.release();
return result;
};

app.get('/hello', async function (req, res) {
if (!pgPool) {
// Cold start. Get Heroku Postgres creds and create pool.
await createConn();
} else {
console.log('Using existing PG connection.');
}

try {
const result = await performQuery();

res.json({
result: `According to PostgreSQL, the time is: ${result.rows[0].now}`,
pgConfigUsed: pgConfig,
});
return;
} catch (e) {
res.json({
error: e.message,
});
return;
}

});

app.post('/onrelease', async function (req, res) {
// Get Heroku Postgres creds and replace pool with new one.
await createConn();

// Response with 2xx response so Heroku knows webhook was successful.
// Response body doesn't matter.
res.status(204).send();
});

module.exports = {
app,
hello: serverless(app),
};
8 changes: 8 additions & 0 deletions aws-node-heroku-postgres/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// index.js
'use strict';

const { app } = require('./handler');

app.listen(3000, () => {
console.info(`Listening on port 3000.`);
});
Loading