|
1 | | -## My Project |
| 1 | +# AWS Bedrock Langchain Python CDK Stack |
2 | 2 |
|
3 | | -TODO: Fill this README out! |
| 3 | +## Overview |
| 4 | +Optimize AWS Lambda functions with Boto3 by adding the latest packages and creating Lambda layers using `aws-cdk.aws-lambda-python-alpha`. Avoid common errors, like the numpy module issue, by following the guide. Use provided code and insights to enhance performance across various development environments. |
4 | 5 |
|
5 | | -Be sure to: |
6 | 6 |
|
7 | | -* Change the title in this README |
8 | | -* Edit your repository description on GitHub |
| 7 | +This AWS Cloud Development Kit (CDK) stack, named `AwsBedrockLangchainPythonCdkStack`, is designed to deploy AWS Lambda functions along with the necessary AWS Identity and Access Management (IAM) roles and policies. The stack includes the following components: |
9 | 8 |
|
| 9 | +1. **IAM Roles and Policies:** |
| 10 | + - A Lambda execution role (`LambdaRole`) is created with permissions to access S3 buckets and the Bedrock service. It is associated with the `AWSLambdaBasicExecutionRole` managed policy and a custom policy (`bedrock_policy`) allowing specific Bedrock actions. |
| 11 | + |
| 12 | +2. **Lambda Layers:** |
| 13 | + - Two Lambda layers, `boto3_lambda_layer` and `langchain_lambda_layer`, are defined with specific configurations for architecture and runtime. These layers contain Python dependencies for the Lambda functions. |
| 14 | + |
| 15 | +3. **Lambda Functions:** |
| 16 | + - The stack deploys two Lambda functions, `boto3_bedrock_example_lambda` and `langchain_bedrock_example_lambda`, each with its own code, runtime settings, and associated layers. |
| 17 | + |
| 18 | +4. **CDK NAG Suppressions:** |
| 19 | + - CDK NAG (Not a Good Idea) suppressions are applied to address specific warnings related to IAM policies. These suppressions ensure that the Lambda execution policy and associated resources comply with the intended access patterns. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +### Installing the Latest Boto3 Package |
| 24 | + |
| 25 | +To ensure you have the latest version of the Boto3 package, follow these steps: |
| 26 | + |
| 27 | +1. **Create a Lambda Layer:** |
| 28 | + |
| 29 | + Use the `aws-cdk.aws-lambda-python-alpha` package to define a Lambda layer containing the latest Boto3 package. |
| 30 | + |
| 31 | + ```python |
| 32 | + boto3_lambda_layer = _alambda.PythonLayerVersion(self, |
| 33 | + 'boto3-lambda-layer', |
| 34 | + entry = './aws_bedrock_langchain_python_cdk/lambda/layer/boto3_latest/', |
| 35 | + compatible_architectures=[_lambda.Architecture.ARM_64], |
| 36 | + compatible_runtimes=[_lambda.Runtime.PYTHON_3_11], |
| 37 | + ) |
| 38 | + ``` |
| 39 | + |
| 40 | +2. **Attach the Layer to Your Lambda Function:** |
| 41 | + |
| 42 | + Once the layer is created, attach it to your Lambda function to ensure access to the latest Boto3 package and its bedrock API calls. |
| 43 | + |
| 44 | + ```python |
| 45 | + boto3_bedrock_example_lambda = _lambda.Function( |
| 46 | + self, |
| 47 | + "boto3_bedrock_example_lambda", |
| 48 | + handler="index.lambda_handler", |
| 49 | + code=_lambda.Code.from_asset("./aws_bedrock_langchain_python_cdk/lambda/code/boto3_example/"), |
| 50 | + runtime=_lambda.Runtime.PYTHON_3_11, |
| 51 | + architecture=_lambda.Architecture.ARM_64, |
| 52 | + role=lambda_role, |
| 53 | + layers=[ |
| 54 | + boto3_lambda_layer |
| 55 | + ], |
| 56 | + timeout=Duration.seconds(300), |
| 57 | + memory_size=1024, |
| 58 | + ) |
| 59 | + ``` |
| 60 | + |
| 61 | +### Langchain Package Integration |
| 62 | + |
| 63 | +Similarly, optimize your Lambda functions by installing the Langchain package. Follow the same process of creating a Lambda layer as with Boto3, replacing "boto3" in the `requirements.txt` file with "langchain," and then attach it to your function to leverage Langchain's capabilities. |
| 64 | + |
| 65 | +1. **Create a Lambda Layer:** |
| 66 | + |
| 67 | + ```python |
| 68 | + langchain_lambda_layer = _alambda.PythonLayerVersion(self, |
| 69 | + 'langchain-lambda-layer', |
| 70 | + entry = './aws_bedrock_langchain_python_cdk/lambda/layer/langchain_latest/', |
| 71 | + compatible_architectures=[_lambda.Architecture.ARM_64], |
| 72 | + compatible_runtimes=[_lambda.Runtime.PYTHON_3_11], |
| 73 | + ) |
| 74 | + ``` |
| 75 | + |
| 76 | +2. **Attach the Layer to Your Lambda Function:** |
| 77 | + |
| 78 | + ```python |
| 79 | + langchain_bedrock_example_lambda = _lambda.Function( |
| 80 | + self, |
| 81 | + "langchain-bedrock-example-lambda", |
| 82 | + handler="index.lambda_handler", |
| 83 | + code=_lambda.Code.from_asset("./aws_bedrock_langchain_python_cdk/lambda/code/langchain_example/"), |
| 84 | + runtime=_lambda.Runtime.PYTHON_3_11, |
| 85 | + architecture=_lambda.Architecture.ARM_64, |
| 86 | + role=lambda_role, |
| 87 | + layers=[ |
| 88 | + boto3_lambda_layer, |
| 89 | + langchain_lambda_layer |
| 90 | + ], |
| 91 | + timeout=Duration.seconds(300), |
| 92 | + memory_size=1024, |
| 93 | + ) |
| 94 | + ``` |
| 95 | + |
| 96 | +### Avoiding Common Pitfalls |
| 97 | + |
| 98 | +When creating Lambda layers, be mindful of specifying the compatible architecture, either ARM_64 or X86_64. Failure to define this may result in container image errors, such as the numpy module error. This error can manifest as: |
| 99 | + |
| 100 | +```json |
| 101 | +{ |
| 102 | + "errorMessage": "Unable to import module 'index': Error importing numpy: you should not try to import numpy from its source directory; please exit the numpy source tree, and relaunch your python interpreter from there.", |
| 103 | + "errorType": "Runtime.ImportModuleError", |
| 104 | + "requestId": "977004a7-f205-481a-8b53-7a4b5bf48d2b", |
| 105 | + "stackTrace": [] |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +To prevent this issue, **explicitly mention** the compatible architectures during the layer creation process. |
| 110 | + |
| 111 | + |
| 112 | +## Useful commands |
| 113 | + |
| 114 | +To manually create a virtualenv on MacOS and Linux: |
| 115 | + |
| 116 | +``` |
| 117 | +$ python3 -m venv .venv |
| 118 | +``` |
| 119 | + |
| 120 | +After the init process completes and the virtualenv is created, you can use the following |
| 121 | +step to activate your virtualenv. |
| 122 | + |
| 123 | +``` |
| 124 | +$ source .venv/bin/activate |
| 125 | +``` |
| 126 | + |
| 127 | +If you are a Windows platform, you would activate the virtualenv like this: |
| 128 | + |
| 129 | +``` |
| 130 | +% .venv\Scripts\activate.bat |
| 131 | +``` |
| 132 | + |
| 133 | +Once the virtualenv is activated, you can install the required dependencies. |
| 134 | + |
| 135 | +``` |
| 136 | +$ pip install -r requirements.txt |
| 137 | +``` |
| 138 | + |
| 139 | +At this point you can now synthesize the CloudFormation template for this code. |
| 140 | + |
| 141 | +``` |
| 142 | +$ cdk synth |
| 143 | +``` |
| 144 | + |
| 145 | +To add additional dependencies, for example other CDK libraries, just add |
| 146 | +them to your `setup.py` file and rerun the `pip install -r requirements.txt` |
| 147 | +command. |
| 148 | + |
| 149 | + * `cdk ls` list all stacks in the app |
| 150 | + * `cdk synth` emits the synthesized CloudFormation template |
| 151 | + * `cdk deploy` deploy this stack to your default AWS account/region |
| 152 | + * `cdk diff` compare deployed stack with current state |
| 153 | + * `cdk docs` open CDK documentation |
10 | 154 | ## Security |
11 | 155 |
|
12 | 156 | See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. |
13 | 157 |
|
14 | 158 | ## License |
15 | 159 |
|
16 | 160 | This library is licensed under the MIT-0 License. See the LICENSE file. |
17 | | - |
|
0 commit comments