Skip to content
Open
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
11 changes: 11 additions & 0 deletions .autover/changes/27264eba-d94e-4190-a5ab-91c895db6b45.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Projects": [
{
"Name": "Amazon.Lambda.TestTool",
"Type": "Patch",
"ChangelogMessages": [
"Add support for configuring HTTPS endpoints for the Lambda UI emulator and API Gateway emulator"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public sealed class RunCommand(
IToolInteractiveService toolInteractiveService, IEnvironmentManager environmentManager) : CancellableAsyncCommand<RunCommandSettings>
{
public const string LAMBDA_RUNTIME_API_PORT = "LAMBDA_RUNTIME_API_PORT";
public const string LAMBDA_WEB_UI_HTTPS_PORT = "LAMBDA_WEB_UI_HTTPS_PORT";
public const string API_GATEWAY_EMULATOR_PORT = "API_GATEWAY_EMULATOR_PORT";
public const string API_GATEWAY_EMULATOR_HTTPS_PORT = "API_GATEWAY_EMULATOR_HTTPS_PORT";

/// <summary>
/// Task for the Lambda Runtime API.
Expand All @@ -37,10 +39,10 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCommandS
{
EvaluateEnvironmentVariables(settings);

if (!settings.LambdaEmulatorPort.HasValue && !settings.ApiGatewayEmulatorPort.HasValue && string.IsNullOrEmpty(settings.SQSEventSourceConfig))
if (!settings.LambdaEmulatorPort.HasValue && !settings.ApiGatewayEmulatorPort.HasValue && !settings.ApiGatewayEmulatorHttpsPort.HasValue && string.IsNullOrEmpty(settings.SQSEventSourceConfig))
{
throw new ArgumentException("At least one of the following parameters must be set: " +
"--lambda-emulator-port, --api-gateway-emulator-port or --sqs-eventsource-config");
"--lambda-emulator-port, --api-gateway-emulator-port, --api-gateway-emulator-https-port or --sqs-eventsource-config");
}

var tasks = new List<Task>();
Expand Down Expand Up @@ -69,11 +71,11 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCommandS
}
}

if (settings.ApiGatewayEmulatorPort.HasValue)
if (settings.ApiGatewayEmulatorPort.HasValue || settings.ApiGatewayEmulatorHttpsPort.HasValue)
{
if (settings.ApiGatewayEmulatorMode is null)
{
throw new ArgumentException("When --api-gateway-emulator-port is set the --api-gateway-emulator-mode must be set to configure the mode for the API Gateway emulator.");
throw new ArgumentException("When --api-gateway-emulator-port or --api-gateway-emulator-https-port is set the --api-gateway-emulator-mode must be set to configure the mode for the API Gateway emulator.");
}

var apiGatewayEmulatorProcess =
Expand Down Expand Up @@ -137,6 +139,18 @@ private void EvaluateEnvironmentVariables(RunCommandSettings settings)
throw new ArgumentException($"Value for {LAMBDA_RUNTIME_API_PORT} environment variable was not a valid port number");
}
}
if (environmentVariables.Contains(LAMBDA_WEB_UI_HTTPS_PORT))
{
var envValue = environmentVariables[LAMBDA_WEB_UI_HTTPS_PORT]?.ToString();
if (int.TryParse(envValue, out var port))
{
settings.LambdaEmulatorHttpsPort = port;
}
else
{
throw new ArgumentException($"Value for {LAMBDA_WEB_UI_HTTPS_PORT} environment variable was not a valid port number");
}
}
if (environmentVariables.Contains(API_GATEWAY_EMULATOR_PORT))
{
var envValue = environmentVariables[API_GATEWAY_EMULATOR_PORT]?.ToString();
Expand All @@ -149,6 +163,18 @@ private void EvaluateEnvironmentVariables(RunCommandSettings settings)
throw new ArgumentException($"Value for {API_GATEWAY_EMULATOR_PORT} environment variable was not a valid port number");
}
}
if (environmentVariables.Contains(API_GATEWAY_EMULATOR_HTTPS_PORT))
{
var envValue = environmentVariables[API_GATEWAY_EMULATOR_HTTPS_PORT]?.ToString();
if (int.TryParse(envValue, out var port))
{
settings.ApiGatewayEmulatorHttpsPort = port;
}
else
{
throw new ArgumentException($"Value for {API_GATEWAY_EMULATOR_HTTPS_PORT} environment variable was not a valid port number");
}
}

if (settings.SQSEventSourceConfig != null && settings.SQSEventSourceConfig.StartsWith(Constants.ArgumentEnvironmentVariablePrefix, StringComparison.CurrentCultureIgnoreCase))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public sealed class RunCommandSettings : CommandSettings
[Description("The port number used for the test tool's web interface.")]
public int? LambdaEmulatorPort { get; set; }

/// <summary>
/// The https port number used for the test tool's web interface. This is only used for the web UI. Lambda functions making REST calls to the Lambda Runtime API
/// always use http configured by the port specified in <see cref="LambdaEmulatorPort"/>. To use HTTPS the environment must be configured with certs
/// for the host specified in <see cref="LambdaEmulatorHost"/>.
/// </summary>
[CommandOption("--lambda-emulator-https-port <PORT>")]
[Description("The https port number used for the test tool's web interface.")]
public int? LambdaEmulatorHttpsPort { get; set; }

/// <summary>
/// Disable auto launching the test tool's web interface in a browser.
/// </summary>
Expand All @@ -49,12 +58,22 @@ public sealed class RunCommandSettings : CommandSettings
public ApiGatewayEmulatorMode? ApiGatewayEmulatorMode { get; set; }

/// <summary>
/// The port number used for the test tool's API Gateway emulator. If a port is specified the API Gateway emulator will be started. The --api-gateway-mode muse also be set when setting the API Gateway emulator port.
/// The port number used for the test tool's API Gateway emulator. If a port is specified the API Gateway emulator will be started. The --api-gateway-emulator-mode
/// must also be set when setting the API Gateway emulator port.
/// </summary>
[CommandOption("--api-gateway-emulator-port <PORT>")]
[Description("The port number used for the test tool's API Gateway emulator.")]
public int? ApiGatewayEmulatorPort { get; set; }

/// <summary>
/// The https port number used for the test tool's API Gateway emulator. If a port is specified the API Gateway emulator will be started. The --api-gateway--emulator-mode must
/// also be set when setting the API Gateway emulator port. To use HTTPS the environment must be configured with certs
/// for the host specified in <see cref="LambdaEmulatorHost"/>.
/// </summary>
[CommandOption("--api-gateway-emulator-https-port <PORT>")]
[Description("The https port number used for the test tool's API Gateway emulator.")]
public int? ApiGatewayEmulatorHttpsPort { get; set; }

/// <summary>
/// The configuration for the SQS event source. The format of the config is a comma delimited key pairs. For example \"QueueUrl=queue-url,FunctionName=function-name,VisibilityTimeout=100\".
/// Possible keys are: BatchSize, DisableMessageDelete, FunctionName, LambdaRuntimeApi, Profile, QueueUrl, Region, VisibilityTimeout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,22 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
builder.Services.AddApiGatewayEmulatorServices();
builder.Services.AddSingleton<ILambdaClient, LambdaClient>();

var serviceUrl = $"http://{settings.LambdaEmulatorHost}:{settings.ApiGatewayEmulatorPort}";
builder.WebHost.UseUrls(serviceUrl);
string? serviceHttpUrl = null;
string? serviceHttpsUrl = null;
var serviceUrls = new List<string>();

if (settings.ApiGatewayEmulatorPort.HasValue)
{
serviceHttpUrl = $"http://{settings.LambdaEmulatorHost}:{settings.ApiGatewayEmulatorPort}";
serviceUrls.Add(serviceHttpUrl);
}
if (settings.ApiGatewayEmulatorHttpsPort.HasValue)
{
serviceHttpsUrl = $"https://{settings.LambdaEmulatorHost}:{settings.ApiGatewayEmulatorHttpsPort}";
serviceUrls.Add(serviceHttpsUrl);
}

builder.WebHost.UseUrls(serviceUrls.ToArray());
builder.WebHost.SuppressStatusMessages(true);

builder.Services.AddHealthChecks();
Expand All @@ -66,7 +80,7 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can

app.Lifetime.ApplicationStarted.Register(() =>
{
app.Logger.LogInformation("The API Gateway Emulator is available at: {ServiceUrl}", serviceUrl);
app.Logger.LogInformation("The API Gateway Emulator is available at: {ServiceUrl}", serviceHttpsUrl ?? serviceHttpUrl);
});

app.Map("/{**catchAll}", async (HttpContext context, IApiGatewayRouteConfigService routeConfigService, ILambdaClient lambdaClient) =>
Expand Down Expand Up @@ -160,7 +174,7 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
{
Services = app.Services,
RunningTask = runTask,
ServiceUrl = serviceUrl
ServiceUrl = serviceHttpsUrl ?? serviceHttpUrl ?? throw new InvalidOperationException("No valid service URL was configured for the API Gateway emulator.")
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,20 @@ public static TestToolProcess Startup(RunCommandSettings settings, CancellationT
}
builder.Services.AddSingleton<IDirectoryManager, DirectoryManager>();

var serviceUrl = $"http://{settings.LambdaEmulatorHost}:{settings.LambdaEmulatorPort}";
builder.WebHost.UseUrls(serviceUrl);
var serviceHttp = $"http://{settings.LambdaEmulatorHost}:{settings.LambdaEmulatorPort}";

string? serviceHttps = null;

if (settings.LambdaEmulatorHttpsPort.HasValue)
{
serviceHttps = $"https://{settings.LambdaEmulatorHost}:{settings.LambdaEmulatorHttpsPort}";
builder.WebHost.UseUrls(serviceHttp, serviceHttps);
}
else
{
builder.WebHost.UseUrls(serviceHttp);
}

builder.WebHost.SuppressStatusMessages(true);

builder.Services.AddSingleton<IGlobalSettingsRepository, FileSettingsRepository>();
Expand Down Expand Up @@ -102,7 +114,7 @@ public static TestToolProcess Startup(RunCommandSettings settings, CancellationT
{
Services = app.Services,
RunningTask = runTask,
ServiceUrl = serviceUrl
ServiceUrl = serviceHttps ?? serviceHttp
};

return startup;
Expand Down