in src/Aspire.Hosting.AWS/Lambda/LambdaExtensions.cs [32:112]
public static IResourceBuilder<LambdaProjectResource> AddAWSLambdaFunction<TLambdaProject>(this IDistributedApplicationBuilder builder, string name, string lambdaHandler, LambdaFunctionOptions? options = null) where TLambdaProject : IProjectMetadata, new()
{
options ??= new LambdaFunctionOptions();
var metadata = new TLambdaProject();
var serviceEmulator = AddOrGetLambdaServiceEmulatorResource(builder);
IResourceBuilder<LambdaProjectResource> resource;
// The Lambda function handler for a Class Library contains "::".
// This is an example of a class library function handler "WebCalculatorFunctions::WebCalculatorFunctions.Functions::AddFunctionHandler".
if (lambdaHandler.Contains("::") && AspireUtilities.IsRunningInDebugger)
{
// If we are running Aspire through an IDE where a debugger is attached,
// we want to configure the Aspire resource to use a Launch Setting Profile that will be able to run the class library Lambda function.
var project = new LambdaProjectResource(name);
resource = builder.AddResource(project)
.WithAnnotation(new LaunchProfileAnnotation($"{Constants.LaunchSettingsNodePrefix}{name}"))
.WithAnnotation(new TLambdaProject());
}
else
{
var project = new LambdaProjectResource(name);
resource = builder.AddResource(project)
.WithAnnotation(new TLambdaProject());
}
resource.WithParentRelationship(serviceEmulator);
resource.WithOpenTelemetry();
resource.WithEnvironment(context =>
{
var serviceEmulatorEndpoint = serviceEmulator.GetEndpoint("http");
// Add the Lambda function resource on the path so the emulator can distinguish request
// for each Lambda function.
var apiPath = $"{serviceEmulatorEndpoint.Host}:{serviceEmulatorEndpoint.Port}/{name}";
context.EnvironmentVariables["AWS_EXECUTION_ENV"] = $"aspire.hosting.aws#{SdkUtilities.GetAssemblyVersion()}";
context.EnvironmentVariables["AWS_LAMBDA_RUNTIME_API"] = apiPath;
context.EnvironmentVariables["AWS_LAMBDA_FUNCTION_NAME"] = name;
context.EnvironmentVariables["_HANDLER"] = lambdaHandler;
context.EnvironmentVariables["AWS_LAMBDA_LOG_FORMAT"] = options.LogFormat.Value;
context.EnvironmentVariables["AWS_LAMBDA_LOG_LEVEL"] = options.ApplicationLogLevel.Value;
var lambdaEmulatorEndpoint = $"http://{serviceEmulatorEndpoint.Host}:{serviceEmulatorEndpoint.Port}/?function={Uri.EscapeDataString(name)}";
resource.WithAnnotation(new ResourceCommandAnnotation(
name: "LambdaEmulator",
displayName: "Lambda Service Emulator",
updateState: context =>
{
if (string.Equals(context.ResourceSnapshot.State?.Text, KnownResourceStates.Running))
{
return ResourceCommandState.Enabled;
}
return ResourceCommandState.Disabled;
},
executeCommand: context =>
{
var startInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = lambdaEmulatorEndpoint
};
Process.Start(startInfo);
return Task.FromResult(CommandResults.Success());
},
displayDescription: "Open the Lambda service emulator configured for this Lambda function",
parameter: null,
confirmationMessage: null,
iconName: "Bug",
iconVariant: IconVariant.Filled,
isHighlighted: true)
);
});
resource.WithAnnotation(new LambdaFunctionAnnotation(lambdaHandler));
return resource;
}