Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(layer): Use local host to install requirements #177

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/common/helpers/python-lambda-layer-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import { execSync } from 'child_process';
import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as s3assets from 'aws-cdk-lib/aws-s3-assets';
Expand All @@ -19,8 +20,9 @@ export interface LayerProps {
runtime: lambda.Runtime;
architecture: lambda.Architecture;
path: string;
autoUpgrade?: boolean;
description: string;
autoUpgrade?: boolean;
local?: boolean;
}

export class Layer extends Construct {
Expand All @@ -29,16 +31,26 @@ export class Layer extends Construct {
constructor(scope: Construct, id: string, props: LayerProps) {
super(scope, id);

const { runtime, architecture, path, autoUpgrade, description } = props;
const { runtime, architecture, path, description, autoUpgrade, local } = props;

const args = ['-t /asset-output/python'];
const args = local ? [] : ['-t /asset-output/python'];
if (autoUpgrade) {
args.push('--upgrade');
}

const layerAsset = new s3assets.Asset(this, 'LayerAsset', {
path,
bundling: {
bundling: local ? {
// If local is true use the host to install the requirements
image: runtime.bundlingImage,
local: {
tryBundle(outputDir) {
execSync(`pip install -r ${path}/requirements.txt -t ${outputDir}/python ${args.join(' ')}`);
return true;
},
},
} : {
// Default: Docker is used to install the requirements
image: runtime.bundlingImage,
platform: architecture.dockerPlatform,
command: [
Expand Down
32 changes: 19 additions & 13 deletions src/patterns/gen-ai/aws-langchain-common-layer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@

## Table of contents

- [Credits](#credits)
- [Overview](#overview)
- [Initializer](#initializer)
- [Pattern Construct Props](#pattern-construct-props)
- [Pattern Properties](#pattern-properties)
- [Default properties](#default-properties)
- [Troubleshooting](#troubleshooting)
- [Architecture](#architecture)
- [Cost](#cost)
- [Security](#security)
- [Supported AWS Regions](#supported-aws-regions)
- [Quotas](#quotas)
- [Clean up](#clean-up)
- [aws-langchain-common-layer](#aws-langchain-common-layer)
- [Table of contents](#table-of-contents)
- [Credits](#credits)
- [Overview](#overview)
- [Initializer](#initializer)
- [Pattern Construct Props](#pattern-construct-props)
- [Pattern Properties](#pattern-properties)
- [Default properties](#default-properties)
- [Python utility layer (LangchainCommonLayer)](#python-utility-layer-langchaincommonlayer)
- [Registry](#registry)
- [Adapters](#adapters)
- [Troubleshooting](#troubleshooting)
- [Architecture](#architecture)
- [Cost](#cost)
- [Security](#security)
- [Supported AWS Regions](#supported-aws-regions)
- [Quotas](#quotas)
- [Clean up](#clean-up)

## Credits

Expand Down Expand Up @@ -183,6 +188,7 @@ Parameters
| runtime | [lambda.Runtime](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Runtime.html) | ![Required](https://img.shields.io/badge/required-ff0000) | Lambda function runtime compatible with this layer. |
| architecture | [lambda.Architecture](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Architecture.html)| ![Required](https://img.shields.io/badge/required-ff0000) | Lambda function architecture compatible with this layer. |
| autoUpgrade | boolean | ![Optional](https://img.shields.io/badge/optional-4169E1) | Add '--upgrade' to pip install requirements.txt. In case of a LangchainCommonLayer, this parameter is not used. |
| local | boolean | ![Optional](https://img.shields.io/badge/optional-4169E1) | Local compute will be used when installing requirements.txt. By default if this is not true, a docker container will be spun up to install requirements. |

## Pattern Properties

Expand Down
10 changes: 9 additions & 1 deletion src/patterns/gen-ai/aws-langchain-common-layer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export interface LangchainLayerProps{
* @default - none
*/
readonly autoUpgrade?: boolean;
/**
* Optional: Local compute will be used when installing requirements.txt.
* By default if this is not true, a docker container will be spun up to install requirements.
*
* @default - none
*/
readonly local?: boolean;
}

/**
Expand All @@ -64,8 +71,9 @@ export class LangchainCommonDepsLayer extends Construct {
runtime: props.runtime,
architecture: props.architecture,
path: path.join(__dirname, '../../../../layers/langchain-common-deps'),
autoUpgrade: props.autoUpgrade,
description: 'Dependencies to build gen ai applications with the langchain client',
autoUpgrade: props.autoUpgrade,
local: props.local,
});

this.layer = layer.layer;
Expand Down