-
Are there any examples of how to implement an authorizer function with simple response format 2.0? I keep receiving "Internal Server Error". I've given API gateway permission and I've tried responding with use lambda_http::{run, service_fn, Body, Error, Request, Response};
use serde_json::Value;
/// This is the main body for the function.
/// Write your code inside it.
/// There are some code example in the following URLs:
/// - https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples
async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
/// async fn function_handler(event: Request) -> Result<Value, Error> {
let simple_response = serde_json::json!({
"isAuthorized": true,
}).to_string();
// Ok(simple_response)
// Return something that implements IntoResponse.
// It will be serialized to the right response event automatically by the runtime
let resp = Response::builder()
.status(200)
.header("content-type", "application/json")
.body(simple_response.into())
.map_err(Box::new)?;
Ok(resp)
}
#[tokio::main]
async fn main() -> Result<(), Error> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
// disable printing the name of the module in every log line.
.with_target(false)
// disabling time is handy because CloudWatch will add the ingestion time.
.without_time()
.init();
run(service_fn(function_handler)).await
} {
"Version": "2012-10-17",
"Id": "default",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "lambda:InvokeFunction",
"Resource": "arn:aws:lambda:us-east-1:*:function:*",
"Condition": {
"ArnLike": {
"AWS:SourceArn": "arn:aws:execute-api:us-east-1:*:*/*/*"
}
}
}
]
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I think the problem is that you're trying to use the You'll need something like the basic lambda example, but receiving structures deserialized from the authorizer payload: https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples/basic-lambda You might find the right structure in the lambda_events repo, but I'd keep it simple to start and just use use lambda_runtime::{run, service_fn, Error, LambdaEvent};
use serde_json::Value;
async fn function_handler(event: LambaEvent<Value>) -> Result<Value, Error> {
let simple_response = serde_json::json!({
"isAuthorized": true,
});
Ok(simple_response)
}
#[tokio::main]
async fn main() -> Result<(), Error> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
// disable printing the name of the module in every log line.
.with_target(false)
// disabling time is handy because CloudWatch will add the ingestion time.
.without_time()
.init();
run(service_fn(function_handler)).await
} |
Beta Was this translation helpful? Give feedback.
-
Solved my issue. In the API Gateway console you cannot have a value in the IAM role arn for the authorizer if you have the console auto generate invoke permissions. Even though the auto generate button hides the input box, any previously set arn value seems to override. I should really just create a role in my SAM template so I don't have to touch the console. Thanks @calavera for your input here and your contributions to this library. |
Beta Was this translation helpful? Give feedback.
-
Great news @nabware ! Btw, I just released a new version of |
Beta Was this translation helpful? Give feedback.
Solved my issue. In the API Gateway console you cannot have a value in the IAM role arn for the authorizer if you have the console auto generate invoke permissions. Even though the auto generate button hides the input box, any previously set arn value seems to override. I should really just create a role in my SAM template so I don't have to touch the console.
Thanks @calavera for your input here and your contributions to this library.