Using IronPdf to create PDF files on AWS Lambda
1. Create AWS Lambda with a container template (.NET 5)
Please refer to the first part of this official document from AWS: https://aws.amazon.com/blogs/developer/net-5-aws-lambda-support-with-container-images/
2. Add package dependencies
These dependencies are required for Chrome in this AWS environment.
Please modify the Docker file according to these instructions:
3. Add the IronPDF (Linux) NuGet package
Install IronPdf.Linux
- In Solution Explorer, right-click References, Manage NuGet Packages
- Select Browse and search
IronPdf.Linux
- Select the package and install.
4. Modify FunctionHandler code
This example will create a pdf file from a webpage https://ironpdf.com/ and save to /tmp
. In order to view this pdf you must upload this pdf to another service such as S3.
public Casing FunctionHandler(string input, ILambdaContext context)
{
try
{
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");
var awsTmpPath = @"/tmp/"; // AWS temporary storage
//[optional] enable logging (please uncomment these code if you face any problem)
//IronPdf.Logging.Logger.EnableDebugging = true;
//IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
//IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
//set your license key
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";
//set ChromeGpuMode to Disabled
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
//set IronPDF Temp Path
Environment.SetEnvironmentVariable("TEMP", awsTmpPath, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("TMP", awsTmpPath, EnvironmentVariableTarget.Process);
IronPdf.Installation.TempFolderPath = awsTmpPath;
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;
//set auto LinuxAndDockerDependenciesAutoConfig
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
context.Logger.LogLine($"creating IronPdf.ChromePdfRenderer");
var Renderer = new IronPdf.ChromePdfRenderer();
context.Logger.LogLine($"rendering Pdf");
using var pdfDoc = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
var guid = Guid.NewGuid();
var fileName = $"/tmp/{input}_{guid}.pdf"; //save file to /tmp
context.Logger.LogLine($"saving pdf name : {fileName}");
pdfDoc.SaveAs(fileName);
//here you can upload saved pdf file to anywhere such as S3.
context.Logger.LogLine($"COMPLETE!");
}
catch (Exception e)
{
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
}
return new Casing(input?.ToLower(), input?.ToUpper());
}
}
public record Casing(string Lower, string Upper);
public Casing FunctionHandler(string input, ILambdaContext context)
{
try
{
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");
var awsTmpPath = @"/tmp/"; // AWS temporary storage
//[optional] enable logging (please uncomment these code if you face any problem)
//IronPdf.Logging.Logger.EnableDebugging = true;
//IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
//IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
//set your license key
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";
//set ChromeGpuMode to Disabled
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
//set IronPDF Temp Path
Environment.SetEnvironmentVariable("TEMP", awsTmpPath, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("TMP", awsTmpPath, EnvironmentVariableTarget.Process);
IronPdf.Installation.TempFolderPath = awsTmpPath;
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;
//set auto LinuxAndDockerDependenciesAutoConfig
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
context.Logger.LogLine($"creating IronPdf.ChromePdfRenderer");
var Renderer = new IronPdf.ChromePdfRenderer();
context.Logger.LogLine($"rendering Pdf");
using var pdfDoc = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
var guid = Guid.NewGuid();
var fileName = $"/tmp/{input}_{guid}.pdf"; //save file to /tmp
context.Logger.LogLine($"saving pdf name : {fileName}");
pdfDoc.SaveAs(fileName);
//here you can upload saved pdf file to anywhere such as S3.
context.Logger.LogLine($"COMPLETE!");
}
catch (Exception e)
{
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
}
return new Casing(input?.ToLower(), input?.ToUpper());
}
}
public record Casing(string Lower, string Upper);
Public Function FunctionHandler(ByVal input As String, ByVal context As ILambdaContext) As Casing
Try
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}")
Dim awsTmpPath = "/tmp/" ' AWS temporary storage
'[optional] enable logging (please uncomment these code if you face any problem)
'IronPdf.Logging.Logger.EnableDebugging = true;
'IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
'IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
'set your license key
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY"
'set ChromeGpuMode to Disabled
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled
'set IronPDF Temp Path
Environment.SetEnvironmentVariable("TEMP", awsTmpPath, EnvironmentVariableTarget.Process)
Environment.SetEnvironmentVariable("TMP", awsTmpPath, EnvironmentVariableTarget.Process)
IronPdf.Installation.TempFolderPath = awsTmpPath
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath
'set auto LinuxAndDockerDependenciesAutoConfig
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = True
context.Logger.LogLine($"creating IronPdf.ChromePdfRenderer")
Dim Renderer = New IronPdf.ChromePdfRenderer()
context.Logger.LogLine($"rendering Pdf")
Dim pdfDoc = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
Dim guid As System.Guid = System.Guid.NewGuid()
Dim fileName = $"/tmp/{input}_{guid}.pdf" 'save file to /tmp
context.Logger.LogLine($"saving pdf name : {fileName}")
pdfDoc.SaveAs(fileName)
'here you can upload saved pdf file to anywhere such as S3.
context.Logger.LogLine($"COMPLETE!")
Catch e As Exception
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}")
End Try
Return New Casing(input?.ToLower(), input?.ToUpper())
End Function
}
'INSTANT VB TODO TASK: C# 'records' are not converted by Instant VB:
' public record Casing(string Lower, string Upper)
5. Increase Memory and Timeout
IronPdf requires more time and memory than the default value of Lambda. You can configure it at aws-lambda-tools-defaults.json
. Please adjust this to match your function. In this example we will set it to 512 (MB) and 330 (seconds)
"function-memory-size" : 512,
"function-timeout" : 330,
"function-memory-size" : 512,
"function-timeout" : 330,
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'"function-memory-size" : 512, "function-timeout" : 330,
You also can update this configuration using the Lambda console. https://docs.aws.amazon.com/lambda/latest/dg/configuration-function-common.html#configuration-memory-console
6. Publish
Please follow the instructions in the latter part of this document to publish and test your Lambda function. https://aws.amazon.com/blogs/developer/net-5-aws-lambda-support-with-container-images/
7. Try it out!
You can activate the Lambda function using the Lambda console https://console.aws.amazon.com/lambda or via Visual Studio by using the AWS Toolkit for Visual Studio. https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/lambda-creating-project-in-visual-studio.html