{"id":13674720,"url":"https://github.com/ARautio/aws-lambda-pdf-generator-puppeteer","last_synced_at":"2025-04-28T14:32:03.480Z","repository":{"id":38763916,"uuid":"198268125","full_name":"ARautio/aws-lambda-pdf-generator-puppeteer","owner":"ARautio","description":"PDF generator for AWS lambda with puppeteer","archived":false,"fork":false,"pushed_at":"2023-03-04T04:22:57.000Z","size":741,"stargazers_count":57,"open_issues_count":16,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T04:51:13.989Z","etag":null,"topics":["aws-lambda","nodejs","pdf-generation","puppeteer"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ARautio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-22T17:03:33.000Z","updated_at":"2024-08-09T06:55:43.000Z","dependencies_parsed_at":"2024-11-11T14:34:54.420Z","dependency_job_id":"e344cf19-f49e-4136-af89-c05f397c2418","html_url":"https://github.com/ARautio/aws-lambda-pdf-generator-puppeteer","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARautio%2Faws-lambda-pdf-generator-puppeteer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARautio%2Faws-lambda-pdf-generator-puppeteer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARautio%2Faws-lambda-pdf-generator-puppeteer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARautio%2Faws-lambda-pdf-generator-puppeteer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ARautio","download_url":"https://codeload.github.com/ARautio/aws-lambda-pdf-generator-puppeteer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251330380,"owners_count":21572278,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws-lambda","nodejs","pdf-generation","puppeteer"],"created_at":"2024-08-02T11:00:57.956Z","updated_at":"2025-04-28T14:32:02.920Z","avatar_url":"https://github.com/ARautio.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# AWS lambda PDF generator example\n\nThe purpose of this repository is to demonstrate a PDF generator inside\nAWS lambda with [chrome-aws-lambda](https://www.npmjs.com/package/chrome-aws-lambda), [serverless](https://serverless.com/), [pug](https://pugjs.org/) and [knex](https://knexjs.org/). This has been done as part of the article in dev.to\n\n# Setup\n\n1. Initialize serverless either inside project or globally (after installing package globally) with\n\n```\nserverless\n```\n\n2. Modify PUG template inside src/\n3. Fetch data for the template. In this repository we use knex and postgresSQL. For production use I would suggest to add database username and password to environmental variables either inside deployment platform or in AWS.\n4. Deploy with\n\n```\nnpm run deploy:dev\n```\n\n## Usage\n\nDeploying this to AWS will generate you an url like https://xxxx.execute-api.*area*.amazonaws.com/development/pdf/{yearMonth} in development and https://xxxx.execute-api.*area*.amazonaws.com/production/pdf/{yearMonth} in production.\n\nWhen running this setup with 1024 MB memory, it takes rougly 4 seconds to execute. With more complex pages or data fetching default timeout may not be enough so you may need to increase the timeout either with [serverless.yaml](https://www.serverless.com/framework/docs/providers/aws/guide/functions/) or straight from AWS console.\n\nPuppeteer is pretty versatile so you can create PDF from your own html file like in this example with PUG template language or calling URL.\n\nMore information regarding this repository can found from [Generate a PDF in AWS Lambda with NodeJS and Puppeteer](https://dev.to/akirautio/generate-a-pdf-in-aws-lambda-with-nodejs-and-puppeteer-2b93) dev.to post.\n\n## Security\n\nSince this deployment setup exposes your PDF generator straight to open the internet, it can be called just by knowing the URL. This can lead to the abuse of your application resulting in either your data to leak or getting a huge bill from AWS.\n\nTo mitigate the risk, there are several ways to increase security and reduce/block the abuse:\n\n1. Enabling the API key to API Gateway to limit access and monitor the usage. In this option, a client needs to send an API key in the request to be able to use the lambda function so you need to create a way to get the API key to the client. It won't fully block the abuse since the API key is exposed when making the request and the same key can be used again but it will give you a chance to limit the usage amount. This works pretty well when you don't have authorization in your application.\n\nServerless has [API key plugin](https://www.serverless.com/plugins/serverless-add-api-key) to create this for your lambda function.\n\n2. Enable authorizer to API Gateway (either [AWS_IAM](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/#http-endpoints-with-aws_iam-authorizers) or [custom](https://www.serverless.com/framework/docs/providers/aws/events/apigateway#http-endpoints-with-custom-authorizers)). In this option, you need to have a user authorization set up (either provided by AWS or using a custom one). This method blocks the abuse from unauthorized use but if you have open registration, the user can still make an unlimited amount of request.\n\n3. Checking authorization before starting up puppeteer. This is almost the same as the previous option but the authorization check is done inside the lambda and not in the API gateway. The difference is that you don't have to have a custom authorization function the Lambda function handles itself. It won't block the abuse since the endpoint will respond every time but it makes execution time for unauthorized use a lot smaller.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FARautio%2Faws-lambda-pdf-generator-puppeteer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FARautio%2Faws-lambda-pdf-generator-puppeteer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FARautio%2Faws-lambda-pdf-generator-puppeteer/lists"}