question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[lambda-python] Abstraction for bundling code for use in either a Function or a Layer

See original GitHub issue

The current bundling function that drives PythonFunction contains 99% of what I need to accomplish building a Layer, but doesn’t allow me to place assets into runtime-specific directories.

It would be nice if there were a publicized construct of some sort that encapsulated the knowledge about runtime specific paths, and contained the bundling code that could be used for either a PythonFunction or a python-based Layer.

Use Case

This would make integrating layer code into function code more intuitive since a python layer would put the code and dependencies in the right /opt path for me. It would also cut down on the amount of code currently needed to build a layer, which is almost completely redundant of what is currently in the bundle functions (for both lambda-nodejs and lambda-python).

Proposed Solution

I hacked this quickly by allowing an output subdirectory as part of my bundle options. But an abstraction that houses all those runtime specific paths and drops code into them would be cool.

/**
 * Options for bundling
 */
export interface BundlingOptions {
  /**
   * Entry path
   */
  readonly entry: string;

  /**
   * The runtime of the lambda function
   */
  readonly runtime: lambda.Runtime;

  /**
   * Subdirectory in which to place bundled assets inside the output directory
   *
   * This is useful if building a layer and you need to place
   * bundled assets inside the AWS specified runtime folders.
   *
   * e.g. /python for Python
   *
   * @default ""
   */
  readonly outputSubdirectory?: string;
}

enum Installer {
  PIP = 'pip',
  PIP3 = 'pip3',
}

function chain(commands: string[]): string {
  return commands.filter((c) => !!c).join(' && ');
}

/**
 * Produce bundled Lambda asset code
 */
export function bundle(options: BundlingOptions): lambda.AssetCode {
  const installer = options.runtime === lambda.Runtime.PYTHON_2_7 ? Installer.PIP : Installer.PIP3;
  const outputSubdirectory = options.outputSubdirectory || '';
  const targetDirectory = path.join(cdk.AssetStaging.BUNDLING_OUTPUT_DIR, outputSubdirectory);

  const hasRequirements = fs.existsSync(path.join(options.entry, 'requirements.txt'));

  const depsCommand = chain([
    hasRequirements ? `${installer} install -r requirements.txt -t ${targetDirectory}` : '',
    `cp -r . ${targetDirectory}`,
  ]);

  return lambda.Code.fromAsset(options.entry, {
    bundling: {
      image: BundlingDockerImage.fromRegistry(`amazon/aws-sam-cli-build-image-${options.runtime.name}`),
      command: ['bash', '-c', depsCommand],
    },
  });
}

Other

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
adamelmorecommented, Aug 25, 2020

FYI, layer support will be coming soon in https://github.com/aws/aws-cdk/pull/9582

1reaction
adamelmorecommented, Oct 19, 2020

This was closed out by #9582 cc @eladb @SomayaB

Read more comments on GitHub >

github_iconTop Results From Across the Web

Managing AWS SDKs in Lambda functions
The bundled SDK is provided as a convenience for developers building simpler functions or using the Lambda console for development. In these cases,...
Read more >
Lambda in Python | A Quick Glance of Lambda functions
Guide to Lambda in Python. Here we discuss the introduction to Lambda functions along with characteristics, key elements and sample programming exmple.
Read more >
How to Build Both Kinds of AWS Lambda Layers. (Yes, There ...
We're here today to talk about the third option: Lambda layers. A layer is just another bundle of code that sits alongside your...
Read more >
How to Install Python Packages for AWS Lambda Layers
Limitations of Lambda Layers · You can only use up to 5 layers per Lambda. · The size of all your layers unzipped...
Read more >
@aws-cdk/aws-lambda-python - npm
Amazon Lambda Python Library · Python Function · Python Layer · Packaging · Custom Bundling · Custom Bundling with Code Artifact · Keywords....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found