https://github.com/keithrozario/cdk-klayers
Klayers and CDK Python integration
https://github.com/keithrozario/cdk-klayers
Last synced: 6 months ago
JSON representation
Klayers and CDK Python integration
- Host: GitHub
- URL: https://github.com/keithrozario/cdk-klayers
- Owner: keithrozario
- Created: 2024-04-13T02:58:41.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-18T04:46:55.000Z (about 1 year ago)
- Last Synced: 2025-04-12T06:09:22.282Z (6 months ago)
- Language: Python
- Size: 69.3 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://www.python.org/downloads/release/python-3100/)
[](https://www.python.org/downloads/release/python-3110/)
[](https://www.python.org/downloads/release/python-3120/) [](https://github.com/psf/black)# CDK-Klayers
Use [Klayers](https://github.com/keithrozario/Klayers) within your CDK Stacks.
## Install
pip install cdk-klayers
## Usage (short version)
Simply use the Klayers Class within your CDK `Stack`. You will need to specify a runtime and region for it to work.
```python
from cdk_klayers import Klayers
class MockStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)runtime = aws_lambda.Runtime.PYTHON_3_12
# Initialize Klayers
klayers = Klayers(
self,
python_version = runtime
region = "ap-southeast-1"
)
# get the latest layer version for the requests package
requests_layer = klayers.layer_version(self, "requests")lambda_function = aws_lambda.Function(
self, 'HelloHandler',
runtime=runtime,
layers=[requests_layer],
code=aws_lambda.Code.from_asset('lambda'),
handler='hello.handler'
)```
## Usage (long version)
This example shows a fully working CDK stack that deploys a single lambda function with 2 layers from Klayers. The region of the stack was inferred from the CDK `Environment`.
```python
from aws_cdk import aws_lambda
from aws_cdk import App, Environment, Stack
from aws_cdk import Aws
from constructs import Constructfrom cdk_klayers import Klayers
class MockStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)runtime = aws_lambda.Runtime.PYTHON_3_12
# Initialize Klayers
klayers = Klayers(
self,
python_version = runtime
)
# get the latest layer version for the requests package
requests_layer = klayers.layer_version(self, "requests")
idna_layer = klayers.layer_version(self, "idna", layer_version="2")lambda_function = aws_lambda.Function(
self, 'HelloHandler',
runtime=runtime,
layers=[requests_layer, idna_layer],
code=aws_lambda.Code.from_asset('lambda'),
handler='hello.handler'
)app = App()
env = Environment(region="us-east-1")
mock_stack =MockStack(app, "test", env=env)
```## Other Notes
We're still in beta, this might change. Please raise an issue if you have any feedback.