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
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)
]
});
Properties
interceptionPointInterceptionPointinterception_pointInterceptionPointinterceptionPointInterceptionPointInterceptionPointInterceptionPointInterceptionPointInterceptionPointrequired#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) LambdaInterceptorstatic#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) LambdaInterceptorstatic#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