AWS 명령줄 도구에서 MFA 로그인을 해야 하는 상황이 있다. 로그인하는 방법은 Authenticate Access using MFA Through the AWS CLI에서 자세히 설명하지만 이 글을 다 읽는다 해도 그 과정이 엄청나게 번거롭다는 생각에 정신이 아득해질 뿐이다.
그래서! 인생을 편하게 해줄 간단한 스크립트를 작성했다. 사용법은 매우 간단하다.
$ awslogin.sh
This will save new AWS credentials into the profile aws4-mfa once signed with MFA token, which issued with the profile aws4.
Would you like to proceed? [y/n]:
$ AWS_LOGIN_PROFILE=my-profile AWS_PROFILE=my-profile-mfa awslogin.sh 987654
Successfully logged in
혹시라도 실패하는 경우에는 간단하게 안내를 해준다.
An error occurred (AccessDenied) when calling the GetSessionToken operation: MultiFactorAuthentication failed with invalid MFA one time pass code.
오류 문구
USAGE: source awslogin.sh 123456
See https://aws.amazon.com/premiumsupport/knowledge-center/authenticate-mfa-cli/
이 스크립트를 사용하려면 먼저 AWS의 Access key와 Secret key를 ~/.aws/credentials 파일이나 환경변수로 등록해야 한다. 그리고 AWS_LOGIN_PROFILE과 AWS_PROFILE은 direnv 같은 도구를 이용해 설정하면 더욱 편리하다.
AWS_LOGIN_PROFILE: AWS에서 발급 받은 키 값을 가진 Profile을 적는다.AWS_PROFILE:awslogin.sh로 MFA 로그인하면 임시로 세션을 발급 받는데 그 세션 토큰 값을 저장할 Profile을 가리킨다.
Pingback: AWS 주간 소식 모음 – 2018년 7월 30일 – 지락문화예술공작단