Skip to content

Commit a2386b9

Browse files
authored
add example for Lambda + HTTP + Axum (#598)
this example bridges Lambda's HTTP support, which is based on the `tower::Service` trait, and the axum web framework, which builds applications that are also based on the `tower::Service` trait.
1 parent f56c7dc commit a2386b9

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

examples/http-axum/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "http-basic-lambda"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
7+
# Use cargo-edit(https://github.com/killercup/cargo-edit#installation)
8+
# to manage dependencies.
9+
# Running `cargo add DEPENDENCY_NAME` will
10+
# add the latest version of a dependency to the list,
11+
# and it will keep the alphabetic ordering for you.
12+
13+
[dependencies]
14+
lambda_http = { path = "../../lambda-http" }
15+
lambda_runtime = { path = "../../lambda-runtime" }
16+
tokio = { version = "1", features = ["macros"] }
17+
tracing = { version = "0.1", features = ["log"] }
18+
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
19+
20+
axum = "0.6.4"
21+
serde_json = "1.0"

examples/http-axum/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# AWS Lambda Function example
2+
3+
## Build & Deploy
4+
5+
1. Install [cargo-lambda](https://github.com/cargo-lambda/cargo-lambda#installation)
6+
2. Build the function with `cargo lambda build --release`
7+
3. Deploy the function to AWS Lambda with `cargo lambda deploy --iam-role YOUR_ROLE`
8+
9+
## Build for ARM 64
10+
11+
Build the function with `cargo lambda build --release --arm64`

examples/http-axum/src/main.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//! This is an example function that leverages the Lambda Rust runtime's HTTP support
2+
//! and the [axum](https://docs.rs/axum/latest/axum/index.html) web framework. The
3+
//! runtime HTTP support is backed by the [tower::Service](https://docs.rs/tower-service/0.3.2/tower_service/trait.Service.html)
4+
//! trait. Axum applications are also backed by the `tower::Service` trait. That means
5+
//! that it is fairly easy to build an Axum application and pass the resulting `Service`
6+
//! implementation to the Lambda runtime to run as a Lambda function. By using Axum instead
7+
//! of a basic `tower::Service` you get web framework niceties like routing, request component
8+
//! extraction, validation, etc.
9+
10+
use lambda_http::{
11+
run,
12+
Error,
13+
};
14+
use axum::{
15+
extract::Path,
16+
response::Json,
17+
Router,
18+
routing::{get, post},
19+
};
20+
use serde_json::{Value, json};
21+
22+
async fn root() -> Json<Value> {
23+
Json(json!({ "msg": "I am GET /" }))
24+
}
25+
26+
async fn get_foo() -> Json<Value> {
27+
Json(json!({ "msg": "I am GET /foo" }))
28+
}
29+
30+
async fn post_foo() -> Json<Value> {
31+
Json(json!({ "msg": "I am POST /foo" }))
32+
}
33+
34+
async fn post_foo_name(Path(name): Path<String>) -> Json<Value> {
35+
Json(json!({ "msg": format!("I am POST /foo/:name, name={name}") }))
36+
}
37+
38+
#[tokio::main]
39+
async fn main() -> Result<(), Error> {
40+
tracing_subscriber::fmt()
41+
.with_max_level(tracing::Level::INFO)
42+
// disabling time is handy because CloudWatch will add the ingestion time.
43+
.without_time()
44+
.init();
45+
46+
let app = Router::new()
47+
.route("/", get(root))
48+
.route("/foo", get(get_foo).post(post_foo))
49+
.route("/foo/:name", post(post_foo_name));
50+
51+
run(app).await
52+
}

0 commit comments

Comments
 (0)