투케이2K

480. (javaScript) 자바스크립트 AWS Access Key ID 액세스 키 가 마지막으로 사용된 이력 조회 수행 - GetAccessKeyLastUsed 본문

JavaScript

480. (javaScript) 자바스크립트 AWS Access Key ID 액세스 키 가 마지막으로 사용된 이력 조회 수행 - GetAccessKeyLastUsed

투케이2K 2026. 1. 10. 10:12
728x90

[개발 환경 설정]

개발 툴 : Edit++

개발 언어 : JavaScript

 

[소스 코드]

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

- 개발 환경 : Web


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


- 사전) AWS IAM 자격 증명 설명 정리 : 

  >> AWS IAM 아이엠 계정 은 일부 AWS 서비스 및 리소스에 대한 액세스 권한을 가지고 있는 자격 증명입니다

  >> IAM 아이엠 계정 은 ROOT 계정 혹은 다른 IAM 계정으로부터 권한을 부여 받을 수 있으며, 주어진 권한 내의 작업만 할 수 있습니다

  >> IAM 아이엠 계정에서 할당 된 권한 외의 작업이 필요한 경우 ROOT 계정으로부터 추가 액세스 접근 권한을 부여 받아야 사용할 수 있습니다

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





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

<!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 = {
              AccessKeyId: accessKey
            };


            // -----------------------------------------
            // [GetAccessKeyLastUsed] : AWS AccessKey 마지막 사용 이력 정보 확인
            // -----------------------------------------
            // AWS 참고 사이트 : https://docs.aws.amazon.com/IAM/latest/APIReference/API_GetAccessKeyLastUsed.html
            // -----------------------------------------
            // 권한 요구사항: iam:GetAccessKeyLastUsed 권한이 필요.
            // -----------------------------------------
            // 리전 무관 : 모든 리전에서 동일하게 동작.
            // -----------------------------------------
            aws.getAccessKeyLastUsed( param , function(err, data) { 
              if (err) {
                console.error("");
                console.error("=========================================");
                console.error("[getAccessKeyLastUsed] : [Error]");
                console.error("---------------------------------------");
                console.error(err);
                console.error("---------------------------------------");
                console.error(err.code);
                console.error("---------------------------------------");
                console.error(err.message);		
                console.error("=========================================");
                console.error("");

                // ---------------------------------------------
                // ✅ [주요 에러 정리]
                // ---------------------------------------------
                // AccessDenied : 호출 주체가 iam:GetAccessKeyLastUsed 권한이 없거나 정책에서 거부됨
                // NoSuchEntity : 지정한 AccessKeyId가 존재하지 않음 (삭제되었거나 잘못된 키)
                // InvalidInput : 잘못된 형식의 AccessKeyId를 전달한 경우
                // ThrottlingAPI : 호출이 너무 많아 제한된 경우
                // InternalFailure : AWS 내부 오류
                // ---------------------------------------------


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

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


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

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

                // ---------------------------------------------
                // ✅ [로그 출력 예시 첨부]
                // ---------------------------------------------
                /*
                {
                  "ResponseMetadata": {
                    "RequestId": "7210a581-8e76-448e-99cd-b925b23c94ea"
                  },
                  "UserName": "2k@twok.com-CLI",
                  "AccessKeyLastUsed": {
                    "LastUsedDate": "2026-01-09T07:22:00.000Z",
                    "ServiceName": "iot",
                    "Region": "ap-northeast-2"
                  }
                }
                */
                // ---------------------------------------------


                // ---------------------------------------------
                // [Body 표시 JSON]
                // ---------------------------------------------
                var resJson = {
                  respones: "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 = {
              respones: "exception",
              data: exception.message
            }
            // ---------------------------------------------


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

          }

        };

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

    </script>


</head>


<body>

</body>

</html>

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





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

[로그인 계정] ROOT 루트 계정 로그인과 IAM 아이엠 계정 로그인 차이 설명 정리

https://kkh0977.tistory.com/7618

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


[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