

# Rust로 HTTP 이벤트 처리
<a name="rust-http-events"></a>

Amazon API Gateway APIs, Application Load Balancer 및 [Lambda 함수 URL](urls-configuration.md)에서 Lambda에 HTTP 이벤트를 전송할 수 있습니다. 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 함수입니다.