imhamburger 님의 블로그

ERROR: Lambda is initializing your function 본문

끄적끄적

ERROR: Lambda is initializing your function

imhamburger 2025. 5. 11. 14:34

운영 DB에 저장된 데이터는 AWS Lambda와 Airflow를 활용해 일정 주기로 BigQuery로 전송되며, 배치 작업이 자동으로 수행되도록 구성되어 있다.

 

그런데 다음과 같은 에러메세지가 떴다:

botocore.exceptions.ClientError: An error occurred (CodeArtifactUserPendingException) when calling the Invoke operation: ERROR: Lambda is initializing your function. It will be ready to invoke shortly.
[2025-05-06, 05:30:30 UTC] {{taskinstance.py:1345}} INFO - Marking task as FAILED. dag_id=recommend-simillar-companies-train-dag, task_id=invoke_tokenizer_lambda, execution_date=20250429T053000, start_date=20250506T053029, end_date=20250506T053030
[2025-05-06, 05:30:30 UTC] {{base.py:73}} INFO - Using connection ID 'telegram_alert' for task execution.
[2025-05-06, 05:30:31 UTC] {{_client.py:1740}} INFO - HTTP Request: POST https://api.telegram.org/bot5498099514:AAEfATAJuRVU4xPy2gnP4xjw0j_lw4xEjMA/sendMessage "HTTP/1.1 200 OK"
[2025-05-06, 05:30:31 UTC] {{standard_task_runner.py:104}} ERROR - Failed to execute job 757492 for task invoke_tokenizer_lambda (An error occurred (CodeArtifactUserPendingException) when calling the Invoke operation: ERROR: Lambda is initializing your function. It will be ready to invoke shortly.; 29484)
[2025-05-06, 05:30:31 UTC] {{local_task_job_runner.py:225}} INFO - Task exited with return code 1
[2025-05-06, 05:30:31 UTC] {{taskinstance.py:2653}} INFO - 0 downstream tasks scheduled from follow-on schedule check

 

이 에러는 AWS Lambda 함수가 아직 "초기화 중"(initializing) 상태일 때 호출하려고 해서 발생한 것인데, 주로 다음과 같은 이유로 발생한다.

 

 

원인


1. Lambda 함수 콜드 스타트(cold start)
- Lambda가 처음 호출되거나 일정 시간 동안 호출되지 않으면, AWS가 내부적으로 함수를 초기화하는 데 시간이 걸린다.

이때 함수가 아직 준비되지 않았는데 호출이 들어오면 위와 같은 에러가 발생할 수 있다.


2. VPC 연결 또는 레이어가 많은 경우
- 함수가 VPC에 연결되어 있거나, 레이어(Layer)를 여러 개 사용하는 경우 초기화 시간이 더 오래 걸린다.


3. 동시성 부족 or 리소스 설정 부족
- 동시에 여러 요청이 들어왔을 때 리소스가 부족해서 초기화가 지연되는 경우이다.


4. 잘못된 IAM 권한/설정 문제로 인해 초기화 중 실패

 

해결 방법


1. 재시도 로직 추가
- Airflow의 `invoke_tokenizer_lambda` 태스크에 재시도(retries) 설정을 추가하면 일시적인 초기화 문제를 피할 수 있다.

default_args = {
    'owner': 'hamburger',
    'depends_on_past': False,
    'retries': 3,
    'retry_delay': timedelta(minutes=1)
}

 

이렇게 하면 Lambda가 "초기화 중"일 경우 약간의 여유 시간을 두고 재시도하게 된다.