in src/Aspire.Hosting.AWS/Lambda/LambdaExtensions.cs [123:166]
public static IResourceBuilder<LambdaEmulatorResource> AddAWSLambdaServiceEmulator(this IDistributedApplicationBuilder builder, LambdaEmulatorOptions? options = null)
{
options ??= new LambdaEmulatorOptions();
if (builder.Resources.FirstOrDefault(x => x.TryGetAnnotationsOfType<LambdaEmulatorAnnotation>(out _)) is ExecutableResource serviceEmulator)
{
throw new InvalidOperationException("A Lambda service emulator has already been added. The AddAWSLambdaFunction will add the emulator " +
"if it hasn't already been added. This method must be called before AddAWSLambdaFunction if the Lambda service emulator needs to be customized.");
}
builder.Services.TryAddSingleton<IProcessCommandService, ProcessCommandService>();
var lambdaEmulator = builder.AddResource(new LambdaEmulatorResource("LambdaServiceEmulator")).ExcludeFromManifest();
lambdaEmulator.WithArgs(context =>
{
lambdaEmulator.Resource.AddCommandLineArguments(context.Args);
});
var annotation = new EndpointAnnotation(
protocol: ProtocolType.Tcp,
uriScheme: "http",
port: options.Port);
lambdaEmulator.WithAnnotation(annotation);
var endpointReference = new EndpointReference(lambdaEmulator.Resource, annotation);
lambdaEmulator.WithAnnotation(new LambdaEmulatorAnnotation(endpointReference)
{
DisableAutoInstall = options.DisableAutoInstall,
OverrideMinimumInstallVersion = options.OverrideMinimumInstallVersion,
AllowDowngrade = options.AllowDowngrade,
});
lambdaEmulator.WithAnnotation(new EnvironmentCallbackAnnotation(context =>
{
context.EnvironmentVariables[Constants.IsAspireHostedEnvVariable] = "true";
context.EnvironmentVariables["LAMBDA_RUNTIME_API_PORT"] = endpointReference.Property(EndpointProperty.TargetPort);
}));
serviceEmulator = lambdaEmulator.Resource;
builder.Services.TryAddLifecycleHook<LambdaLifecycleHook>();
return lambdaEmulator;
}