Lambda context 객체로 Rust 함수 정보 조회하기
Lambda context 객체로 Rust 함수 정보 조회하기 (Using the Lambda context object to retrieve Rust function information)
Lambda가 함수를 실행할 때, 핸들러가 받는 LambdaEvent에 context 객체를 추가해요. 이 객체는 호출, 함수, 실행 환경에 대한 정보를 담은 속성을 제공해요.
본문
Context 속성
- request_id: Lambda 서비스가 생성한 AWS 요청 ID예요.
- deadline: 현재 호출의 실행 마감 시한(밀리초)이에요.
- invoked_function_arn: 호출되는 Lambda 함수의 Amazon Resource Name(ARN)이에요.
- xray_trace_id: 현재 호출의 AWS X-Ray 트레이스 ID예요.
- client_content: AWS 모바일 SDK가 보낸 클라이언트 context 객체예요. AWS 모바일 SDK로 함수를 호출하는 경우가 아니면 이 필드는 비어 있어요.
- identity: 함수를 호출한 Amazon Cognito 신원이에요. Lambda API에 대한 호출 요청이 Amazon Cognito identity pools가 발급한 AWS 자격 증명으로 이뤄진 경우가 아니면 이 필드는 비어 있어요.
- env_config: 로컬 환경 변수의 Lambda 함수 구성이에요. 함수 이름, 메모리 할당, 버전, 로그 스트림 같은 정보가 포함돼요.
호출 context 정보 접근하기
Lambda 함수는 환경과 호출 요청에 대한 메타데이터에 접근할 수 있어요. 함수 핸들러가 받는 LambdaEvent 객체에는 context 메타데이터가 포함돼요.
use lambda_runtime::{service_fn, LambdaEvent, Error};
use serde_json::{json, Value};
async fn handler(event: LambdaEvent<Value>) -> Result<Value, Error> {
let invoked_function_arn = event.context.invoked_function_arn;
Ok(json!({ "message": format!("Hello, this is function {invoked_function_arn}!") }))
}
#[tokio::main]
async fn main() -> Result<(), Error> {
lambda_runtime::run(service_fn(handler)).await
}