in src/Aspire.Hosting.AWS/Lambda/SQSEventSourceExtensions.cs [84:115]
private static IResourceBuilder<LambdaProjectResource> WithSQSEventSource(IResourceBuilder<LambdaProjectResource> lambdaFunction, Func<ValueTask<string>> queueUrlResolver, SQSEventSourceOptions? options = null)
{
var sqsEventSourceResource = lambdaFunction.ApplicationBuilder.AddResource(new SQSEventSourceResource($"SQSEventSource-{lambdaFunction.Resource.Name}"))
.WithParentRelationship(lambdaFunction)
.ExcludeFromManifest();
sqsEventSourceResource.WithArgs(context =>
{
sqsEventSourceResource.Resource.AddCommandLineArguments(context.Args);
});
sqsEventSourceResource.WithEnvironment(async (context) =>
{
LambdaEmulatorAnnotation? lambdaEmulatorAnnotation = null;
if (lambdaFunction.ApplicationBuilder.Resources.FirstOrDefault(x => x.TryGetLastAnnotation<LambdaEmulatorAnnotation>(out lambdaEmulatorAnnotation)) == null ||
lambdaEmulatorAnnotation == null)
{
throw new InvalidOperationException("Lambda function is missing required annotations for Lambda emulator");
}
var queueUrl = await queueUrlResolver();
// Look to see if the Lambda function has been configured with an AWS SDK config. If so then
// configure the SQS event source with the same config to access the SQS queue.
var awsSdkConfig = lambdaFunction.Resource.Annotations.OfType<SDKResourceAnnotation>().FirstOrDefault()?.SdkConfig;
var sqsEventConfig = SQSEventSourceResource.CreateSQSEventConfig(queueUrl, lambdaFunction.Resource.Name, lambdaEmulatorAnnotation.Endpoint.Url, options, awsSdkConfig);
context.EnvironmentVariables[SQSEventSourceResource.SQS_EVENT_CONFIG_ENV_VAR] = sqsEventConfig;
});
return lambdaFunction;
}