|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from aws_cdk import aws_lambda as lambda_ |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from constructs import Construct |
| 10 | + |
| 11 | +from layer_v3.layer_constructors.helpers import construct_build_args |
| 12 | + |
| 13 | + |
| 14 | +class LambdaPowertoolsLayerPythonV3(lambda_.LayerVersion): |
| 15 | + """ |
| 16 | + A CDK Stack that creates a Lambda Layer for Powertools for AWS Lambda (Python) V3. |
| 17 | +
|
| 18 | + This stack creates a Lambda Layer containing the Powertools for AWS Lambda (Python) V3 library. |
| 19 | + It allows customization of the Python runtime version, inclusion of extra dependencies, |
| 20 | + architecture, Powertools version, and layer name. |
| 21 | +
|
| 22 | + Attributes: |
| 23 | + scope (Construct): The scope in which to define this construct. |
| 24 | + construct_id (str): The scoped construct ID. Must be unique amongst siblings in the same scope. |
| 25 | + python_version (lambda_.Runtime): The Python runtime version for the layer. Defaults to Python 3.12. |
| 26 | + include_extras (bool): Whether to include extra dependencies. Defaults to True. |
| 27 | + architecture (lambda_.Architecture): The compatible Lambda architecture. Defaults to x86_64. |
| 28 | + powertools_version (str): The version of Powertools to use. If empty, uses the latest version. |
| 29 | + layer_name (str): Custom name for the Lambda Layer. If empty, a default name will be used. |
| 30 | +
|
| 31 | + Example: |
| 32 | + >>> app = cdk.App() |
| 33 | + >>> LambdaPowertoolsLayerPythonV3(app, "PowertoolsLayer", |
| 34 | + ... python_version=lambda_.Runtime.PYTHON_3_11, |
| 35 | + ... include_extras=False, |
| 36 | + ... architecture=lambda_.Architecture.ARM_64, |
| 37 | + ... powertools_version="2.10.0", |
| 38 | + ... layer_name="MyCustomPowertoolsLayer") |
| 39 | +
|
| 40 | + """ |
| 41 | + |
| 42 | + def __init__( |
| 43 | + self, |
| 44 | + scope: Construct, |
| 45 | + construct_id: str, |
| 46 | + python_version: lambda_.Runtime = lambda_.Runtime.PYTHON_3_12, |
| 47 | + include_extras: bool = True, |
| 48 | + architecture: lambda_.Architecture = lambda_.Architecture.X86_64, |
| 49 | + powertools_version: str = "", |
| 50 | + layer_name: str = "", |
| 51 | + ) -> None: |
| 52 | + |
| 53 | + docker_file_path = str(Path(__file__).parent.parent / "docker") |
| 54 | + |
| 55 | + python_normalized_version: str = python_version.to_string().replace("python", "") |
| 56 | + |
| 57 | + if architecture.to_string() == "x86_64": |
| 58 | + docker_architecture: str = "linux/amd64" |
| 59 | + else: |
| 60 | + docker_architecture: str = "linux/arm64" |
| 61 | + |
| 62 | + super().__init__( |
| 63 | + scope, |
| 64 | + construct_id, |
| 65 | + code=lambda_.Code.from_docker_build( |
| 66 | + docker_file_path, |
| 67 | + build_args={ |
| 68 | + "PACKAGE_SUFFIX": construct_build_args( |
| 69 | + include_extras, |
| 70 | + powertools_version, |
| 71 | + ), |
| 72 | + "PYTHON_VERSION": python_normalized_version, |
| 73 | + }, |
| 74 | + platform=docker_architecture, |
| 75 | + ), |
| 76 | + layer_version_name=layer_name, |
| 77 | + license="MIT-0", |
| 78 | + compatible_runtimes=[python_version], |
| 79 | + description=f"Powertools for AWS Lambda (Python) V3 [{architecture.to_string()} - Python {python_normalized_version}]" # noqa E501 |
| 80 | + + (" with extra dependencies" if include_extras else "") |
| 81 | + + (f" version {powertools_version}" if powertools_version else " latest version"), |
| 82 | + compatible_architectures=[architecture] if architecture else None, |
| 83 | + ) |
0 commit comments