투케이2K

485. (javaScript) 자바스크립트 AWS GetUser 호출해 특정 IAM 사용자 상세 정보 조회 수행 본문

JavaScript

485. (javaScript) 자바스크립트 AWS GetUser 호출해 특정 IAM 사용자 상세 정보 조회 수행

투케이2K 2026. 2. 3. 20:38
728x90

[개발 환경 설정]

개발 툴 : Edit++

개발 언어 : JavaScript

 

[소스 코드]

-----------------------------------------------------------------------------------------
[사전 설명 및 설정 사항]
-----------------------------------------------------------------------------------------

- 개발 환경 : Web


- 개발 기술 : JavaScript (자바스크립트) / AWS / GetUser


- 사전) AWS GetUser API 설명 정리 : 

  >> AWS GetUser 는 특정 IAM 사용자의 상세 정보를 조회하는 API입니다

  >> AWS GetUser API 호출 시 반환 되는 정보
  
    - 사용자 생성 날짜
    - Path
    - 고유 ID(UserId)
    - ARN
    - 패스워드 및 액세스키 사용 정보 (일부 시점의 비정확성에 대한 경고 포함)

-----------------------------------------------------------------------------------------





-----------------------------------------------------------------------------------------
[소스 코드]
-----------------------------------------------------------------------------------------

<!DOCTYPE HTML>
<html lang="ko">
<head>
    <title>javaScriptTest</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">


    <!-- 반응형 구조 만들기 -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">


    <!-- 내부 CSS 스타일 지정 -->
    <style>

        html, body {
            width: 100%;
            height: 100%;
            margin : 0 auto;
            padding : 0;
            border : none;
            background-color: #666;
        }

    </style>





    <!-- [CDN 주소 설정] -->
    <script src="https://code.jquery.com/jquery-latest.min.js"></script>
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.1416.0.min.js"></script>






    <!-- [자바스크립트 코드 지정] -->
    <script>

        // --------------------------------------------------------------------------------------------------------------

        // [전역 변수 선언]        
        const accessKey = 'AK..A6';
        const secretKey = 'mP..5J';

        // --------------------------------------------------------------------------------------------------------------

        // [html 최초 로드 및 이벤트 상시 대기 실시]
        window.onload = async function() {
          console.log("");
          console.log("=========================================");
          console.log("[window onload] : [start]");
          console.log("=========================================");
          console.log("");

          try {

            // -----------------------------------------
            // [AWS.config 지정]
            // -----------------------------------------       
            AWS.config.update({              
              accessKeyId: accessKey,
              secretAccessKey: secretKey
            });


            // -----------------------------------------
            // [AWS 객체 생성]
            // -----------------------------------------
            const aws = new AWS.IAM();


            // -----------------------------------------
            // [요청 파라미터 생성]
            // -----------------------------------------
            /*
            const param = { // ✅ Request_1
              UserName: 'twok2k' // ✅ IAM 사용자 이름 : 파라미터 전송 생략 시 Access Key ID를 기반으로 IAM이 사용자 이름을 암묵적으로 결정합니다              
            };
            // */

            const param = { // ✅ Request_2 
            };


            // -----------------------------------------
            // [GetUser] : 특정 IAM 사용자의 상세 정보를 조회 수행
            // -----------------------------------------
            // AWS 참고 사이트 : https://docs.aws.amazon.com/IAM/latest/APIReference/API_GetUser.html
            // -----------------------------------------
            // 리전 무관 : 모든 리전에서 동일하게 동작.
            // -----------------------------------------
            aws.getUser( param , function(err, data) { 
              if (err) {
                console.error("");
                console.error("=========================================");
                console.error("[getUser] : [Error]");
                console.error("---------------------------------------");
                console.error(err);
                console.error("=========================================");
                console.error("");

                // ---------------------------------------------
                // ✅ [주요 에러 정리]
                // ---------------------------------------------
                // NoSuchEntity : 조회하려는 사용자(UserName)가 존재하지 않을 때 발생 혹은 현재 자격증명이 IAM User가 아닐 때(Role 기반일 때) 발생 가능
                // ---------------------------------------------
                // ServiceFailure : AWS 내부 오류로 인해 요청을 처리할 수 없는 경우
                // ---------------------------------------------
                // AccessDenied : 권한 없음 – 별도 공통 오류
                // ---------------------------------------------
                // InvalidClientTokenId : 자격증명 문제
                // ---------------------------------------------
                // SignatureDoesNotMatch : 서명 오류
                // ---------------------------------------------


                // ---------------------------------------------

                // [Body 표시 JSON]
                // ---------------------------------------------
                var errJson = {
                  response: "error",
                  data: err
                }
                // ---------------------------------------------


                // ---------------------------------------------
                // [에러 출력]
                // ---------------------------------------------
                document.write(JSON.stringify(errJson));
                // ---------------------------------------------

              } else {
                console.log("");
                console.log("=========================================");
                console.log("[getUser] : [Success]");
                console.log("---------------------------------------");
                console.log(JSON.stringify(data));
                console.log("=========================================");
                console.log("");

                // ---------------------------------------------
                // ✅ [로그 출력 예시 첨부]
                // ---------------------------------------------
                /*
                {
                  "ResponseMetadata": {
                    "RequestId": "9029..375e"
                  },
                  "User": {
                    "Path": "/",
                    "UserName": "twok2k@test.com-CLI",
                    "UserId": "AI..2S",
                    "Arn": "arn:aws:iam::123456789012:user/twok2k@test.com-CLI",
                    "CreateDate": "2025-02-18T01:09:27.000Z",
                    "Tags": [
                      {
                        "Key": "AK..A6",
                        "Value": "twok2k@test.com-CLI-accesskey"
                      },
                      {
                        "Key": "Name",
                        "Value": "투케이 책임"
                      }
                    ]
                  }
                }
                */
                // ---------------------------------------------


                // ---------------------------------------------
                // [Body 표시 JSON]
                // ---------------------------------------------
                var resJson = {
                  response: "success",
                  data: data
                }
                // ---------------------------------------------


                // ---------------------------------------------
                // [결과 출력]
                // ---------------------------------------------
                document.write(JSON.stringify(resJson));
                // ---------------------------------------------

              }

            });

          }
          catch (exception) {
            console.error("");
            console.error("=========================================");
            console.error("[window onload] : [Exception] : 예외 상황 발생");
            console.error("-----------------------------------------");
            console.error(exception);
            console.error("=========================================");
            console.error("");

            // ---------------------------------------------
            // [Body 표시 JSON]
            // ---------------------------------------------
            var errJson = {
              response: "exception",
              data: exception.message
            }
            // ---------------------------------------------


            // ---------------------------------------------
            // [에러 출력]
            // ---------------------------------------------
            document.write(JSON.stringify(errJson));
            // ---------------------------------------------

          }

        };

        // --------------------------------------------------------------------------------------------------------------

    </script>


</head>


<body>

</body>

</html>

-----------------------------------------------------------------------------------------





-----------------------------------------------------------------------------------------
[참고 사이트]
-----------------------------------------------------------------------------------------

[자바스크립트 AWS GetCallerIdentity 사용해 IAM 계정 자격 증명 정보 유효성 검증]

https://kkh0977.tistory.com/8600

https://blog.naver.com/kkh0977/224167527899


[Aws Security Token Service] Aws STS 임시 보안 자격 증명 설명 정리

https://kkh0977.tistory.com/7942

https://blog.naver.com/kkh0977/223846461194?trackingCode=blog_bloghome_searchlist


[AWS] [Lambda] 런타임 Python 3.13 - boto3 모듈 사용해 AWS STS 임시 정보 호출 람다 생성

https://blog.naver.com/kkh0977/223962739399?trackingCode=blog_bloghome_searchlist


[자바스크립트 AWS STS 임시 자격 증명 사용해 S3 Get PreSignedUrl 프리 사인 URL 주소 생성]

https://kkh0977.tistory.com/8151

https://blog.naver.com/kkh0977/223938740405


[Aws S3 Storage] PreSignedUrl 프리 사인 URL 주소 정리 - S3 버킷 저장소 Get 확인 및 Put 업로드 임시 권한 주소

https://blog.naver.com/kkh0977/223903771897


[Aws S3 Storage] S3 (Amazon Simple Storage Service) 버킷 저장소 개념 및 설명 정리

https://blog.naver.com/kkh0977/223733087281?trackingCode=blog_bloghome_searchlist


[[간단 소스] Aws S3 버킷 저장소 리스트 목록 확인 - AmazonS3 listBuckets]

https://blog.naver.com/kkh0977/223797258160?trackingCode=blog_bloghome_searchlist


[자바스크립트 AWS S3 Get 요청 및 Put 업로드 PreSignedUrl 프리 사인 URL 주소 생성 수행 - getSignedUrl]

https://blog.naver.com/kkh0977/223903767776

-----------------------------------------------------------------------------------------
 
728x90
반응형
Comments