A collection of Serverless-framework based functions for AWS Lambda demonstrating the serverless-plugin-chrome
plugin for Serverless to run Headless Chrome serverless-ly. The example functions include:
- A Print to PDF handler
- A Capture Screenshot handler
- A Page-load Request Logger handler
- A Version Info handler (💤 )
First, install serverless
globally:
npm install serverless -g
Then pull down the example service:
serverless create -u \
https://github.com/adieuadieu/serverless-chrome/tree/master/examples/serverless-framework/aws
And install the dependencies:
npm install
We recommend using a tool like AWS Vault to manage your AWS credentials.
You must configure your AWS credentials either by defining AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
environmental variables, or using an AWS profile. You can read more about this on the Serverless Credentials Guide.
In short, either:
export AWS_PROFILE=<your-profile-name>
or
export AWS_ACCESS_KEY_ID=<your-key-here>
export AWS_SECRET_ACCESS_KEY=<your-secret-key-here>
Once Credentials are set up, to deploy the full service run:
npm run deploy
This example service includes the following functions, each demonstrating a common pattern/use-case.
When you the serverless function, it creates a Lambda function which will take a screenshot of a URL it's provided. You can provide this URL to the Lambda function via the AWS API Gateway. After a successful deploy, an API endpoint will be provided. Use this URL to call the Lambda function with a url in the query string. E.g. https://XXXXXXX.execute-api.us-east-1.amazonaws.com/dev/screenshot?url=https://github.com/adieuadieu/serverless-chrome
. Add &mobile=1
for mobile device view.
To deploy the Capture Screenshot function:
serverless deploy -f screenshot
The printToPdf handler will create a PDF from a URL it's provided. You can provide this URL to the Lambda function via the AWS API Gateway. After a successful deploy, an API endpoint will be provided. Use this URL to call the Lambda function with a url in the query string. E.g. https://XXXXXXX.execute-api.us-weeast-2.amazonaws.com/dev/pdf?url=https://github.com/adieuadieu/serverless-chrome
This handler also supports configuring the "paper" size, orientation, etc. You can pass any of the DevTools Protocol's Page.printToPdf()
method's parameters. For example, for landscape oriented PDF add &landscape=true
to the end of the URL. Be sure to remember to escape the value of url
if it contains query parameters. E.g. https://XXXXXXX.execute-api.us-east-1.amazonaws.com/dev/pdf?url=https://github.com/adieuadieu/serverless-chrome&landscape=true
To deploy the Capture Screenshot function:
serverless deploy -f pdf
Returns an array of every request which was made loading a given page.
To deploy the Page-load Request Logger function:
serverless deploy -f request-logger
Prints version info of headless chrome binary
To deploy the Chrome Version Info function:
serverless deploy -f version-info
These are simple functions and don't offer any configuration options. Take a look at the serverless-plugins-chrome
plugin's README for it's configuration options.
Go for it. Locally, if installed, Chrome will be launched. More in the plugin's README.
Invoke a function locally with:
serverless invoke local -f replaceThisWithTheFunctionName
I keep getting a timeout error when deploying and it's really annoying.
Indeed, that is annoying. I've had the same problem, and so that's why it's now here in this troubleshooting section. This may be an issue in the underlying AWS SDK when using a slower Internet connection. Try changing the AWS_CLIENT_TIMEOUT
environment variable to a higher value. For example, in your command prompt enter the following and try deploying again:
export AWS_CLIENT_TIMEOUT=3000000
Aaaaaarggghhhhhh!!!
Uuurrrggghhhhhh! Have you tried filing an Issue?