Failed to bundle asset error with CDK + Lambda

rod*_*ocf 6 aws-lambda aws-cdk

I have this project structure (where control is the name and root of my project):

control
|_ src
  |_ control_loader -> this has a function inside called also control_loader
  |_ utils
     |_ some_helper_function.py
     |_ __init__.py
  |_ __init__.py
|_ lib
   |_ some-cdk-where-i-declare-a-lambda.ts
|_ requirements.txt
Run Code Online (Sandbox Code Playgroud)

Inside some-cdk-where-i-declare-a-lambda.ts I have this (among all the other necessary stuff):

        new Function(this, `${this.appName}${this.stageName}ControlLambdaLoader`, {
            code: Code.fromAsset(path.join(__dirname, '../src'), {
                bundling: {
                    image: Runtime.PYTHON_3_8.bundlingImage,
                    command: [
                        'bash', '-c',
                        'pip install -r requirements.txt -t /asset-output && cp -au . /asset-output',
                    ],
                },
            }),
            runtime: Runtime.PYTHON_3_8,
            handler: 'control_loader.control_loader',
            vpc,
            vpcSubnets: vpc.selectSubnets({
                subnetType: SubnetType.PRIVATE_WITH_NAT,
            }),
        });
Run Code Online (Sandbox Code Playgroud)

However, upon running cdk synth, I get the following:

(venv) PS C:\Users\rodri\Documents\control> cdk synth
npx: installed 15 in 1.145s
Bundling asset controlPipelineStack/controlBetaDeployStage/controlbetaStack/controlbeta/controlbetaControlLambdaLoader/Code/Stage...
Failed to bundle asset controlPipelineStack/controlBetaDeployStage/controlbetaStack/controlbeta/controlbetaControlLambdaLoader/Code/Stage, bundle output is located at C:\Users\rodri\Documents\control\cdk.out\asset.059c3b383943a1fadd3d933b670a7d351991e742d24a9785474b35c846267fde-error: Error: spawnSync docker EN
OENT
Run Code Online (Sandbox Code Playgroud)

This is a very cryptic error. I know the bundling is done by docker to push the dependencies as a zip asset, but, any idea of where is this failing? I also tried to change the location of requirements.txt to inside src and that didn't help. I can deploy everything I remove the Lambda out. What am I doing wrong? Also, how do I make the bundle include some_helper_function.py as well?

Thanks!

Ric*_*ard 0

您可能需要在本地捆绑 Python 函数:

import * as cdk from '@aws-cdk/core';
import { Code, Runtime, Function} from '@aws-cdk/aws-lambda';
import * as path from 'path';
import { execSync } from 'child_process';

export class CdkLocalBundlingExampleStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const functionDir = path.join(__dirname, "functions", "exampleFunction")

    const exampleFunction = new Function(this, "ExampleFunction", {
      handler: 'index.handler',
      runtime: Runtime.PYTHON_3_8,
      code: Code.fromAsset(functionDir, {
        bundling: {
          image: Runtime.PYTHON_3_8.bundlingImage,
          local: {
            tryBundle(outputDir: string) {
              try {
                execSync('pip3 --version')
              } catch {
                return false
              }

              execSync(`pip install -r ${path.join(functionDir, "requirements.txt")} -t ${path.join(outputDir)}`)
              execSync(`cp -au ${functionDir}/* ${path.join(outputDir)}`)
              return true
            }
          }
        }
      })
    })
  }
}
Run Code Online (Sandbox Code Playgroud)

这是来自:

https://github.com/1davidmichael/cdk-local-bundling-example

那里还引用了两个开放的 GitHub 问题(它们已自动关闭,但仍然有效)。

https://github.com/aws/aws-cdk/issues/12940

https://github.com/aws/aws-cdk/issues/11230