Failed to bundle asset error with CDK + Lambda
Asked Answered
H

2

7

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

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,
            }),
        });

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

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!

Hock answered 1/5, 2022 at 20:22 Comment(2)
Did you manage to solve this? My guess is that your synth step is missing the setting privileged: true. It's also not enough perhaps to enable this, but you when you do, also remove at first the lambda function requiring bundling from the build step. Then, let me pipeline mutate itself to make it privileged, then push back the lambda, and it could work.Paniculate
It's telling you that you don't have docker installed.Taiwan
G
0

You probably need to bundle the Python function locally:

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
            }
          }
        }
      })
    })
  }
}

This is from:

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

There are also two open GitHub Issues referenced there (well they have been closed automatically but are still valid).

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

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

Garthgartner answered 3/5, 2023 at 14:39 Comment(0)
S
0

I faced the same problem for a .net based cdk project. I didn't have my docker daemon running locally. When I started the docker agent, I got this successfully running.

Strychninism answered 12/9 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.