Skip to content

introduce lambda-http module based on lando to support apigateway #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 4, 2018
Merged
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"lambda-runtime-client",
"lambda-runtime"
"lambda-runtime",
"lambda-http"
]
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

[![Build Status](https://travis-ci.org/awslabs/aws-lambda-rust-runtime.svg?branch=master)](https://travis-ci.org/awslabs/aws-lambda-rust-runtime)

This package makes it easy to run AWS Lambda Functions written in Rust. This workspace includes two crates:
This package makes it easy to run AWS Lambda Functions written in Rust. This workspace includes multiple crates:

* **`lambda-runtime-client`** is a client SDK for the Lambda Runtime APIs. You probably don't need to use this crate directly!
* **`lambda-runtime`** is a library that makes it easy to write Lambda functions in Rust.
* **`lambda-http`** is a library that makes it easy to write API Gateway proxy event focused Lambda functions in Rust.

## Example function

Expand Down Expand Up @@ -87,7 +88,7 @@ $ cat output.json # Prints: {"message":"Hello, world!"}

## lambda-runtime-client

Defines the `RuntimeClient` trait and provides its `HttpRuntimeClient` implementation. The client fetches events and returns output as `Vec<u8>`.
Defines the `RuntimeClient` trait and provides its `HttpRuntimeClient` implementation. The client fetches events and returns output as `Vec<u8>`.

For error reporting to the runtime APIs the library defines the `RuntimeApiError` trait and the `ErrorResponse` object. Custom errors for the APIs should implement the `to_response() -> ErrorResponse` method of the `RuntimeApiError` trait.

Expand Down
26 changes: 26 additions & 0 deletions lambda-http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "lambda_http"
version = "0.1.0"
authors = ["Doug Tangren"]
description = "Rust API Gateway proxy event interfaces for AWS Lambda"
keywords = ["AWS", "Lambda", "APIGateway", "Rust", "API"]
license = "Apache-2.0"
homepage = "https://github.com/awslabs/aws-lambda-rust-runtime"
repository = "https://github.com/awslabs/aws-lambda-rust-runtime"
documentation = "https://docs.rs/lambda_runtime"

[dependencies]
http = "0.1"
serde = "^1"
serde_json = "^1"
serde_derive = "^1"
lambda_runtime = { path = "../lambda-runtime", version = "^0.1" }
tokio = "^0.1"
base64 = "0.10"
failure = "0.1"
failure_derive = "0.1"
serde_urlencoded = "0.5"

[dev-dependencies]
log = "^0.4"
simple_logger = "^1"
30 changes: 30 additions & 0 deletions lambda-http/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extern crate lambda_http as http;
extern crate lambda_runtime as runtime;
extern crate log;
extern crate simple_logger;

use http::{lambda, Body, Request, RequestExt, Response};
use runtime::{error::HandlerError, Context};

use log::error;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
simple_logger::init_with_level(log::Level::Debug).unwrap();
lambda!(my_handler);

Ok(())
}

fn my_handler(e: Request, c: Context) -> Result<Response<Body>, HandlerError> {
Ok(match e.query_string_parameters().get("first_name") {
Some(first_name) => Response::new(format!("Hello, {}!", first_name).into()),
_ => {
error!("Empty first name in request {}", c.aws_request_id);
Response::builder()
.status(400)
.body::<Body>("Empty first name".into())
.expect("failed to render response")
}
})
}
Loading