DEV Community

desawsume
desawsume

Posted on

Alternative way of working Lambda layer in CDK

What if your Lambda functions doesn't have the module or packages in AWS, and you wish to run this Lambda in the Cloud

Simple answer is using Lambda Layer.

CDK has the native way of handling it like below

layer = aws_lambda.LayerVersion(self, 'lambda-layer',
                code = code,
                layer_version_name='layer-bundle',
                compatible_runtimes=[
                    aws_lambda.Runtime.PYTHON_3_7,
                    aws_lambda.Runtime.PYTHON_3_8,
                    aws_lambda.Runtime.PYTHON_3_9
                ]
            )
Enter fullscreen mode Exit fullscreen mode

The other way of do this is:

Deploy Python Lambda functions with .zip file archives.

- cd lambda

- python3 -m venv .venv-zip

- source .venv-zip/bin/activate

- pip install -r requirements.txt

- deactivate

- cd .venv-zip/lib/python3.9/site-packages

- zip -r ../../../../lambda_handler.zip .

- cd ../../../../

- zip -g lambda_handler.zip *.py requirements.txt
Enter fullscreen mode Exit fullscreen mode

using the from_asset() method point to your zip file location

Top comments (0)