Aspose library in the AWS Lambda environment

I am encountering an issue with “import aspose. words as aw” in AWS lambda env. Please see the error details below

[ERROR] KeyError: 'aspose.words'
Traceback (most recent call last):
  File "/var/lang/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/var/task/lambda_function.py", line 4, in <module>
    import aspose.words as aw

Do we have a separate library or method to run on AWS Lambda?

@manojTk5 Unfortunately, the provided error message does not give enough information for understanding what the problem is.

  • Are you able to install aspose.words package in AWS lambda env?
  • What OS is used in AWS lambda env?
  • What CPU?
  • If you are able to install aspose.words and the problem occurs while running code, please provide code and input documents that will allow us to reproduce the problem.
1 Like

@alexey.noskov please see the full Error

[ERROR] KeyError: 'aspose.words'
Traceback (most recent call last):
  File "/var/lang/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/var/task/lambda_function.py", line 4, in <module>
    import aspose.words as aw
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 674, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 642, in _load_backward_compatible
  • Are you able to install the aspose.words package in the AWS Lambda environment?

I have added the Aspose.Words library to my Python code folder and zipped it. This zip file needs to be uploaded to Lambda.

  • What OS is used in the AWS Lambda environment?

It uses a Linux OS.

  • If you are able to install aspose.words and the problem occurs while running the code, please provide the code and input documents that will allow us to reproduce the problem.

It is not even reaching the code execution; it stops at the import stage. I will provide my sample code.Preformatted text

import json
import boto3
import os
import aspose.words as aw
from botocore.exceptions import NoCredentialsError

def lambda_handler(event, context):
    # Load Aspose.Words license
    license_path = '/tmp/Aspose.Words.lic'
    s3 = boto3.client('s3')
    s3.download_file('', '', license_path)
    license = aw.License()
    license.set_license(license_path)

    # Extract bucket name and object key from the event
    try:
        bucket_name = event['Records'][0]['s3']['bucket']['name']
        object_key = event['Records'][0]['s3']['object']['key']
    except KeyError as e:
        return {
            'statusCode': 400,
            'body': json.dumps(f'Error extracting S3 bucket details: {e}')
        }

    # Download the RTF file from S3
    input_path = '/tmp/input.rtf'
    try:
        s3.download_file(bucket_name, object_key, input_path)
    except NoCredentialsError:
        return {
            'statusCode': 400,
            'body': json.dumps('Credentials not available')
        }

    # Convert RTF to PDF using Aspose.Words
    doc = aw.Document(input_path)
    pdf_path = '/tmp/output.pdf'
    doc.save(pdf_path, aw.SaveFormat.PDF)

    # Upload the PDF back to the S3 bucket
    pdf_key = object_key.replace('.rtf', '.pdf')
    s3.upload_file(pdf_path, bucket_name, pdf_key)

    return {
        'statusCode': 200,
        'body': json.dumps('RTF converted to PDF successfully')
    }

@manojTk5 Thank you for additional information. I will consult with our python developers and provide you more information.

1 Like

@alexey.noskov Thank you!

1 Like

@manojTk5 Have you tried installing Aspose.Words using pip in your AWS Lambda?
https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-native-libraries

1 Like

No, I have never tried that. I will try this method and check
Thank you @alexey.noskov

@manojTk5 Please let us know the results of testing.

1 Like

Yha sure will let you know

1 Like

Hi @alexey.noskov, that method worked fine. No errors this time. Thank you so much for the support. We really appreciate it

1 Like