aws-cdk: (aws-lambda-python-alpha): The command "export" does not exist - Poetry

Describe the bug

Our python poetry lambda deployments started failing with the error message The command "export" does not exist. I guess this is due to the recent poetry 2.0 release (https://github.com/python-poetry/poetry/releases/tag/2.0.0) that removes the export command and relies on the poetry-plugin-export plugin instead. The issue can be solved by using a custom build image that installs the plugin, this is the Dockerfile that worked for me: https://github.com/aws/aws-cdk/pull/32761/files.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

Lambda bundling succeeds.

Current Behavior

Bundling fails with The command "export" does not exist

Reproduction Steps

new lambda.PythonLayerVersion(this, "TestLayer", {
  entry: path.join(__dirname, "../layer"),
  description: "Test Lambda Layer with Poetry dependencies",
  compatibleRuntimes: [cdk.aws_lambda.Runtime.PYTHON_3_11],
});

Possible Solution

Install plugin in build image, see https://github.com/aws/aws-cdk/pull/32761/files

Additional Information/Context

No response

CDK CLI Version

2.142.1

Framework Version

No response

Node.js Version

18.0.0

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 20
  • Comments: 21 (9 by maintainers)

Most upvoted comments

This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.

I am having this issue and solved it by using a custom command in the bundling step. It would be great if the poetry version was pinned automatically in the bundling step.

new PythonFunction(this, `Name`, {
    ...
    bundling: {
      command: [
          'bash', '-c',
          'pip install poetry==1.5.1 && poetry install --no-dev --no-interaction --no-ansi && cp -r . /asset-output'
      ],
      user: 'root'
    }
})

EDIT: this worked locally and in CircleCI but not in CodePipeline, it failed with permission issues.

Thanks, I understand that.

however, is if not save to include something like that

# Install the poetry-plugin-export plugin, needed for poetry versions >= 2.0.0
    poetry self add poetry-plugin-export -q && \

to the docker file. that should not do anything for versions starting from poetry=1.5.1 till 2.0.0 and will also include desired export plugin starting from versions 2.0.0.

is it not a good compromise i.e. is there any risk?

To anyone wanting an easy workaround to use dependencies managed by poetry v2 this is what I’m using:

import { PythonFunction } from '@aws-cdk/aws-lambda-python-alpha';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import Path from "node:path";

const pythonFunction = new PythonFunction(stack, "PythonFuncWithPoetry2", {
  entry: Path.join(import.meta.dirname, "location/to/code/root"),
  runtime: Runtime.PYTHON_3_11,
  index: "location/to/your/python/function/relative/to/root/index.py",
  handler: "handler_function_name",
  bundling: {
    buildArgs: {
        POETRY_VERSION: '2.1.2',
    },
    user: 'root', // Required to install poetry-plugin-export package
    commandHooks: {
      beforeBundling: () => [`poetry self add poetry-plugin-export`],
      afterBundling: () => [] // After bundling command is not used but we get a type error if we don't provide it
    }
  }
})

Although my overal cdk version was showing as 2.143.0, after sending logs over to AWS they discovered that I was using v2.92.0 of @aws-cdk/aws-lambda-python-alpha in a couple of my stacks. Uplifted @aws-cdk/aws-lambda-python-alpha to latest and things are looking better now.