본문 바로가기

Project/트러블 슈팅

[Error] CodeDeploy Error code : UnknownError 원인, 해결방법

1. 문제 상황

codedeploy agent was not able to receive the lifecycle event. check the codedeploy agent logs on your host and make sure the agent is running and can connect to the codedeploy server.

CICD 구축을 위해 CodeDeploy를 Github Actions와 연동시켰습니다. 그런데, 자꾸 ApplicationStop Event 단에서 실패하는 것입니다. 원인을 분석하기 위해 CodeDeploy log를 열어보았습니다.

 

2. 원인 분석

less /var/log/aws/codedeploy-agent/codedeploy-agent.log

저는 Ubuntu 인스턴스이기 때문에 위 명령어를 입력해 로그 파일을 볼 수 있었습니다. Windows의 경우 아래 aws docs 공식 reference를 참조하시길 바랍니다.

 

2023-05-31T05:53:49 ERROR [codedeploy-agent(672)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Missing credentials - please check if this instance was started with an IAM instance profile
2023-05-31T05:54:23 INFO  [codedeploy-agent(672)]: Version file found in /opt/codedeploy-agent/.version with agent version OFFICIAL_1.6.0-49_deb.
2023-05-31T05:54:23 ERROR [codedeploy-agent(672)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Error polling for host commands: Aws::Errors::MissingCredentialsError - unable to sign request without credentials set - 생략...

Missing credentials - please check if this instance was started with an IAM instance profile

아! EC2에서 S3에 접근하여 배포할 .zip 파일을 가져올 수 있는 IAM role을 부여하기 전에 CodeDeploy agent를 설치한 것이 생각났습니다. 로그 메시지의 힌트 대로 AmazonEC2RoleforAWSCodeDeploy IAM Role을 적용하고, CodeDeploy agent를 재실행 시켜주었습니다.

 

3. 해결 방법

sudo service codedeploy-agent restart
systemctl status codedeploy-agent.service

위는 CodeDeploy agent 재실행 명령어 및 agent가 실행 중인지 상태를 체크하는 명령어입니다.

 

Actvie: active (running)을 보아 agent가 재부팅되어 잘 동작하고 있음을 확인했으니 다시 Deploy를 재실행 시켜보겠습니다.

 

결과는? 성공입니다! IAM Role을 부여한 후 codedeploy-agent를 부팅시켜야 한다는 점과 agent log 파일을 확인하는 방법을 알게 되었던 오류였습니다.

 

reference) https://docs.aws.amazon.com/ko_kr/codedeploy/latest/userguide/deployments-view-logs.html#deployments-view-logs-instance-unix