@@ -52,6 +52,7 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
52
52
Utils . ConfigureWebApplicationBuilder ( builder ) ;
53
53
54
54
builder . Services . AddApiGatewayEmulatorServices ( ) ;
55
+ builder . Services . AddSingleton < ILambdaClient , LambdaClient > ( ) ;
55
56
56
57
var serviceUrl = $ "http://{ settings . LambdaEmulatorHost } :{ settings . ApiGatewayEmulatorPort } ";
57
58
builder . WebHost . UseUrls ( serviceUrl ) ;
@@ -68,7 +69,7 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
68
69
app . Logger . LogInformation ( "The API Gateway Emulator is available at: {ServiceUrl}" , serviceUrl ) ;
69
70
} ) ;
70
71
71
- app . Map ( "/{**catchAll}" , async ( HttpContext context , IApiGatewayRouteConfigService routeConfigService ) =>
72
+ app . Map ( "/{**catchAll}" , async ( HttpContext context , IApiGatewayRouteConfigService routeConfigService , ILambdaClient lambdaClient ) =>
72
73
{
73
74
var routeConfig = routeConfigService . GetRouteConfig ( context . Request . Method , context . Request . Path ) ;
74
75
if ( routeConfig == null )
@@ -101,38 +102,56 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
101
102
PayloadStream = lambdaRequestStream
102
103
} ;
103
104
104
- using var lambdaClient = CreateLambdaServiceClient ( routeConfig , settings ) ;
105
- var response = await lambdaClient . InvokeAsync ( invokeRequest ) ;
106
-
107
- if ( response . FunctionError == null ) // response is successful
105
+ try
108
106
{
109
- if ( settings . ApiGatewayEmulatorMode . Equals ( ApiGatewayEmulatorMode . HttpV2 ) )
107
+ var endpoint = routeConfig . Endpoint ?? $ "http://{ settings . LambdaEmulatorHost } :{ settings . LambdaEmulatorPort } ";
108
+ var response = await lambdaClient . InvokeAsync ( invokeRequest , endpoint ) ;
109
+
110
+ if ( response . FunctionError == null ) // response is successful
110
111
{
111
- var lambdaResponse = response . ToApiGatewayHttpApiV2ProxyResponse ( ) ;
112
- await lambdaResponse . ToHttpResponseAsync ( context ) ;
112
+ if ( settings . ApiGatewayEmulatorMode . Equals ( ApiGatewayEmulatorMode . HttpV2 ) )
113
+ {
114
+ var lambdaResponse = response . ToApiGatewayHttpApiV2ProxyResponse ( ) ;
115
+ await lambdaResponse . ToHttpResponseAsync ( context ) ;
116
+ }
117
+ else
118
+ {
119
+ var lambdaResponse = response . ToApiGatewayProxyResponse ( settings . ApiGatewayEmulatorMode . Value ) ;
120
+ await lambdaResponse . ToHttpResponseAsync ( context , settings . ApiGatewayEmulatorMode . Value ) ;
121
+ }
113
122
}
114
123
else
115
124
{
116
- var lambdaResponse = response . ToApiGatewayProxyResponse ( settings . ApiGatewayEmulatorMode . Value ) ;
117
- await lambdaResponse . ToHttpResponseAsync ( context , settings . ApiGatewayEmulatorMode . Value ) ;
125
+ // For errors that happen within the function they still come back as 200 status code (they dont throw exception) but have FunctionError populated.
126
+ // Api gateway just displays them as an internal server error, so we convert them to the correct error response here.
127
+ if ( settings . ApiGatewayEmulatorMode . Equals ( ApiGatewayEmulatorMode . HttpV2 ) )
128
+ {
129
+ var lambdaResponse = InvokeResponseExtensions . ToHttpApiV2ErrorResponse ( ) ;
130
+ await lambdaResponse . ToHttpResponseAsync ( context ) ;
131
+ }
132
+ else
133
+ {
134
+ var lambdaResponse = InvokeResponseExtensions . ToApiGatewayErrorResponse ( settings . ApiGatewayEmulatorMode . Value ) ;
135
+ await lambdaResponse . ToHttpResponseAsync ( context , settings . ApiGatewayEmulatorMode . Value ) ;
136
+ }
118
137
}
119
138
}
120
- else
139
+ catch ( AmazonLambdaException e )
121
140
{
122
- // For function errors, api gateway just displays them as an internal server error, so we convert them to the correct error response here.
123
-
124
- if ( settings . ApiGatewayEmulatorMode . Equals ( ApiGatewayEmulatorMode . HttpV2 ) )
141
+ if ( e . ErrorCode == Exceptions . RequestEntityTooLargeException )
125
142
{
126
- var lambdaResponse = InvokeResponseExtensions . ToHttpApiV2ErrorResponse ( ) ;
127
- await lambdaResponse . ToHttpResponseAsync ( context ) ;
128
- }
129
- else
130
- {
131
- var lambdaResponse = InvokeResponseExtensions . ToApiGatewayErrorResponse ( settings . ApiGatewayEmulatorMode . Value ) ;
132
- await lambdaResponse . ToHttpResponseAsync ( context , settings . ApiGatewayEmulatorMode . Value ) ;
143
+ if ( settings . ApiGatewayEmulatorMode . Equals ( ApiGatewayEmulatorMode . HttpV2 ) )
144
+ {
145
+ var lambdaResponse = InvokeResponseExtensions . ToHttpApiV2RequestTooLargeResponse ( ) ;
146
+ await lambdaResponse . ToHttpResponseAsync ( context ) ;
147
+ }
148
+ else
149
+ {
150
+ var lambdaResponse = InvokeResponseExtensions . ToHttpApiRequestTooLargeResponse ( settings . ApiGatewayEmulatorMode . Value ) ;
151
+ await lambdaResponse . ToHttpResponseAsync ( context , settings . ApiGatewayEmulatorMode . Value ) ;
152
+ }
133
153
}
134
154
}
135
-
136
155
} ) ;
137
156
138
157
var runTask = app . RunAsync ( cancellationToken ) ;
@@ -144,30 +163,4 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
144
163
ServiceUrl = serviceUrl
145
164
} ;
146
165
}
147
-
148
- /// <summary>
149
- /// Creates an Amazon Lambda service client with the specified configuration.
150
- /// </summary>
151
- /// <param name="routeConfig">The API Gateway route configuration containing the endpoint information.
152
- /// If the endpoint is specified in routeConfig, it will be used as the service URL.</param>
153
- /// <param name="settings">The run command settings containing host and port information.
154
- /// If routeConfig endpoint is null, the service URL will be constructed using settings.Host and settings.Port.</param>
155
- /// <returns>An instance of IAmazonLambda configured with the specified endpoint and credentials.</returns>
156
- /// <remarks>
157
- /// The function uses hard-coded AWS credentials ("accessKey", "secretKey") for authentication since they are not actually being used.
158
- /// The service URL is determined by either:
159
- /// - Using routeConfig.Endpoint if it's not null
160
- /// - Combining settings.Host and settings.Port if routeConfig.Endpoint is null
161
- /// </remarks>
162
- private static IAmazonLambda CreateLambdaServiceClient ( ApiGatewayRouteConfig routeConfig , RunCommandSettings settings )
163
- {
164
- var endpoint = routeConfig . Endpoint ?? $ "http://{ settings . LambdaEmulatorHost } :{ settings . LambdaEmulatorPort } ";
165
-
166
- var lambdaConfig = new AmazonLambdaConfig
167
- {
168
- ServiceURL = endpoint
169
- } ;
170
-
171
- return new AmazonLambdaClient ( new Amazon . Runtime . BasicAWSCredentials ( "accessKey" , "secretKey" ) , lambdaConfig ) ;
172
- }
173
166
}
0 commit comments