# Kanject.Core.CloudFunction

The host foundation for AWS Lambda functions. Mark a `partial` class with `[CloudFunctionHost]` and the source generator builds the generic host, the Lambda serializer, and the configuration source for you — you override `ConfigureServices` to register dependencies. The built host is cached across invocations, so warm calls skip the setup cost.

## Install

```bash
dotnet add package Kanject.Core.CloudFunction.Provider.AwsLambda
```

Pulls in `Kanject.Core.CloudFunction.Abstractions` and the `Kanject.Core.CloudFunction.Aws.Annotations` generator transitively.

## Define the host

```csharp
using Kanject.Core.CloudFunction.Abstractions.Attributes;

// The Lambda host. [CloudFunctionHost] marks a partial class; the source
// generator makes it inherit the CloudFunction base and wires the host,
// the Lambda serializer, and the configuration source. You override
// ConfigureServices to register your dependencies.
[CloudFunctionHost(
    Provider = CloudProvider.AwsLambda,
    ConfigurationSource = ConfigurationSource.AwsSystemManagerParameterStore)]
public partial class Functions
{
    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IRetentionService, RetentionService>();
        // …the rest of your DI graph
    }
}
```

Your Lambda handler methods live on this same partial class and resolve their dependencies from the generated, cold-start-cached service provider. `ConfigurationSource.AwsSystemManagerParameterStore` layers Parameter Store values into configuration at startup — the same resolve-once, read-locally pattern the rest of Kanject uses.

## Lifecycle hooks

Beyond `ConfigureServices`, two optional overrides let you shape the host:

```csharp
// Optional lifecycle overrides on the same partial class:
public override void OnStartup(IConfigurationBuilder configurationBuilder)
{
    // add configuration providers before the host is built
}

public override void Configure(IServiceProvider serviceProvider)
{
    // run once after the host is built (warm-up, migrations, …)
}
```

## Host options

- `Provider` — the target cloud host (`CloudProvider.AwsLambda`)
- `ConfigurationSource` — e.g. `AwsSystemManagerParameterStore` to hydrate config from Parameter Store
- `EnableAot` — emit an AOT-friendly host for Native AOT Lambda packages
- `EmitLambdaSerializer` — generate the Lambda serializer registration (on by default)
- `EnablePrintInConsoleLogging` — structured console logging for the function
- `DebugEnvironment` / `DebugFetchSecretPathFromAppSettings` — local-debug configuration

> **Scheduling lives with queues:** There is no `[Schedule]` attribute here. Time-based triggers are declared on SQS queues in `Kanject.Core.Queue` with `[CronSchedule("0 2 * * ? *")]` / `[RateSchedule(...)]`, which provision EventBridge Scheduler rules. See [Kanject.Core.Queue](https://www.kanject.com/docs/core-queue/).

> **HTTP APIs on Lambda:** For a request/response web API on Lambda — like the Bean & Bark Orders API — the CLI's `webapi` template hosts ASP.NET Core under Lambda directly (AWS's `AddAWSLambdaHosting`). [Ship before the tasting](https://www.kanject.com/docs/cli-bean-bark-first-deploy/) walks through it. Reach for `CloudFunction` when you need a non-HTTP, event-driven function host.

---
_Source: https://www.kanject.com/docs/core-cloudfunction/ · Kanject Docs_
