LambdaInterceptor Class
A Lambda-based interceptor for Gateway.
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
| Source | aws-bedrockagentcore/lib/gateway/interceptor.ts |
|---|---|
| Type last changed |
Example
ts
// Create Lambda functions for interceptors
const requestInterceptorFn = new lambda.Function(this, "RequestInterceptor", {
runtime: lambda.Runtime.PYTHON_3_12,
handler: "index.handler",
code: lambda.Code.fromInline(`
def handler(event, context):
# Validate and transform request
return {
"interceptorOutputVersion": "1.0",
"mcp": {
"transformedGatewayRequest": event["mcp"]["gatewayRequest"]
}
}
`),
});
const responseInterceptorFn = new lambda.Function(this, "ResponseInterceptor", {
runtime: lambda.Runtime.PYTHON_3_12,
handler: "index.handler",
code: lambda.Code.fromInline(`
def handler(event, context):
# Filter or transform response
return {
"interceptorOutputVersion": "1.0",
"mcp": {
"transformedGatewayResponse": event["mcp"]["gatewayResponse"]
}
}
`),
});
// Create gateway with interceptors
const gateway = new agentcore.Gateway(this, "MyGateway", {
gatewayName: "my-gateway",
interceptorConfigurations: [
agentcore.LambdaInterceptor.forRequest(requestInterceptorFn, {
passRequestHeaders: true // Only if you need to inspect headers
}),
agentcore.LambdaInterceptor.forResponse(responseInterceptorFn)
]
});
python
# Create Lambda functions for interceptors
request_interceptor_fn = lambda_.Function(self, "RequestInterceptor",
runtime=lambda_.Runtime.PYTHON_3_12,
handler="index.handler",
code=lambda_.Code.from_inline("""
def handler(event, context):
# Validate and transform request
return {
"interceptorOutputVersion": "1.0",
"mcp": {
"transformedGatewayRequest": event["mcp"]["gatewayRequest"]
}
}
""")
)
response_interceptor_fn = lambda_.Function(self, "ResponseInterceptor",
runtime=lambda_.Runtime.PYTHON_3_12,
handler="index.handler",
code=lambda_.Code.from_inline("""
def handler(event, context):
# Filter or transform response
return {
"interceptorOutputVersion": "1.0",
"mcp": {
"transformedGatewayResponse": event["mcp"]["gatewayResponse"]
}
}
""")
)
# Create gateway with interceptors
gateway = agentcore.Gateway(self, "MyGateway",
gateway_name="my-gateway",
interceptor_configurations=[
agentcore.LambdaInterceptor.for_request(request_interceptor_fn,
pass_request_headers=True
),
agentcore.LambdaInterceptor.for_response(response_interceptor_fn)
]
)
java
// 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();
csharp
// Install: dotnet add package Amazon.CDK.Lib
// Create Lambda functions for interceptors
var requestInterceptorFn = new Function(this, "RequestInterceptor", new FunctionProps {
Runtime = Runtime.PYTHON_3_12,
Handler = "index.handler",
Code = Code.FromInline(@"
def handler(event, context):
# Validate and transform request
return {
""interceptorOutputVersion"": ""1.0"",
""mcp"": {
""transformedGatewayRequest"": event[""mcp""][""gatewayRequest""]
}
}
")
});
var responseInterceptorFn = new Function(this, "ResponseInterceptor", new FunctionProps {
Runtime = Runtime.PYTHON_3_12,
Handler = "index.handler",
Code = Code.FromInline(@"
def handler(event, context):
# Filter or transform response
return {
""interceptorOutputVersion"": ""1.0"",
""mcp"": {
""transformedGatewayResponse"": event[""mcp""][""gatewayResponse""]
}
}
")
});
// Create gateway with interceptors
var gateway = new Gateway(this, "MyGateway", new GatewayProps {
GatewayName = "my-gateway",
InterceptorConfigurations = new [] { LambdaInterceptor.ForRequest(requestInterceptorFn, new InterceptorOptions {
PassRequestHeaders = true
}), LambdaInterceptor.ForResponse(responseInterceptorFn) }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// Create Lambda functions for interceptors
requestInterceptorFn := lambda.NewFunction(this, jsii.String("RequestInterceptor"), &FunctionProps{
Runtime: lambda.Runtime_PYTHON_3_12(),
Handler: jsii.String("index.handler"),
Code: lambda.Code_FromInline(jsii.String(`
def handler(event, context):
# Validate and transform request
return {
"interceptorOutputVersion": "1.0",
"mcp": {
"transformedGatewayRequest": event["mcp"]["gatewayRequest"]
}
}
`)),
})
responseInterceptorFn := lambda.NewFunction(this, jsii.String("ResponseInterceptor"), &FunctionProps{
Runtime: lambda.Runtime_PYTHON_3_12(),
Handler: jsii.String("index.handler"),
Code: lambda.Code_*FromInline(jsii.String(`
def handler(event, context):
# Filter or transform response
return {
"interceptorOutputVersion": "1.0",
"mcp": {
"transformedGatewayResponse": event["mcp"]["gatewayResponse"]
}
}
`)),
})
// Create gateway with interceptors
gateway := agentcore.NewGateway(this, jsii.String("MyGateway"), &GatewayProps{
GatewayName: jsii.String("my-gateway"),
InterceptorConfigurations: []IInterceptor{
agentcore.LambdaInterceptor_ForRequest(requestInterceptorFn, &InterceptorOptions{
PassRequestHeaders: jsii.Boolean(true),
}),
agentcore.LambdaInterceptor_ForResponse(responseInterceptorFn),
},
})
Properties
interceptionPointInterceptionPointinterception_pointInterceptionPointinterceptionPointInterceptionPointInterceptionPointInterceptionPointInterceptionPointInterceptionPoint #The interception point (REQUEST or RESPONSE).
Methods
bind(_scope: Construct, gateway: IGateway): InterceptorBindConfigdef bind(self, _scope: Construct, gateway: IGateway) -> InterceptorBindConfigpublic InterceptorBindConfig bind(Construct _scope, IGateway gateway)public InterceptorBindConfig Bind(Construct _scope, IGateway gateway)func Bind(_scope Construct, Gateway IGateway) InterceptorBindConfig #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
2 parameters · returns
Parameters
_scope_scope_scope_scope_scope— The construct scope (currently unused, reserved for future extensions).gatewaygatewaygatewaygatewayGateway— The gateway this interceptor is being bound to.
Returns: Configuration for CloudFormation rendering
forRequest(lambdaFunction: IFunction, options?: InterceptorOptions): LambdaInterceptor // chainable@staticmethod def for_request(lambda_function: IFunction, options: typing.Optional[InterceptorOptions] = None) -> LambdaInterceptorpublic static LambdaInterceptor forRequest(IFunction lambdaFunction, InterceptorOptions options)public static LambdaInterceptor ForRequest(IFunction lambdaFunction, InterceptorOptions? options = null)func ForRequest(LambdaFunction IFunction, Options InterceptorOptions) LambdaInterceptor static #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:InvokeFunction permission on the specified Lambda function.
2 parameters · returns
Parameters
lambdaFunctionlambda_functionlambdaFunctionlambdaFunctionLambdaFunction— The Lambda function to invoke.optionsoptionsoptionsoptionsOptions— Optional configuration for the interceptor.
Returns: A configured LambdaInterceptor for request interception
forResponse(lambdaFunction: IFunction, options?: InterceptorOptions): LambdaInterceptor // chainable@staticmethod def for_response(lambda_function: IFunction, options: typing.Optional[InterceptorOptions] = None) -> LambdaInterceptorpublic static LambdaInterceptor forResponse(IFunction lambdaFunction, InterceptorOptions options)public static LambdaInterceptor ForResponse(IFunction lambdaFunction, InterceptorOptions? options = null)func ForResponse(LambdaFunction IFunction, Options InterceptorOptions) LambdaInterceptor static #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:InvokeFunction permission on the specified Lambda function.
2 parameters · returns
Parameters
lambdaFunctionlambda_functionlambdaFunctionlambdaFunctionLambdaFunction— The Lambda function to invoke.optionsoptionsoptionsoptionsOptions— Optional configuration for the interceptor.
Returns: A configured LambdaInterceptor for response interception