- Newest
- Most votes
- Most comments
The error you're encountering is related to the Dockerfile structure, not your CDK integration approach. Your use of Runtime.FROM_IMAGE with Code.fromDockerBuild() is correct for container images in Amplify Gen 2.
Root Cause
AWS Lambda container images must follow specific conventions. The main issue is that your Dockerfile uses /asset as the working directory instead of the Lambda-required ${LAMBDA_TASK_ROOT}.
Solution
Updated Dockerfile
FROM public.ecr.aws/x4k0r1b8/lambda-libreoffice-base:25.2-node22-x86_64 # Copy package files to Lambda task root COPY package*.json ${LAMBDA_TASK_ROOT}/ # Install dependencies WORKDIR ${LAMBDA_TASK_ROOT} RUN npm ci --only=production # Copy and build TypeScript source COPY handler.ts ./ COPY tsconfig.json ./ RUN npm install typescript @types/node RUN npx tsc # Set the handler (must match the handler parameter in your Function definition) CMD [ "handler.handler" ]
Updated Resource Definition
import * as path from "node:path"; import { fileURLToPath } from "node:url"; import { defineFunction } from "@aws-amplify/backend"; import { Duration } from "aws-cdk-lib"; import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda"; const functionDir = path.dirname(fileURLToPath(import.meta.url)); export const docxToPdf = defineFunction( (scope) => new Function(scope, "docx-to-pdf", { handler: "handler.handler", // Must match CMD in Dockerfile runtime: Runtime.FROM_IMAGE, timeout: Duration.seconds(45), memorySize: 1024, code: Code.fromDockerBuild(functionDir, { platform: 'linux/amd64', }), }) );
Key Points
- Your CDK approach is correct - Using
defineFunctionwith CDK constructs is the proper way to deploy container images in Amplify Gen 2 - Use
${LAMBDA_TASK_ROOT}- This is where Lambda expects your code to be located - Match handler names - The
handlerparameter in your Function definition must match the CMD in your Dockerfile - Build in container - Install dependencies and compile TypeScript inside the container for consistency
The standard Amplify Gen 2 defineFunction({ name: "..." }) pattern does not support container images - you need the CDK escape hatch approach you're already using.
It looks like there's an issue with how the Docker image is being specified for your Lambda function in Amplify Gen 2. The error message suggests that while you've correctly set runtime: Runtime.FROM_IMAGE, there might be a mismatch in how you're providing the Docker image code.
When using container images with Lambda in Amplify Gen 2, you need to ensure that the way you're specifying the code is compatible with the runtime type. The Code.fromDockerBuild() method might not be properly recognized in this context.
For Amplify Gen 2 functions, the approach is typically more streamlined. Instead of using the CDK constructs directly, you might want to try the Amplify Gen 2 way of defining functions:
- First, define your function using the standard Amplify Gen 2 pattern:
import { defineFunction } from "@aws-amplify/backend"; export const docxToPdf = defineFunction({ name: "docxToPdf", });
- Then create a
handler.tsfile with your function logic.
If you specifically need to use a Docker container with LibreOffice, you might need to:
- Build your Docker image separately
- Push it to Amazon ECR
- Reference the image URI in your function definition
Alternatively, you could try modifying your current approach by ensuring that all the CDK constructs are properly imported and used. Make sure you're using the latest version of the Amplify libraries and that your Docker build configuration is correct.
The error specifically mentions validation of the runtime when using an image asset, which suggests there might be an incompatibility between how you're specifying the Docker image and the runtime configuration.
Sources
Amplify Functions: Create serverless functions using TypeScript, powered by AWS Lambda | Front-End Web & Mobile
Deploy Lambda functions with container images - AWS Prescriptive Guidance
Deploy Python Lambda functions with container images - AWS Lambda
Relevant content
- asked a year ago
- AWS OFFICIALUpdated 10 months ago

Thanks for your answer. I've tried your solution but I get the following error:
It's looking in the
/assetfolder, not in the${LAMBDA_TASK_ROOT}Have you tried it yourself?