Skip to content

Commit

Permalink
feat: support configurable runtimes (#52)
Browse files Browse the repository at this point in the history
* feat: support configurable runtimes

* fix: lint
  • Loading branch information
JensRoland authored Dec 21, 2024
1 parent 4ad3539 commit fcb394d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/kegstandcli/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def find_config_file(verbose: bool, config_file: str | None) -> str | None: # n
return config_file


def get_kegstand_config(verbose: bool, project_dir: str, config_file: str) -> dict[str, Any]:
def get_kegstand_config(verbose: bool, project_dir: str, config_file: str) -> dict[str, Any]: # noqa: PLR0912
"""Load and parse the Kegstand configuration.
Args:
Expand All @@ -58,15 +58,23 @@ def get_kegstand_config(verbose: bool, project_dir: str, config_file: str) -> di
# If the config file is pyproject.toml, the config will be under the 'tool.kegstand' key
if config_file.endswith("pyproject.toml"):
config = parsed_toml_config.get("tool", {}).get("kegstand", {})
# Some keys are used from the [project] section if not specified in [tool.kegstand]
# Some keys are used from the [project] or [tool.poetry] section
# if not specified in [tool.kegstand]
properties_from_pyproject = ["name", "description", "version"]
if "project" not in config:
config["project"] = {}
for property_name in properties_from_pyproject:
if property_name not in config["project"]:
config["project"][property_name] = parsed_toml_config.get("project", {}).get(
property_name
)
# Try to get the property from the [project] section
if property_name in parsed_toml_config.get("project", {}):
config["project"][property_name] = parsed_toml_config.get("project", {}).get(
property_name
)
# Try to get the property from the [tool.poetry] section
elif property_name in parsed_toml_config.get("tool", {}).get("poetry", {}):
config["project"][property_name] = (
parsed_toml_config.get("tool", {}).get("poetry", {}).get(property_name)
)
else:
config = parsed_toml_config

Expand All @@ -81,7 +89,9 @@ def get_kegstand_config(verbose: bool, project_dir: str, config_file: str) -> di
config["config_file"] = str(config_path)

# Set defaults where missing
config_defaults = {"api": {"name": "Untitled API", "entrypoint": "api.lambda.handler"}}
config_defaults = {
"api": {"name": "Untitled API", "entrypoint": "api.lambda.handler", "runtime": "python3.13"}
}
for section, defaults in config_defaults.items():
if section not in config:
config[section] = {}
Expand Down
4 changes: 2 additions & 2 deletions src/kegstandcli/infra/stacks/rest_api_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from aws_cdk import aws_lambda as lambda_
from constructs import Construct

from kegstandcli.utils import find_resource_modules
from kegstandcli.utils import LambdaRuntime, find_resource_modules

MODULE_CONFIG_KEY = "api"

Expand Down Expand Up @@ -66,7 +66,7 @@ def __init__(
self,
f"{construct_id}-Backend",
function_name=f"{construct_id}-Function",
runtime=lambda_.Runtime.PYTHON_3_9,
runtime=LambdaRuntime(config[MODULE_CONFIG_KEY]["runtime"]).to_lambda_runtime(),
handler=config[MODULE_CONFIG_KEY]["entrypoint"],
code=lambda_.Code.from_asset(str(api_src_path)),
layers=[ # See Lambda Powertools: https://awslabs.github.io/aws-lambda-powertools-python/2.4.0/
Expand Down
21 changes: 21 additions & 0 deletions src/kegstandcli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any

import click
from aws_cdk import aws_lambda as lambda_
from tomlkit import loads as parse_toml


Expand All @@ -18,6 +19,26 @@ def __str__(self) -> str:
return self.value


class LambdaRuntime(str, PythonEnum):
"""Lambda runtime types."""

PYTHON_3_10 = "python3.10"
PYTHON_3_11 = "python3.11"
PYTHON_3_12 = "python3.12"
PYTHON_3_13 = "python3.13"

def __str__(self) -> str:
return self.value

def to_lambda_runtime(self) -> lambda_.Runtime:
return {
LambdaRuntime.PYTHON_3_10: lambda_.Runtime.PYTHON_3_10,
LambdaRuntime.PYTHON_3_11: lambda_.Runtime.PYTHON_3_11,
LambdaRuntime.PYTHON_3_12: lambda_.Runtime.PYTHON_3_12,
LambdaRuntime.PYTHON_3_13: lambda_.Runtime.PYTHON_3_13,
}[self]


def find_resource_modules(api_src_dir: str) -> list[dict[str, Any]]:
"""Find API resource modules in the source directory.
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fcb394d

Please sign in to comment.