Skip to content

About

An extension for running Probot on Lambda

Topics

Resources

Stars

94 stars

Watchers

4 watching

Forks

Latest commit

 

History

174 Commits

Folders and files

Repository files navigation

@probot/adapter-aws-lambda-serverless

Adapter to run a Probot application function in AWS Lambda using the Serverless Framework

Build Status

Usage

Requires Node.js 22 or later. This adapter uses ES modules, matching Probot v14. Set "type": "module" in your application's package.json.

npm install @probot/adapter-aws-lambda-serverless
// handler.js
import {
  createLambdaFunction,
  createProbot,
} from "@probot/adapter-aws-lambda-serverless";
import appFn from "./index.js";
export const lambdaFn = createLambdaFunction(appFn, {
  probot: createProbot(),
});

Migrating from v4 to v5

Version 5 introduces breaking changes for applications using version 4:

  • Upgrade your AWS Lambda runtime to Node.js 22 or later, such as nodejs22.x.
  • Update your application and any directly installed probot dependency for compatibility with Probot 14.
  • Migrate CommonJS handlers and application modules to ES modules. Set "type": "module" in your application's package.json, replace require() with import, and replace module.exports with ES module exports, as shown in the usage example above.

Configuration

You need to add environment variables to configure Probot to your Lambda function. If you use the Serverless App, you can add parameters for APP_ID, PRIVATE_KEY, WEBHOOK_SECRET, the use these parameters in serverless.yaml.

provider:
  name: aws
  runtime: nodejs22.x
  lambdaHashingVersion: 20201221
  environment:
    APP_ID: ${param:APP_ID}
    PRIVATE_KEY: ${param:PRIVATE_KEY}
    WEBHOOK_SECRET: ${param:WEBHOOK_SECRET}
    NODE_ENV: production
    LOG_LEVEL: debug

functions:
  probot:
    handler: handler.lambdaFn
    events:
      - httpApi:
          path: /api/github/webhooks
          method: post

Make sure to configure your GitHub App registration's webhook URL to <your lambda's URL>/api/github/webhooks.

Logging invocation IDs

To include Lambda and request trace IDs in JSON logs, supply a Pino logger and use AsyncLocalStorage to associate fields with each invocation. Install Pino directly in your application:

npm install pino
// handler.js
import { AsyncLocalStorage } from "node:async_hooks";
import pino from "pino";
import {
  createLambdaFunction,
  createProbot,
} from "@probot/adapter-aws-lambda-serverless";
import appFn from "./index.js";

const invocation = new AsyncLocalStorage();
const log = pino({
  level: process.env.LOG_LEVEL || "warn",
  mixin() {
    return { ...invocation.getStore() };
  },
});

const handle = createLambdaFunction(appFn, {
  probot: createProbot({ defaults: { log } }),
});

export const lambdaFn = (event, context) => {
  const headers = Object.fromEntries(
    Object.entries(event.headers ?? {}).map(([name, value]) => [
      name.toLowerCase(),
      value,
    ]),
  );

  return invocation.run(
    {
      awsRequestId: context.awsRequestId,
      apiGatewayRequestId: event.requestContext?.requestId,
      traceId: headers["x-amzn-trace-id"],
      cloudFrontRequestId: headers["x-amz-cf-id"],
    },
    () => handle(event, context),
  );
};

The supplied Pino logger outputs JSON independently of NODE_ENV and LOG_FORMAT. Probot adds the GitHub delivery ID as id to event-handler logs. Invocation fields are available to logs created within the invocation's async context, not to startup logs emitted before it. Some request fields may be absent depending on the AWS integration. Log only selected headers rather than all headers, which may contain secrets.

Test deployment

For testing your Probot deployment without end to end (GitHub) integration, you can use this shell snippet:

$ LAMBDA_URL=https://x.execute-api.y.amazonaws.com/stage-or-basePath/api/github/webhooks
$ SECRET=the_webhook_secret
$ TMP_DATA_FILE=/tmp/smoke.data

$ echo -n "{\"action\":\"test\"}" > $TMP_DATA_FILE
$ SIGN=$(openssl dgst -sha256 -hmac $SECRET $TMP_DATA_FILE | cut -d" " -f2)
$ curl --request POST --header "X-Hub-Signature-256: sha256=$SIGN" --header "X-Github-Event: test" --header "X-GitHub-Delivery: fake" --data-binary "@$TMP_DATA_FILE" $LAMBDA_URL
{"ok":true}        <-- Concent for Probot v10: {"message":"Received test.test"}

Examples

Add yours!

Common errors

Http Code Message Description
400 Missing required headers A webhook header is missing; the JSON response lists the headers and links to CloudWatch documentation
403 Missing Authentication Token Bad endpoint (this one is not binded on Lambda)
500 Internal server error Incorrect header values or a Probot error

Webhooks require X-GitHub-Delivery, X-GitHub-Event, and a SHA-256 signature. Use X-Hub-Signature-256; a SHA-256 signature in X-Hub-Signature is also accepted as a fallback.

Probot v11 migration key points

This section describes the historical v11 migration. For Probot v14, use the ES module handler and Node.js runtime shown above.

For Probot v11 support, this adapter introduces significant changes. Here the key points to update (in addition of Probot v11 breaking changes):

Key point / Probot <= v10 >= v11
NPM package name @probot/serverless-lambda @probot/adapter-aws-lambda-serverless
handler.js content See Usage v1.x See Usage
AWS Lambda Runtime handler.probot handler.webhooks
AWS Lambda Handler Node.js 12.x (preferred) Node.js 12.x (required)

LICENSE

ISC

About

An extension for running Probot on Lambda

Topics

Resources

Stars

94 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages