Amazon ECR 배포 중 나타나는 [An error occurred (AccessDeniedException) when calling the GetAuthorizationToken operation] 문제 해결
안녕하세요 클래스메소드 김재욱(Kim Jaewook) 입니다. 이번에는 Amazon ECR 배포 중 나타나는 [An error occurred (AccessDeniedException) when calling the GetAuthorizationToken operation] 문제를 해결하는 방법에 대해서 정리해 봤습니다.
문제 발생
Amazon ECR의 리포지토리에 이미지를 푸시하기 위한 명령어를 확인하고, 순서대로 명령어를 실행해 푸시합니다.
Unable to locate credentials. You can configure credentials by running "aws configure".
Error: Cannot perform an interactive login from a non TTY device
명령어를 입력해 보면, 상기와 같은 에러가 뜨는데, aws configure를 설정하라는 메시지입니다.
아래 블로그를 바탕으로 aws configure를 설정합시다.
aws configure 설정 이후 아래 명령어를 통해 정상적으로 프로필이 출력되는지 확인합니다.
[ec2-user@ip-xx-x-x-xx ~]$ aws sts get-caller-identity
{
"Account": "xxxxxxxxxxxxx",
"UserId": "xxxxxxxxxxxxxxxxxxx",
"Arn": "arn:aws:iam::xxxxxxxxxxxxx:user/cm-kim.jaewook"
}
이어서 로그인을 시도합니다. 로그인은 문제 없이 성공입니다.
[ec2-user@ip-xx-x-x-xx ~]$ aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin xxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com
WARNING! Your password will be stored unencrypted in /home/ec2-user/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
하지만, docker 관련 명령어를 수행하면, 접근이 거부되었다는 메시지가 출력됩니다.
[ec2-user@ip-xx-x-x-xx ~]$ docker tag ecs-nginx:latest xxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com/ecs-nginx:latest
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.44/images/ecs-nginx:latest/tag?repo=xxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com%2Fecs-nginx&tag=latest": dial unix /var/run/docker.sock: connect: permission denied
문제 해결
먼저 AWS 공식 문서를 살펴보면, AWS CLI 버전을 확인하라고 합니다. 현재 사용하고 있는 버전은 문제가 없으므로 AWS CLI 문제는 아닙니다.
그렇다면 일반 유저로 명령어를 입력했기 때문일까요? sudo를 사용하니 문제없이 성공합니다.
[ec2-user@ip-xx-x-x-xx ~]$ sudo docker push xxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com/ecs-nginx:latest
하지만 push 명령어를 실행하니 또 실패합니다. sudo가 아닌, 일반 유저로 push를 진행하면 [permission denied] 접근 거부 메시지가 출력되기 때문에 반드시 sudo를 사용할 필요가 있다는 뜻인데.. 왜 안 되는 걸까요?
[ec2-user@ip-xx-x-x-xx ~]$ sudo docker push xxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com/ecs-nginx:latest
The push refers to repository [xxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com/ecs-nginx]
bb4903379e79: Preparing
cd38dca3d982: Preparing
d35594dd7e6d: Preparing
126eaee18409: Preparing
6380429cac56: Preparing
13fcb2d303e8: Preparing
151f9feea563: Preparing
7fb72a7d1a8e: Preparing
no basic auth credentials
정답은 aws configure에 있습니다. 일반 유저에는 aws configure를 통해 권한 설정을 마쳤지만, sudo 즉 루트 유저에는 aws configure 설정이 되어있지 않다는 것입니다.
※ 일반 유저에서 설정한 값이 루트 유저에게는 적용되지 않을 수 있습니다.
항목 | 일반 사용자 (ec2-user 등) |
루트 사용자 (root ) |
---|---|---|
홈 디렉터리 | /home/ec2-user 등 |
/root |
쉘 환경 설정 파일 | /home/ec2-user/.bashrc 등 |
/root/.bashrc 등 |
권한 수준 | 제한적 (sudo 필요) | 시스템 전체 권한 보유 |
PATH, alias 설정 | 사용자 디렉터리 기준으로 설정 | 루트 전용으로 별도 설정 필요 |
AWS CLI 자격 정보 | ~/.aws/credentials |
/root/.aws/credentials |
crontab 설정 | crontab -e (개별 사용자 기준) |
루트 전용 crontab 별도 존재 |
디폴트 로그인 셸 | /bin/bash 또는 /bin/sh 등 |
동일하지만 설정 별도로 존재 가능 |
이를 해결하기 위해 루트 유저에도 aws configure를 설정합시다.
aws configure 설정 이후 다시 로그인부터 명령어를 입력해 보면, 명령어가 성공한 것을 확인할 수 있습니다.
[root@ip-xx-x-x-xx dockerfile-folder]# docker push xxxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com/ecs-nginx:latest
The push refers to repository [xxxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com/ecs-nginx]
bb4903379e79: Pushed
cd38dca3d982: Pushed
d35594dd7e6d: Pushed
126eaee18409: Pushed
6380429cac56: Pushed
13fcb2d303e8: Pushed
151f9feea563: Pushed
7fb72a7d1a8e: Pushed
latest: digest: sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx size: 1985
ECR 콘솔 화면에서도 이미지가 푸시된 것을 확인할 수 있습니다.
본 블로그 게시글을 읽고 궁금한 사항이 있으신 분들은 jaewookkim533@yahoo.com로 보내주시면 감사하겠습니다.
문의 사항은 클래스메소드 코리아로!
클래스메소드 코리아에서는 다양한 세미나 및 이벤트를 진행하고 있습니다.
진행중인 이벤트는 아래 페이지를 참고해주세요.
AWS에 대한 상담 및 클래스 메소드 멤버스에 관한 문의사항은 아래 메일로 연락주시면 감사드립니다!
Info@classmethod.kr