A preview URL for every pull request with AWS Lambda Web Functions

GitHub Actions → Amazon S3 Code bucket → AWS Lambda Web Function → Client → Reviewer

Give every GitHub pull request its own live preview URL on a Lambda Web Function endpoint, updated on each push and deleted on close.

This pattern gives every pull request in a GitHub repository its own live preview URL, the way hosting platforms such as Vercel and Netlify do, using one AWS Lambda Web Function. A GitHub Actions workflow deploys each pull request to an endpoint named pr-<number>, comments the URL on the pull request, updates the same URL on every push and deletes the endpoint when the pull request closes. Merging to main deploys the prod endpoint.
A Web Function can have up to 10 endpoints, each with its own HTTPS domain, and an endpoint with Disabled auto-deployment keeps serving the revision that you point it at. Every endpoint in this pattern uses Disabled: with the default LatestRevision, production would serve the code of the last pull request that was pushed.
The workflows assume AWS IAM roles with GitHub OpenID Connect, so the repository stores no AWS keys. Because each endpoint has its own ARN, the preview role can change only pr-* endpoints and can pass only a separate preview execution role, so pull request code cannot touch the production endpoint or run with production permissions. The preview role cannot delete revisions either: the Production workflow deletes the preview revisions that no endpoint routes to.
Idle previews scale to zero, and each preview endpoint is capped at 2 execution environments and 100 requests per second. Preview endpoints are public HTTPS URLs like production, so add authentication in the app if previews must stay private.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/lambda-web-pr-previews/nodejs/sam

Deploy

Get your repository's OIDC subject prefix with gh api repos/my-org/my-repo/actions/oidc/customization/sub --jq .sub_claim_prefix.Deploy the stack: sam deploy --stack-name lambda-web-pr-previews --region us-east-1 --capabilities CAPABILITY_IAM --parameter-overrides GitHubSubjectPrefix=<prefix>.Deploy production: AWS_REGION=us-east-1 scripts/deploy.sh production. Then copy src, scripts and .github into your repository and set the repository variables described in the GitHub repo.


Testing

Create a preview from your terminal with AWS_REGION=us-east-1 scripts/deploy.sh preview 1, or open a pull request in your repository. See the GitHub repo for detailed testing instructions.

Cleanup

Delete the Web Function and empty the code bucket: AWS_REGION=us-east-1 scripts/deploy.sh destroy.
Delete the stack: sam delete --stack-name lambda-web-pr-previews --region us-east-1 --no-prompts.

Created by:

Manish S

Manish S

AWS Support Engineer, trying to build things