Previously when AWS Lambda was used with AWS ApiGW there was the requestContext.identity part of the event that could have been used to fetch the source ip from it.
When Lambda is used with function-url is there anything like this?
I was trying to get the request_context but identity is empty in it.
As it turns out the RequestContext in Rust is an Enum.
pub enum RequestContext {
ApiGatewayV1(ApiGatewayProxyRequestContext),
ApiGatewayV2(ApiGatewayV2httpRequestContext),
Alb(AlbTargetGroupRequestContext),
WebSocket(ApiGatewayWebsocketProxyRequestContext),
}
Making IP extraction type safe the following way:
fn get_source_ip(ctx: RequestContext) -> Option<String> {
match ctx {
lambda_http::request::RequestContext::ApiGatewayV1(x) => x.identity.source_ip,
lambda_http::request::RequestContext::ApiGatewayV2(y) => y.http.source_ip,
lambda_http::request::RequestContext::Alb(_z) => None,
lambda_http::request::RequestContext::WebSocket(w) => w.identity.source_ip,
}
}
AWS Lambda with function-url uses the ApiGatewayV2 type. I am not sure what to do with ALB for now.