Skip to content

Commit 34caaa4

Browse files
authored
Merge pull request #2 from probot/update-readme
Update to Probot 7 and add readme
2 parents f8aea8f + a301155 commit 34caaa4

File tree

4 files changed

+614
-3568
lines changed

4 files changed

+614
-3568
lines changed

README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
## Lambda Handler for Probot
1+
## AWS Lambda Extension for Probot
2+
A [Probot](https://github.com/probot/probot) extension to make it easier to run your Probot Apps in AWS Lambda.
23

3-
4-
5-
<details>
6-
<summary>TODO: Not published yet. Coming soon.</summary>
4+
## Usage
75

86
```shell
97
$ npm install @probot/serverless-lambda
108
```
119

1210
```javascript
1311
# handler.js
14-
const serverless = require('@probot/serverless-lambda');
15-
const plugin = require('./')
16-
module.exports.probot = serverless(plugin)
12+
const serverless = require('@probot/serverless-lambda')
13+
const appFn = require('./')
14+
module.exports.probot = serverless(appFn)
1715
```
1816

19-
</details>
17+
## Configuration
18+
This package moves the functionality of `probot run` into a handler suitable for usage on AWS Lambda + API Gateway. Follow the documentation on [Environment Configuration](https://probot.github.io/docs/configuration/) to setup your app's environment variables. You can add these to `.env`, but for security reasons you may want to use the [AWS CLI](https://aws.amazon.com/cli/) or [Serverless Framework](https://github.com/serverless/serverless) to set Environment Variables for the function so you don't have to include any secrets in the deployed package.
19+
20+
For the private key, since AWS environment variables cannot be multiline strings, you could [Base64 encode](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) the `.pem` file you get from the GitHub App or use [KMS](https://aws.amazon.com/kms/) to encrypt and store the key.
21+
22+
## Differences from `probot run`
23+
24+
#### Local Development
25+
Since Lambda functions do not start a normal node process, the best way we've found to test this out locally is to use [`serverless-offline`](https://github.com/dherault/serverless-offline). This plugin for the serverless framework emulates AWS Lambda and API Gateway on your local machine, allowing you to continue working from `https://localhost:3000/probot` before deploying your function to AWS.
26+
27+
#### Long running tasks
28+
Some Probot Apps that depend on long running processes or intervals will not work with this extension. This is due to the inherent architecture of serverless functions, which are designed to respond to events and stop running as quickly as possible. For longer running apps we recommend using [other deployment options](https://probot.github.io/docs/deployment).
29+
30+
#### Only responds to Webhooks from GitHub
31+
This extension is designed primarily for receiving webhooks from GitHub and responding back as a GitHub App. If you are using [HTTP Routes](https://probot.github.io/docs/http/) in your app to serve additional pages, you should take a look at [`serverless-http`](https://github.com/dougmoscrop/serverless-http), which can be used with Probot's [express server](https://github.com/probot/probot/blob/master/src/server.ts) by wrapping `probot.server`.

index.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
const createProbot = require('probot');
1+
const { Application } = require('probot')
22
const { resolve } = require('probot/lib/resolver')
33
const { findPrivateKey } = require('probot/lib/private-key')
44
const { template } = require('./views/probot')
55

6+
let app
7+
let probot
8+
69
const loadProbot = (plugin) => {
7-
const probot = createProbot({
10+
app = app || new Application({
811
id: process.env.APP_ID,
912
secret: process.env.WEBHOOK_SECRET,
1013
cert: findPrivateKey()
@@ -14,16 +17,13 @@ const loadProbot = (plugin) => {
1417
plugin = resolve(plugin)
1518
}
1619

17-
probot.load(plugin)
20+
app.load(plugin)
1821

19-
return probot
22+
return app
2023
}
2124

22-
2325
module.exports.serverless = (plugin) => {
24-
2526
return async (event, context) => {
26-
2727
// 🤖 A friendly homepage if there isn't a payload
2828
if (event.httpMethod === 'GET' && event.path === '/probot') {
2929
const res = {
@@ -37,7 +37,7 @@ module.exports.serverless = (plugin) => {
3737
}
3838

3939
// Otherwise let's listen handle the payload
40-
const probot = loadProbot(plugin)
40+
probot = probot || loadProbot(plugin)
4141

4242
// Ends function immediately after callback
4343
context.callbackWaitsForEmptyEventLoop = false
@@ -53,25 +53,31 @@ module.exports.serverless = (plugin) => {
5353
console.log(`Received event ${e}${event.body.action ? ('.' + event.body.action) : ''}`)
5454
if (event) {
5555
try {
56-
await probot.receive({
56+
await app.receive({
5757
event: e,
5858
payload: event.body
5959
})
6060
const res = {
6161
statusCode: 200,
6262
body: JSON.stringify({
63-
message: 'Hi Node8!'
63+
message: `Received ${e}.${event.body.action}`
6464
})
6565
}
6666
return context.done(null, res)
6767
} catch (err) {
6868
console.error(err)
69-
return err
69+
return context.done(null, {
70+
statusCode: 500,
71+
body: JSON.stringify(err)
72+
})
7073
}
7174
} else {
7275
console.error({ event, context })
73-
callback('unknown error')
76+
context.done(null, 'unknown error')
7477
}
78+
return context.done(null, {
79+
statusCode: 200,
80+
body: 'Nothing to do.'
81+
})
7582
}
76-
7783
}

0 commit comments

Comments
 (0)