Skip to content

Commit 2f7709f

Browse files
softpropsdavidbarsky
authored andcommitted
Introduce lambda-http module based on lando to support API Gateway (#18)
1 parent 109a547 commit 2f7709f

File tree

13 files changed

+1234
-3
lines changed

13 files changed

+1234
-3
lines changed

Cargo.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
33
"lambda-runtime-client",
4-
"lambda-runtime"
4+
"lambda-runtime",
5+
"lambda-http"
56
]

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

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

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

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

1011
## Example function
1112

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

8889
## lambda-runtime-client
8990

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

9293
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.
9394

lambda-http/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "lambda_http"
3+
version = "0.1.0"
4+
authors = ["Doug Tangren"]
5+
description = "Rust API Gateway proxy event interfaces for AWS Lambda"
6+
keywords = ["AWS", "Lambda", "APIGateway", "Rust", "API"]
7+
license = "Apache-2.0"
8+
homepage = "https://github.com/awslabs/aws-lambda-rust-runtime"
9+
repository = "https://github.com/awslabs/aws-lambda-rust-runtime"
10+
documentation = "https://docs.rs/lambda_runtime"
11+
12+
[dependencies]
13+
http = "0.1"
14+
serde = "^1"
15+
serde_json = "^1"
16+
serde_derive = "^1"
17+
lambda_runtime = { path = "../lambda-runtime", version = "^0.1" }
18+
tokio = "^0.1"
19+
base64 = "0.10"
20+
failure = "0.1"
21+
failure_derive = "0.1"
22+
serde_urlencoded = "0.5"
23+
24+
[dev-dependencies]
25+
log = "^0.4"
26+
simple_logger = "^1"

lambda-http/examples/basic.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
extern crate lambda_http as http;
2+
extern crate lambda_runtime as runtime;
3+
extern crate log;
4+
extern crate simple_logger;
5+
6+
use http::{lambda, Body, Request, RequestExt, Response};
7+
use runtime::{error::HandlerError, Context};
8+
9+
use log::error;
10+
use std::error::Error;
11+
12+
fn main() -> Result<(), Box<dyn Error>> {
13+
simple_logger::init_with_level(log::Level::Debug).unwrap();
14+
lambda!(my_handler);
15+
16+
Ok(())
17+
}
18+
19+
fn my_handler(e: Request, c: Context) -> Result<Response<Body>, HandlerError> {
20+
Ok(match e.query_string_parameters().get("first_name") {
21+
Some(first_name) => Response::new(format!("Hello, {}!", first_name).into()),
22+
_ => {
23+
error!("Empty first name in request {}", c.aws_request_id);
24+
Response::builder()
25+
.status(400)
26+
.body::<Body>("Empty first name".into())
27+
.expect("failed to render response")
28+
}
29+
})
30+
}

0 commit comments

Comments
 (0)