Skip to content

Use default host and port for lambdaconfig if routeconfig endpoint is not specified #1941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
PayloadStream = lambdaRequestStream
};

using var lambdaClient = CreateLambdaServiceClient(routeConfig);
using var lambdaClient = CreateLambdaServiceClient(routeConfig, settings);
var response = await lambdaClient.InvokeAsync(invokeRequest);

if (response.FunctionError != null)
Expand All @@ -111,13 +111,11 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
{
var lambdaResponse = response.ToApiGatewayHttpApiV2ProxyResponse();
await lambdaResponse.ToHttpResponseAsync(context);
return;
}
else
{
var lambdaResponse = response.ToApiGatewayProxyResponse(settings.ApiGatewayEmulatorMode.Value);
await lambdaResponse.ToHttpResponseAsync(context, settings.ApiGatewayEmulatorMode.Value);
return;
}
});

Expand All @@ -131,12 +129,27 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
};
}

private static IAmazonLambda CreateLambdaServiceClient(ApiGatewayRouteConfig routeConfig)
/// <summary>
/// Creates an Amazon Lambda service client with the specified configuration.
/// </summary>
/// <param name="routeConfig">The API Gateway route configuration containing the endpoint information.
/// If the endpoint is specified in routeConfig, it will be used as the service URL.</param>
/// <param name="settings">The run command settings containing host and port information.
/// If routeConfig endpoint is null, the service URL will be constructed using settings.Host and settings.Port.</param>
/// <returns>An instance of IAmazonLambda configured with the specified endpoint and credentials.</returns>
/// <remarks>
/// The function uses hard-coded AWS credentials ("accessKey", "secretKey") for authentication since they are not actually being used.
/// The service URL is determined by either:
/// - Using routeConfig.Endpoint if it's not null
/// - Combining settings.Host and settings.Port if routeConfig.Endpoint is null
/// </remarks>
private static IAmazonLambda CreateLambdaServiceClient(ApiGatewayRouteConfig routeConfig, RunCommandSettings settings)
{
// TODO: Handle routeConfig.Endpoint to null and use the settings versions of runtime.
var endpoint = routeConfig.Endpoint ?? $"http://{settings.LambdaEmulatorHost}:{settings.LambdaEmulatorPort}";

var lambdaConfig = new AmazonLambdaConfig
{
ServiceURL = routeConfig.Endpoint
ServiceURL = endpoint
};

return new AmazonLambdaClient(new Amazon.Runtime.BasicAWSCredentials("accessKey", "secretKey"), lambdaConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public async Task TestLambdaToUpperV2()
finally
{
await cancellationTokenSource.CancelAsync();
await CleanupProcesses();
}
}

Expand Down Expand Up @@ -97,7 +96,6 @@ public async Task TestLambdaToUpperRest()
finally
{
await cancellationTokenSource.CancelAsync();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is already callled in DisposeAsync which is why i removed it here

await CleanupProcesses();
}
}

Expand Down Expand Up @@ -133,7 +131,6 @@ public async Task TestLambdaToUpperV1()
finally
{
await cancellationTokenSource.CancelAsync();
await CleanupProcesses();
}
}

Expand Down Expand Up @@ -175,7 +172,6 @@ public async Task TestLambdaBinaryResponse()
finally
{
await cancellationTokenSource.CancelAsync();
await CleanupProcesses();
}
}

Expand Down Expand Up @@ -211,7 +207,39 @@ public async Task TestLambdaReturnString()
finally
{
await cancellationTokenSource.CancelAsync();
await CleanupProcesses();
}
}

[Fact]
public async Task TestLambdaWithNullEndpoint()
{
var testProjectDir = Path.GetFullPath("../../../../../testapps");
var config = new TestConfig
{
TestToolPath = Path.GetFullPath(Path.Combine(testProjectDir, "../src/Amazon.Lambda.TestTool")),
LambdaPath = Path.GetFullPath(Path.Combine(testProjectDir, "LambdaTestFunctionV2")),
FunctionName = "LambdaTestFunctionV2",
RouteName = "testfunction",
HttpMethod = "Post"
};

var cancellationTokenSource = new CancellationTokenSource();

try
{
StartTestToolProcessWithNullEndpoint(ApiGatewayEmulatorMode.HttpV2, Constants.DefaultApiGatewayEmulatorPort, config, cancellationTokenSource);
await WaitForGatewayHealthCheck(Constants.DefaultApiGatewayEmulatorPort);
await StartLambdaProcess(config, Constants.DefaultLambdaEmulatorPort);

var response = await TestEndpoint(config, Constants.DefaultApiGatewayEmulatorPort );
var responseContent = await response.Content.ReadAsStringAsync();

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("HELLO WORLD", responseContent);
}
finally
{
await cancellationTokenSource.CancelAsync();
}
}

Expand All @@ -236,6 +264,24 @@ private async Task<HttpResponseMessage> TestEndpoint(TestConfig config, int apiG
};
}

private void StartTestToolProcessWithNullEndpoint(ApiGatewayEmulatorMode apiGatewayMode, int apiGatewayPort, TestConfig config, CancellationTokenSource cancellationTokenSource)
{
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
Environment.SetEnvironmentVariable("APIGATEWAY_EMULATOR_ROUTE_CONFIG", $@"{{
""LambdaResourceName"": ""{config.RouteName}"",
""HttpMethod"": ""{config.HttpMethod}"",
""Path"": ""/{config.RouteName}""
}}");

cancellationTokenSource.CancelAfter(5000);
var settings = new RunCommandSettings { NoLaunchWindow = true, ApiGatewayEmulatorMode = apiGatewayMode, ApiGatewayEmulatorPort = apiGatewayPort};

var command = new RunCommand(_mockInteractiveService.Object, _mockEnvironmentManager.Object);
var context = new CommandContext(new List<string>(), _mockRemainingArgs.Object, "run", null);

_ = command.ExecuteAsync(context, settings, cancellationTokenSource);
}

private void StartTestToolProcess(ApiGatewayEmulatorMode apiGatewayMode, TestConfig config, int lambdaPort, int apiGatewayPort, CancellationTokenSource cancellationTokenSource)
{
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
Expand Down
Loading