AWS CloudFront Lambda serverless JavaScript prerenderer.
- Ability to prerender SPA (Single-page application) pages for SEO indexing.
- Ability to create screenshots of dynamically generated pages for testing.
- Serverless function, scales on a tight budget.
- Can be set-up easily (in minutes).
Get the Chromium binary
$ wget -c https://github.com/nuxy/chromium-lambda-build/releases/download/0.0.2/headless_shell.tar.gz -O - | tar -xz
$ ./deploy --profile <AWS credentials profile>
The following operations are orchestrated by AWS CloudFormation during execution:
- Docker container image is created and uploaded to AWS Elastic Container Registry.
- AWS Lambda function is created with a configured Function URL endpoint.
- AWS CloudFront distribution is created using the new function as origin.
- Network routing occurs thereby exposing your Lambda function URL
curl -X 'POST' \
'https://<url-id>.lambda-url.<region>.on.aws' \
-H 'Accept: text/html' \
-H 'Content-Type: application/json' \
-d '{"url": "<site-url>", "image": <boolean>}'
const {LambdaClient, InvokeCommand} = require('@aws-sdk/client-lambda');
const client = new LambdaClient({region: '<region>'});
const params = {
FunctionName: 'PrerenderApi',
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: JSON.stringify({
url: '<site-url>',
image: <boolean>
})
};
const command = new InvokeCommand(params);
try {
const {PayLoad} = await client.send(command);
console.log(Payload?.body));
} catch (err) {
console.warn(err.message);
throw err;
}
const AWS = require('aws-sdk');
const client = new AWS.Lambda({region: '<region>'});
const params = {
FunctionName: 'PrerenderApi',
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: JSON.stringify({
url: '<site-url>',
image: <boolean>
})
};
client.invoke(params).promise()
.then(function({Payload}) {
console.log(Payload?.body));
})
.catch(function(err) {
console.warn(err.message);
throw err;
});
The following function environment overrides can be configured in the SAM template file:
Variable name | Description | Default value |
---|---|---|
PRERENDER_DEBUG |
Enable verbose logging to CloudWatch group | false |
PRERENDER_TIMEOUT |
Execution timeout in seconds | 60 |
In order to successfully deploy your application you must have set-up your AWS Config and have created an IAM user with the following policies:
- IAMFullAccess
- CloudFrontFullAccess
- AWSCloudFormationFullAccess
- AWSLambda_FullAccess
- AmazonEC2ContainerRegistryFullAccess
WARNING: The policies above are provided to ensure a successful application deployment. It is recommended that you adjust these policies to meet the security requirements of your Lambda application. They should NOT be used in a Production environment.
Run ESLint on project sources:
$ npm run lint
Generate Swagger OpenAPI definitions:
$ npm run genapi
Run Mocha unit tests:
$ npm run test
Running in Lambda there is overhead that occurs (cold start) when the environment is first launched. This overhead does not include headless shell initialization which creates its own latency. Due to this, you can expect a longer response time for the first request. As long as there is an active Lambda handler (hot start) all subsequent requests will not incurr this overhead.
- Setting IAM Permissions and Roles
- Scaling and concurrency in Lambda
- Lambda quotas
- AWS SDK for JavaScript
- chromium-lambda-build
- content_switches.cc
This package is maintained under the Semantic Versioning guidelines.
This package is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
lambda-js-prerender is provided under the terms of the MIT license
AWS is a registered trademark of Amazon Web Services, Inc.