LambdaApi
LambdaApi is the recommended CDK interface. It discovers decorated handlers, creates or uses an API Gateway API, and delegates configuration to LambdaApiConfig.
Constructor
Class signature: LambdaApi(scope, construct_id, *, lambda_path, source_layout=SourceLayout.ROOT, layers_path=None, api=None, api_type=None, config=None)
Bases: Construct
Example:
from aws_cdk import Stack
from constructs import Construct
from lambda_api_decorators_cdk import LambdaApi, LambdaApiConfig
class ApiStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
self.api = LambdaApi(
self, "Api", lambda_path="lambdas", config=LambdaApiConfig()
)
HTTP API example:
from lambda_api_decorators_cdk import ApiType, LambdaApi, SourceLayout
api = LambdaApi(
self,
"HttpApi",
lambda_path="lambdas",
api_type=ApiType.HTTP,
source_layout=SourceLayout.SERVICE,
layers_path="layers",
)
Parameters:
-
scope(Construct) – CDK construct scope. -
construct_id(str) – Identifier for this construct. -
lambda_path(str) – Path to the directory containing discovered handlers. -
source_layout(SourceLayout) – SourceLayout.ROOT or SourceLayout.SERVICE. Default: SourceLayout.ROOT. -
layers_path(Optional[str]) – Optional directory for layer source discovery. Default: None. -
api(Optional[Union[apigateway.IRestApi, apigatewayv2.HttpApi]]) – Existing REST IRestApi or HttpApi. If omitted, an API is created. -
api_type(Optional[ApiType]) – ApiType.REST or ApiType.HTTP. Default: inferred fromapi, or REST when no API is supplied. -
config(Optional[LambdaApiConfig]) – Configuration object for defaults and registries. Default: a new empty LambdaApiConfig.
source_layout, api_type, and config are type-checked. A supplied API must be a REST API implementation or HttpApi; an unsupported object raises TypeError. If api and api_type identify different families, construction raises ValueError. An omitted API produces RestApi for REST and HttpApi for HTTP.
REST routes use the declarations exported by lambda-api-decorators, including ANY. HTTP routes use the explicit GET, POST, PUT, and DELETE declarations; a discovered ANY route is not mapped by the current HTTP builder.
Attributes
api
api -> Union[apigateway.IRestApi, apigatewayv2.HttpApi] returns the supplied or created API object.
api_type
api_type -> ApiType returns the resolved API family.
See Getting Started for a complete deployable project.
When reusing an existing API, pass it through api; api_type may be omitted
because the construct infers REST or HTTP from that object. Supplying a
conflicting api_type raises ValueError.