Class LambdaInterceptor
- All Implemented Interfaces:
IInterceptor,software.amazon.jsii.JsiiSerializable
Interceptors allow you to run custom code during each invocation of your gateway:
- REQUEST interceptors execute before calling the target
- RESPONSE interceptors execute after the target responds
Example:
// Create Lambda functions for interceptors
Function requestInterceptorFn = Function.Builder.create(this, "RequestInterceptor")
.runtime(Runtime.PYTHON_3_12)
.handler("index.handler")
.code(Code.fromInline("\ndef handler(event, context):\n # Validate and transform request\n return {\n \"interceptorOutputVersion\": \"1.0\",\n \"mcp\": {\n \"transformedGatewayRequest\": event[\"mcp\"][\"gatewayRequest\"]\n }\n }\n "))
.build();
Function responseInterceptorFn = Function.Builder.create(this, "ResponseInterceptor")
.runtime(Runtime.PYTHON_3_12)
.handler("index.handler")
.code(Code.fromInline("\ndef handler(event, context):\n # Filter or transform response\n return {\n \"interceptorOutputVersion\": \"1.0\",\n \"mcp\": {\n \"transformedGatewayResponse\": event[\"mcp\"][\"gatewayResponse\"]\n }\n }\n "))
.build();
// Create gateway with interceptors
Gateway gateway = Gateway.Builder.create(this, "MyGateway")
.gatewayName("my-gateway")
.interceptorConfigurations(List.of(LambdaInterceptor.forRequest(requestInterceptorFn, InterceptorOptions.builder()
.passRequestHeaders(true)
.build()), LambdaInterceptor.forResponse(responseInterceptorFn)))
.build();
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class software.amazon.jsii.JsiiObject
software.amazon.jsii.JsiiObject.InitializationModeNested classes/interfaces inherited from interface software.amazon.awscdk.services.bedrockagentcore.IInterceptor
IInterceptor.Jsii$Default, IInterceptor.Jsii$Proxy -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedLambdaInterceptor(software.amazon.jsii.JsiiObject.InitializationMode initializationMode) protectedLambdaInterceptor(software.amazon.jsii.JsiiObjectRef objRef) -
Method Summary
Modifier and TypeMethodDescriptionBinds this Lambda interceptor to a Gateway.static LambdaInterceptorforRequest(IFunction lambdaFunction) Create a REQUEST interceptor that executes before the gateway calls the target.static LambdaInterceptorforRequest(IFunction lambdaFunction, InterceptorOptions options) Create a REQUEST interceptor that executes before the gateway calls the target.static LambdaInterceptorforResponse(IFunction lambdaFunction) Create a RESPONSE interceptor that executes after the target responds.static LambdaInterceptorforResponse(IFunction lambdaFunction, InterceptorOptions options) Create a RESPONSE interceptor that executes after the target responds.The interception point (REQUEST or RESPONSE).Methods inherited from class software.amazon.jsii.JsiiObject
jsiiAsyncCall, jsiiAsyncCall, jsiiCall, jsiiCall, jsiiGet, jsiiGet, jsiiSet, jsiiStaticCall, jsiiStaticCall, jsiiStaticGet, jsiiStaticGet, jsiiStaticSet, jsiiStaticSetMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface software.amazon.jsii.JsiiSerializable
$jsii$toJson
-
Constructor Details
-
LambdaInterceptor
protected LambdaInterceptor(software.amazon.jsii.JsiiObjectRef objRef) -
LambdaInterceptor
protected LambdaInterceptor(software.amazon.jsii.JsiiObject.InitializationMode initializationMode)
-
-
Method Details
-
forRequest
@Stability(Stable) @NotNull public static LambdaInterceptor forRequest(@NotNull IFunction lambdaFunction, @Nullable InterceptorOptions options) Create a REQUEST interceptor that executes before the gateway calls the target.Important: When this interceptor is added to a gateway, the gateway's IAM role will automatically be granted
lambda:InvokeFunctionpermission on the specified Lambda function.- Parameters:
lambdaFunction- The Lambda function to invoke. This parameter is required.options- Optional configuration for the interceptor.- Returns:
- A configured LambdaInterceptor for request interception
-
forRequest
@Stability(Stable) @NotNull public static LambdaInterceptor forRequest(@NotNull IFunction lambdaFunction) Create a REQUEST interceptor that executes before the gateway calls the target.Important: When this interceptor is added to a gateway, the gateway's IAM role will automatically be granted
lambda:InvokeFunctionpermission on the specified Lambda function.- Parameters:
lambdaFunction- The Lambda function to invoke. This parameter is required.- Returns:
- A configured LambdaInterceptor for request interception
-
forResponse
@Stability(Stable) @NotNull public static LambdaInterceptor forResponse(@NotNull IFunction lambdaFunction, @Nullable InterceptorOptions options) Create a RESPONSE interceptor that executes after the target responds.Important: When this interceptor is added to a gateway, the gateway's IAM role will automatically be granted
lambda:InvokeFunctionpermission on the specified Lambda function.- Parameters:
lambdaFunction- The Lambda function to invoke. This parameter is required.options- Optional configuration for the interceptor.- Returns:
- A configured LambdaInterceptor for response interception
-
forResponse
@Stability(Stable) @NotNull public static LambdaInterceptor forResponse(@NotNull IFunction lambdaFunction) Create a RESPONSE interceptor that executes after the target responds.Important: When this interceptor is added to a gateway, the gateway's IAM role will automatically be granted
lambda:InvokeFunctionpermission on the specified Lambda function.- Parameters:
lambdaFunction- The Lambda function to invoke. This parameter is required.- Returns:
- A configured LambdaInterceptor for response interception
-
bind
@Stability(Stable) @NotNull public InterceptorBindConfig bind(@NotNull software.constructs.Construct _scope, @NotNull IGateway gateway) Binds this Lambda interceptor to a Gateway.This method:
- Grants the Gateway's IAM role permission to invoke the Lambda function
- Returns the CloudFormation configuration for this interceptor
- Specified by:
bindin interfaceIInterceptor- Parameters:
_scope- The construct scope (currently unused, reserved for future extensions). This parameter is required.gateway- The gateway this interceptor is being bound to. This parameter is required.- Returns:
- Configuration for CloudFormation rendering
-
getInterceptionPoint
The interception point (REQUEST or RESPONSE).- Specified by:
getInterceptionPointin interfaceIInterceptor
-