

# Rust での HTTP イベントの処理
<a name="rust-http-events"></a>

Amazon API Gateway、アプリケーションロードバランサー、および [Lambda 関数 URL](urls-configuration.md) は、HTTP イベントを Lambda に送信できます。crates.io の [aws\$1lambda\$1events](https://crates.io/crates/aws_lambda_events) クレートを使用して、これらのソースからのイベントを処理できます。

**Example — API Gateway プロキシリクエストの処理**  
次の点に注意してください。  
+ `use aws_lambda_events::apigw::{ApiGatewayProxyRequest, ApiGatewayProxyResponse}`: [aws\$1lambda\$1events](https://crates.io/crates/aws-lambda-events) クレートには、多くの Lambda イベントが含まれています。コンパイル時間を短縮するには、機能フラグを使用して必要なイベントをアクティブにします。例えば、`aws_lambda_events = { version = "0.8.3", default-features = false, features = ["apigw"] }` などです。
+ `use http::HeaderMap`: このインポートでは、依存関係に [http](https://crates.io/crates/http) クレートを追加する必要があります。

```
use aws_lambda_events::apigw::{ApiGatewayProxyRequest, ApiGatewayProxyResponse};
use http::HeaderMap;
use lambda_runtime::{service_fn, Error, LambdaEvent};

async fn handler(
    _event: LambdaEvent<ApiGatewayProxyRequest>,
) -> Result<ApiGatewayProxyResponse, Error> {
    let mut headers = HeaderMap::new();
    headers.insert("content-type", "text/html".parse().unwrap());
    let resp = ApiGatewayProxyResponse {
        status_code: 200,
        multi_value_headers: headers.clone(),
        is_base64_encoded: false,
        body: Some("Hello AWS Lambda HTTP request".into()),
        headers,
    };
    Ok(resp)
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    lambda_runtime::run(service_fn(handler)).await
}
```

また、[Lambda 用の Rust ランタイムクライアント](https://github.com/aws/aws-lambda-rust-runtime)では、これらのイベントタイプを抽象化することもでき、どのサービスがイベントを送信するかに関係なく、ネイティブ HTTP タイプを使用できます。次のコードは前の例と同等で、Lambda 関数 URL、Application Load Balancer、API Gateway ですぐに使用できます。

**注記**  
[lambda\$1http](https://crates.io/crates/lambda_http) クレートは、その下の [lambda\$1runtime](https://crates.io/crates/lambda_runtime) クレートを使用します。`lambda_runtime` を個別にインポートする必要はありません。

**Example — HTTP リクエストの処理**  

```
use lambda_http::{service_fn, Error, IntoResponse, Request, RequestExt, Response};

async fn handler(event: Request) -> Result<impl IntoResponse, Error> {
    let resp = Response::builder()
        .status(200)
        .header("content-type", "text/html")
        .body("Hello AWS Lambda HTTP request")
        .map_err(Box::new)?;
    Ok(resp)
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    lambda_http::run(service_fn(handler)).await
}
```

`lambda_http` の使用方法の別の例については、AWS Labs GitHub リポジトリにある「[http-axum コードサンプル](https://github.com/aws/aws-lambda-rust-runtime/blob/main/examples/http-axum/src/main.rs)」を参照してください。

**Rust 用サンプル HTTP Lambda イベント**
+ 「[Lambda HTTP イベント](https://github.com/aws/aws-lambda-rust-runtime/tree/main/examples/http-basic-lambda)」: HTTP イベントを処理する Rust 関数。
+ 「[CORS ヘッダー付き Lambda HTTP イベント](https://github.com/aws/aws-lambda-rust-runtime/blob/main/examples/http-cors)」: Tower を使用して CORS ヘッダーを挿入する Rust 関数。
+ [共有リソースを使用する Lambda HTTP イベント](https://github.com/aws/aws-lambda-rust-runtime/tree/main/examples/basic-shared-resource): 関数ハンドラーが作成される前に初期化された共有リソースを使用する Rust 関数。