๊ด€๋ฆฌ ๋ฉ”๋‰ด

DATA101

ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (6) AWS Lambda ํ•จ์ˆ˜ ์ž‘์„ฑ ๋ณธ๋ฌธ

SW ๊ฐœ๋ฐœ/Amazon Web Service

ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (6) AWS Lambda ํ•จ์ˆ˜ ์ž‘์„ฑ

DATA101 2022. 6. 10. 03:02
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ ์‹œ๋ฆฌ์ฆˆ ํฌ์ŠคํŒ…

1.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (1) React ๊ธฐ๋ฐ˜ Frontend ๊ฐœ๋ฐœ
2.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (2) AWS IAM ์—ญํ•  ์ƒ์„ฑ
3.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (3) AWS Lambda ์ƒ์„ฑ
4.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (4) AWS S3 ์ƒ์„ฑ
5.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (5) AWS API Gateway ์ƒ์„ฑ
6.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (6) AWS Lambda ํ•จ์ˆ˜ ์ž‘์„ฑ
7.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (7) Axios ๊ธฐ๋ฐ˜ API Call
8.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (8) ํŒŒ์ผ ์—…๋กœ๋“œ ํ…Œ์ŠคํŠธ
9.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (9) AWS S3 ๊ธฐ๋ฐ˜ ์›น ํ˜ธ์ŠคํŒ…

๐Ÿ‘จโ€๐Ÿ’ป ๋“ค์–ด๊ฐ€๋ฉฐ

๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” Backend ์—ญํ• ๋กœ์จ Lambda ํ•จ์ˆ˜ ์ž‘์„ฑ ์ฝ”๋“œ๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค.

1.  ์ „์ฒด ์ฝ”๋“œ

Lambda ํ•จ์ˆ˜์— ์ž‘์„ฑํ•ด์•ผ ํ•  ์ „์ฒด ์ฝ”๋“œ๋Š” ์•„๋ž˜์˜ Github์— ์—…๋กœ๋“œํ•˜์˜€์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

https://github.com/park-gb/file-uploader-backend

 

park-gb/file-uploader-backend

AWS์™€ React๋ฅผ ํ™œ์šฉํ•œ ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌํ˜„: Backend. Contribute to park-gb/file-uploader-backend development by creating an account on GitHub.

github.com

์•ž์„œ ์ƒ์„ฑํ–ˆ๋˜ Lambda ํ•จ์ˆ˜ ๋‚ด ์ฝ”๋“œ๋Š” ์•„๋ž˜์˜ ๊ทธ๋ฆผ 1๊ณผ ๊ฐ™์ด 3๊ฐœ์˜ ์†Œ์Šค ํŒŒ์ผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆผ 1. Backend ๋žŒ๋‹ค ํ•จ์ˆ˜ ๊ตฌ์„ฑ ์ฝ”๋“œ

2.  ์†Œ์Šค ํŒŒ์ผ

2.1.  ์ธ๋ฑ์Šค ํŒŒ์ผ

3๋ฒˆ์งธ ๋ผ์ธ์— ์ƒ์„ฑํ•œ API ๊ฒŒ์ดํŠธ์›จ์ด ๋ฆฌ์†Œ์Šค ์ด๋ฆ„์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ๊ฐ€ POST์ด๋ฉด์„œ ์ƒ์„ฑํ•œ API ๊ฒŒ์ดํŠธ์›จ์ด ๋ฆฌ์†Œ์Šค ์ด๋ฆ„๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ, ํŒŒ์ผ ํ”„๋กœ์„ธ์‹ฑ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.

// index.js
const fileProcessingService = require('./file-processing-service');
const util = require('./util');
const fileUploadPath = '/file-upload'; // API ๊ฒŒ์ดํŠธ์›จ์ด ๋ฆฌ์†Œ์Šค ์ด๋ฆ„

exports.handler = async (event) => {
    console.log('Request Event: ', event);
    let response;
    switch(true){
        case event.httpMethod === 'POST' && event.path === fileUploadPath:
            response = await fileProcessingService.process(event.body);
            break;
        default:
            response = util.buildResponse(404);
    }
    return response;
};

2.2.  ํŒŒ์ผ ํ”„๋กœ์„ธ์‹ฑ ํŒŒ์ผ

5๋ฒˆ์งธ ๋ผ์ธ์— ์•ž์„œ ์ƒ์„ฑํ•œ S3 ๋ฒ„ํ‚ท์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์—…๋กœ๋“œ ํ•œ ํŒŒ์ผ์„ ๊ฐ์ฒด๋กœ ๋ฐ›์•„ S3๋กœ put ํ•ฉ๋‹ˆ๋‹ค.

// file-processing-service.js
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const util = require("./util");

const bucketName = '์ƒ์„ฑํ•œ S3 ๋ฒ„ํ‚ท ์ด๋ฆ„ ์ž…๋ ฅ'; // S3 ๋ฒ„ํ‚ท์ด๋ฆ„ ์ž…๋ ฅ
const s3Subfolder = 'data';

async function process(requestBody){
    const fileName = requestBody.split('\r\n')[1].split(';')[2].split('=')[1].replace(/^"|$/g, '').trim();
    let fileContent = requestBody.split('\r\n')[4].trim();
    fileContent += `\n\nProcess Timestap: ${new Date().toISOString()}`
    const params = {
        Bucket: bucketName,
        Key: `${s3Subfolder}/${fileName}`,
        Body: fileContent
    }
    await s3.putObject(params).promise();
    return util.buildResponse(200,requestBody);
}

module.exports.process = process;

2.3. Response Builder ํŒŒ์ผ

๋ชจ๋“  ๋„๋ฉ”์ธ์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ธํŒ…ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

// util.js
function buildResponse(statusCode, body){
    return{
        statusCode: statusCode,
        headers:{
            'Access-Control-Allow-Origin' : '*',
            'Content-Type' : 'application/json'
        },
        body: JSON.stringify(body)
    }
}

module.exports.buildResponse = buildResponse;

์ด์ œ Backend๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๋žŒ๋‹ค ํ•จ์ˆ˜์˜ ํ•„์š”ํ•œ ์ฝ”๋“œ๋Š” ๋ชจ๋‘ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ ์•„๋ž˜์˜ ๊ทธ๋ฆผ 2์™€ ๊ฐ™์ด, Deploy(๋ฐฐํฌ) ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.

๊ทธ๋ฆผ 2. ๋ฐฐํฌ ๋ฒ„ํŠผ ํด๋ฆญ

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” ํ”„๋ก ํŠธ์—”๋“œ์—์„œ axios๋ฅผ ํ™œ์šฉํ•˜์—ฌ API๋ฅผ Callํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ์‹œ๋ฆฌ์ฆˆ ํฌ์ŠคํŒ…

1.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (1) React ๊ธฐ๋ฐ˜ Frontend ๊ฐœ๋ฐœ
2.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (2) AWS IAM ์—ญํ•  ์ƒ์„ฑ
3.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (3) AWS Lambda ์ƒ์„ฑ
4.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (4) AWS S3 ์ƒ์„ฑ
5.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (5) AWS API Gateway ์ƒ์„ฑ
6.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (6) AWS Lambda ํ•จ์ˆ˜ ์ž‘์„ฑ
7.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (7) Axios ๊ธฐ๋ฐ˜ API Call
8.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (8) ํŒŒ์ผ ์—…๋กœ๋“œ ํ…Œ์ŠคํŠธ
9.  ํŒŒ์ผ ์—…๋กœ๋“œ ์›น ํŽ˜์ด์ง€ ๊ตฌ์ถ•: (9) AWS S3 ๊ธฐ๋ฐ˜ ์›น ํ˜ธ์ŠคํŒ…

ํฌ์ŠคํŒ… ๋‚ด์šฉ์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ๊ฑฐ๋‚˜ ๋ณด์™„ํ•  ์ ์ด ์žˆ๋‹ค๋ฉด ์•„๋ž˜์— ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์„ธ์š”!

๊ทธ๋Ÿผ ์˜ค๋Š˜๋„ ์ฆ๊ฒ๊ณ  ๊ฑด๊ฐ•ํ•œ ํ•˜๋ฃจ ๋ณด๋‚ด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค :)

๊ณ ๋ง™์Šต๋‹ˆ๋‹ค๐Ÿ˜Š

728x90
๋ฐ˜์‘ํ˜•