Running Aspose.Words python inside aws lambda

Hi there,

My company is looking at implementing Aspose.Words as our primary method for handling and converting documents between file formats.

We use AWS Lambda extensively and we’d like to deploy an api endpoint with AWS Lambda to convert documents for us.

One of the primary issues we’re running into is the package size of aspose-words, it’s 66.1mb unzipped, which means that it’s too large to use in a single lambda layer, see this error from cloud formation deployment:

UPDATE_FAILED AWS::Lambda::Function MyLambdaFunction Resource handler returned message: “Function
code combined with layers exceeds the maximum
allowed size of 262144000 bytes. The actual
size is 263554047 bytes. (Service: Lambda,
Status Code: 400, Request ID:
7e676f7e-ab2f-4b91-9351-572fd76c2167)”
(RequestToken:
149fd67b-1724-5938-0052-073d2499ed30,
HandlerErrorCode: InvalidRequest)

So I’m wondering how others have deployed Aspose.Words over AWS Lambda, I understand you can potentially deploy it over a Dockerfile but I’m unsure which architectures are supported.

Any help would be appreciated.

@DavidGaylard

Could you please specify which programming language you are using for AWS Lambda and if you have any specific architecture in mind for the deployment?

Sure, of course.

It’s running on Python.

The architecture is:

AWS SAM deployment with an API gateway.

Fixed this issue with a stable deployment of Aspose.Words for Python in lambda function using Lambda containers.

Here’s a quick readme - hopefully this helps someone else out there!

# Deploying a Lambda Function with a Container Image from ECR

This guide outlines the process of building a Docker image for a Lambda function, pushing it to Amazon ECR, and deploying it using AWS SAM.

## 1. Dockerfile

Create a `Dockerfile` in your project directory:

```dockerfile
FROM public.ecr.aws/lambda/python:3.9.2024.10.04.18-x86_64

# Install necessary system dependencies
RUN yum update -y && yum install -y \
    libgdiplus \
    libicu

# Copy requirements.txt
COPY requirements.txt ${LAMBDA_TASK_ROOT}

# Install the specified packages
RUN pip install -r requirements.txt

# Copy function code
COPY app.py ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler
CMD [ "app.handler" ]

2. Build the Docker Image

Build the Docker image, specifying the platform:

docker build --platform linux/amd64 -t your-function-name:latest .

3. Push the Image to ECR

Login to ECR:

aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin your-account-id.dkr.ecr.your-region.amazonaws.com

Tag and push the image:

docker tag your-function-name:latest your-account-id.dkr.ecr.your-region.amazonaws.com/your-repository-name:latest
docker push your-account-id.dkr.ecr.your-region.amazonaws.com/your-repository-name:latest

4. Update SAM Template

In your template.yaml, use the specific image digest instead of the :latest tag:

Resources:
  YourFunctionName:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      ImageUri: your-account-id.dkr.ecr.your-region.amazonaws.com/your-repository-name@sha256:your-image-digest
      # ... other properties ...

To get the image digest, you can use the AWS CLI:

aws ecr describe-images --repository-name your-repository-name

Look for the imageDigest in the output and use that in your template.yaml.

5. Deploy with SAM

Deploy your Lambda function:

sam deploy

Notes

  • Using a specific image digest ensures that you’re deploying a consistent version of your function.
  • The --platform linux/amd64 flag when building ensures compatibility with Lambda’s execution environment, especially important when building on machines with different architectures (e.g., M1 Macs).
  • Always verify that the pushed image in ECR is not a manifest file before using it in your SAM template.
  • Replace your-account-id, your-region, your-function-name, and your-repository-name with your specific AWS account details and chosen names.

@DavidGaylard Thank you for sharing your solution. You can also find a list of supported Docker images:
https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET/blob/master/Dockerfile