armature_lambda/
lib.rs

1//! # Armature Lambda
2//!
3//! AWS Lambda runtime adapter for Armature applications.
4//!
5//! This crate allows you to deploy Armature applications to AWS Lambda
6//! with API Gateway, ALB, or Lambda Function URLs.
7//!
8//! ## Quick Start
9//!
10//! ```rust,ignore
11//! use armature::prelude::*;
12//! use armature_lambda::LambdaRuntime;
13//!
14//! #[controller("/")]
15//! struct HelloController;
16//!
17//! #[controller_impl]
18//! impl HelloController {
19//!     #[get("/")]
20//!     async fn hello() -> &'static str {
21//!         "Hello from Lambda!"
22//!     }
23//! }
24//!
25//! #[module(controllers: [HelloController])]
26//! struct AppModule;
27//!
28//! #[tokio::main]
29//! async fn main() -> Result<(), lambda_runtime::Error> {
30//!     // Initialize tracing for CloudWatch
31//!     armature_lambda::init_tracing();
32//!
33//!     // Create Armature application
34//!     let app = Application::create::<AppModule>();
35//!
36//!     // Run on Lambda
37//!     LambdaRuntime::new(app).run().await
38//! }
39//! ```
40//!
41//! ## With AWS Services
42//!
43//! ```rust,ignore
44//! use armature_lambda::LambdaRuntime;
45//! use armature_aws::{AwsServices, AwsConfig};
46//!
47//! #[tokio::main]
48//! async fn main() -> Result<(), lambda_runtime::Error> {
49//!     armature_lambda::init_tracing();
50//!
51//!     // Initialize AWS services
52//!     let aws = AwsServices::new(
53//!         AwsConfig::from_env()
54//!             .enable_dynamodb()
55//!             .enable_s3()
56//!             .build()
57//!     ).await?;
58//!
59//!     // Register in DI container
60//!     let app = Application::create::<AppModule>();
61//!     app.container().register(aws);
62//!
63//!     LambdaRuntime::new(app).run().await
64//! }
65//! ```
66//!
67//! ## Deployment
68//!
69//! Build for Lambda with:
70//!
71//! ```bash
72//! # Install cargo-lambda
73//! cargo install cargo-lambda
74//!
75//! # Build for Lambda
76//! cargo lambda build --release
77//!
78//! # Deploy
79//! cargo lambda deploy
80//! ```
81
82mod error;
83mod request;
84mod response;
85mod runtime;
86
87pub use error::{LambdaError, Result};
88pub use request::LambdaRequest;
89pub use response::LambdaResponse;
90pub use runtime::{LambdaConfig, LambdaRuntime};
91
92// Re-export lambda types
93pub use lambda_http;
94pub use lambda_runtime;
95
96/// Initialize tracing for Lambda/CloudWatch.
97///
98/// This sets up structured JSON logging suitable for CloudWatch Logs.
99pub fn init_tracing() {
100    use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
101
102    let filter = tracing_subscriber::EnvFilter::try_from_default_env()
103        .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
104
105    tracing_subscriber::registry()
106        .with(filter)
107        .with(tracing_subscriber::fmt::layer().json().flatten_event(true))
108        .init();
109}
110
111/// Initialize tracing with a custom log level.
112pub fn init_tracing_with_level(level: &str) {
113    use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
114
115    let filter = tracing_subscriber::EnvFilter::new(level);
116
117    tracing_subscriber::registry()
118        .with(filter)
119        .with(tracing_subscriber::fmt::layer().json().flatten_event(true))
120        .init();
121}