Notice
Recent Posts
Recent Comments
Link
투케이2K
224. (NodeJs) [AWS] [Lambda] 람다 함수 기본 내장 https 모듈 사용해 get api 호출 및 결과 확인 본문
NodeJs
224. (NodeJs) [AWS] [Lambda] 람다 함수 기본 내장 https 모듈 사용해 get api 호출 및 결과 확인
투케이2K 2025. 7. 3. 21:27728x90
[개발 환경 설정]
개발 툴 : VS CODE
개발 언어 :NodeJs

[소스 코드]
// --------------------------------------------------------------------------------------
[개발 및 테스트 환경]
// --------------------------------------------------------------------------------------
- 언어 : NodeJs
- 개발 툴 : VsCode
- 개발 기술 : AWS Lambda 이벤트 동작 함수
- 사전) AWS Lambda 설명 :
>> Aws Lambda 는 서버 리스 FaaS 솔루션으로, 함수의 인스턴스를 실행하여 이벤트를 처리할 수 있습니다
>> Aws Lambda 는 이벤트에 응답하여 코드를 실행 하고 해당 코드에 필요한 컴퓨팅 리소스를 자동으로 관리합니다
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[node.js 22 x 버전 : index.mjs : 소스 코드]
// --------------------------------------------------------------------------------------
// ========================================================================
// [Aws] : [https 모듈] : Get 방식 API 호출 및 응답 결과 확인
// ========================================================================
/**
* 1. index.mjs : 핸들러 동작 메인 함수
* >> (mjs 파일) 모듈 불러오기 : import AWS from 'https';
* >> (mjs 파일) 모듈 내보내기 : export const handler / export default handler
* 2. node.js 런타임 환경 22.x : https 모듈 기본 포함
* 3. 참고 사항 :
* >> http는 HTTP 요청용, https는 HTTPS 요청용입니다. 대부분의 API는 HTTPS를 사용하므로 https 모듈을 사용하는 것이 일반적입니다.
* */
// ========================================================================
//*
// ------------------------------------------------------------------------
// [import library]
// ------------------------------------------------------------------------
import https from 'https';
// ------------------------------------------------------------------------
// [Custom Function]
// ------------------------------------------------------------------------
function httpsGet(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
// [Response StatusCode]
const resStatusCode = res.statusCode;
console.log("[httpsGet] : [resStatusCode] : " + resStatusCode);
// [Response Data]
let resData = '';
res.on('data', (chunk) => {
resData += chunk; // Add Response Data
});
res.on('end', () => {
console.log("[httpsGet] : [responseData] : " + resData);
resolve({ resStatusCode, resData }); // CallBack Return
});
}).on('error', (err) => {
console.error("[httpsGet] : [Error] : " + err);
reject(err); // CallBack Return
});
});
}
// ------------------------------------------------------------------------
// [Main Start]
// ------------------------------------------------------------------------
export const handler = async (event) => {
try {
// -----------------------------------------
// [HTTPS 호출 URL 지정]
// -----------------------------------------
const url = 'https://jsonplaceholder.typicode.com/posts?userId=1&id=1';
// -----------------------------------------
// [await https 호출 수행]
// -----------------------------------------
const { resStatusCode, resData } = await httpsGet(url);
console.log("");
console.log("=========================================");
console.log("[Https] : [Success]");
console.log("---------------------------------------");
console.log("resStatusCode :: " + resStatusCode);
console.log("resData :: " + resData);
console.log("=========================================");
console.log("");
// -----------------------------------------
// [Return Response]
// -----------------------------------------
return {
statusCode: resStatusCode,
body: {
response: resData
}
}
} catch(err){
console.error("");
console.error("=========================================");
console.error("[Https] : [Exception]");
console.error("---------------------------------------");
console.error(err);
console.error("=========================================");
console.error("");
// [Exception 500 Error]
return {
statusCode: 500,
body: {
error: err.stack
}
}
}
};
// */
// ========================================================================
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[참고 사이트]
// --------------------------------------------------------------------------------------
[Amazon API Gateway] Aws API Gateway 게이트웨이 설명 정리 - 중개 서버
https://blog.naver.com/kkh0977/223827753479
[Amazon API Gateway] Aws API Gateway 게이트웨이 API 엔드포인트 유형 정리
https://blog.naver.com/kkh0977/223911565693
[Aws Lambda] Aws 사이트에서 생성 된 Lambda 람다 검증 함수 리스트 및 내용 소스 코드 확인 방법
https://blog.naver.com/kkh0977/223765198383
// --------------------------------------------------------------------------------------
728x90
반응형
'NodeJs' 카테고리의 다른 글
Comments
