Introduction
- In this blog post, we’ll show how to create a Lambda function that generates PDF file from HTML using Puppeteer and uploads PDF to S3.
- We’ll also explore how to deploy Chromium on AWS Lambda Layer.
- This blog post uses the (AWS CLI) to simplify the deployment of AWS resources.
- You can download the code for this blog post from the
Prerequisites
- An AWS account with appropriate permissions.
- AWS CLI - check out globally.
- Node.js 18+ and NPM -
In the project structure, there are a few main files to focus on:
cdk.json: Contains information that the toolkit will use to run our app. In our case, it will benpx ts-node --prefer-ts-exts bin/cdk-typescript-lambda-chromium.ts.
bin/cdk-typescript-lambda-chromium.ts: The entry point of the CDK application. It loads whatever the stack we define inlib/cdk-typescript-lambda-chromium-stack.ts.
lib/cdk-typescript-lambda-chromium-stack.ts: This is where the CDK application's main stack is defined. We'll spend most of our time here to create Lambda Function.
Create S3 bucket
First, we’ll create a new S3 bucket to store Chromium Layer and generated PDF files with CLI command below:
CODEaws s3 mb s3://YOUR_S3_BUCKET_NAME
Setting up Puppeteer and Chrome on AWS Lambda
Next, we’ll need to use
puppeteer-core, which includes only a compressed version of Chrome. To incorporate Puppeteer into AWS Lambda function, we'll utilize thechromiumpackage, packages by
Create a Lambda Function Stack with AWS CDK
Then, we’ll define our CDK stack, which will contain the AWS resources required for PDF generating function.
Before that, we’ll install a package called
dotenvand create aconfig.tsfile inlibto load environment variables from.envfile. Install the package using:
CODEnpm install dotenv
We'll use the AWS CDK to define the infrastructure as code. The :
CODEconst bucketArn = config.getS3BucketArn()
lambdaFunction.addToRolePolicy(
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['s3:GetObject', 's3:PutObject'],
resources: [bucketArn, `${bucketArn}/*`],
})
)
Finally, we create a Lambda URL endpoint by calling the
addFunctionUrl()method on our Lambda Function instance. We pass in two options:
authType: The authentication mode required to invoke the Lambda function; we useFunctionUrlAuthType.NONEto make the Lambda publicly accessible, so anyone with the Function URL can invoke it.
cors: We set it to ['*'] to allow all domains.
CODEconst myFunctionUrl = lambdaFunction.addFunctionUrl({
authType: lambda.FunctionUrlAuthType.NONE,
cors: {
allowedOrigins: ['*'],
},
})
new cdk.CfnOutput(this, 'LambdaNodeUrl', {
value: myFunctionUrl.url,
})
Define the Lambda Function Code
In the project structure, we’ll create a folder called
src/generating-pdfto store all function code with Node.js runtime.
The entire Lambda function code can be found
Next, run
cdk bootstrapto create a CloudFormation stack that includes the necessary resources.
After the deployment completes, we'll receive the Lambda function's URL in the output, which you can invoke to generate PDFs.
Conclusion
In this blog post, we walked through deploying a Lambda function with Puppeteer and Chromium for PDF generation and explored setting up AWS resources using AWS CDK. This is a scalable solution to generate PDFs in the cloud.
I hope this post helpful to you and thank you for reading.
Reference
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
SOCIAL SHARE CARD GENERATOR