AWS ๊ธฐ๋ฐ ์๋ฒ๋ฆฌ์ค ์ํคํ ์ฒ (p149)
- Lambda์์๋ ์ฌ์ฉ์๊ฐ ํจ์ ์ฝ๋๋ฅผ ์ ๊ณตํ๊ณ , Lambda๋ ์๊ตฌ์ ๋ฐ๋ผ ๊ทธ ํจ์๋ฅผ ์คํํ๋ค.
- ์ฌ์ฉ์๋ ๊ทธ ํจ์๊ฐ ์ด๋ป๊ฒ ๊ทธ๋ฆฌ๊ณ ์ด๋์ ์คํ๋๋์ง ๋ชจ๋ฅธ๋ค. (serverless)
์ค์ต : S3 ๋ฒํท์ ์ ์ฅ๋ ํ์ผ ๋ชฉ๋ก์ ๋ฐํํ๋ ๋๋ค ํจ์๋ฅผ ์์ฑ, ๋ฐฐํฌ (P166)
CLI ํ๊ฒฝ์์ ๋๋ค ํจ์๋ฅผ ์์ฑํ๊ณ , ๋ฐฐํฌ โ CreateFunction ๊ถํ์ด ํ์
-
์์ ๋๋ ํฐ๋ฆฌ ์์ฑ ๋ฐ ํ๋ก์ ํธ ์ด๊ธฐํ
cd C:\serverless
mkdir get-video-list
d get-video-list
npm init -y
-
ํ์ ๋ชจ๋ ์ถ๊ฐ
npm install aws-sdk
npm install async
- async๋ ๋น๋๊ธฐ ํต์ ์ ์ง์ํด์ฃผ๋ javascript ๋ชจ๋
-
package.json ํ์ผ์ create, precreate ์คํฌ๋ฆฝํธ๋ฅผ ์ถ๊ฐ
-
package.json
{ "name": "get-video-list", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "create": "aws lambda create-function --function-name get-vide-list --handler index.handler --memory-size 128 --runtime nodejs4.3 --role ๋ณธ์ธ์_lambda-s3-execution-role-ARN --timeout 3 --publish --zip-file fileb://Lambda-Deployment.zip", "precreate": "zip -r Lambda-Deployment.zip * -x *.zip *.log node_modules/aws-sdk/*" }, "keywords": [], "author": "", "license": "ISC" }
-
-
async ๋ชจ๋์ watefall() ํจ์ ์ฌ์ฉ๋ฒ
-
`async_test.js
// https://caolan.github.io/async/v3/docs.html#waterfall var async = require("async"); async.waterfall([ function(firstcallbackfunc) { console.log(`์ฒซ๋ฒ์งธ ํจ์`); firstcallbackfunc(null, "Peter", "Sam"); }, function(a1, a2, secondcallbackfunc) { console.log(`๋๋ฒ์งธ ํจ์ ${a1}, ${a2}`); secondcallbackfunc(null, "Serverless"); }, function(a3, thirdcallbackfunc) { console.log(`์ธ๋ฒ์งธ ํจ์ ${a3}`); thirdcallbackfunc(null, "Done") } ], function(err, result) { console.log(`์ต์ข ์ฝ๋ฐฑ ${err}, ${result}`); });
-
-
๋๋ค ํจ์ ์์ฑ
-
index.js
// P172 'use strict'; // ํ์ ๋ชจ๋ ์ถ๊ฐ ๋ฐ S3 ๊ฐ์ฒด ์์ฑ var AWS = require('aws-sdk'); var async = require('async'); var s3 = new AWS.S3(); // next : callback ํจ์ // next(ERROR, DATAS, ...) // next(null, ...) ==> ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์์ผ๋ฉฐ, ์ด๋ค ๊ฐ์ ๋ฐํ // next(์ด๋ค๊ฐ) ==> callback ํจ์๋ก ์ค๋ฅ๋ฅผ ๋ฐํ // S3.listObjects ํจ์ ํธ์ถ์ ์ฌ์ฉํ ์ ๋ ฅ ํฌ๋งท์ ์์ฑ function createBucketParams(next) { var params = { Bucket: process.env.BUCKET, EncodingType: 'url' }; next(null, params); // #1 ํจ์๊ฐ ํธ์ถ } // #1 ๋ฒํท์ ๊ฐ์ฒด(ํ์ผ) ๋ชฉ๋ก์ ์กฐํ function getVideosFromBucket(params, next) { // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listObjects-property s3.listObjects(params, function(err, data) { if (err) { next(err); } else { next(null, data); // #2 ํจ์๊ฐ ํธ์ถ } }); } // #2 ๋ฒํท์ ๊ฐ์ฒด ๋ชฉ๋ก ์กฐํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ ํ์์ ๋ง์ถฐ์ ๋ณํ function createList(data, next) { console.log(data); // ๋ฒํท์ ๊ฐ์ฒด ์ด๋ฆ(ํด๋๋ช ๊ณผ ํ์ฅ์๋ฅผ ํฌํจ)์ ์ ์ฅํ ๋ฐฐ์ด var urls = []; for (var i = 0; i < data.Contents.length; i ++) { var file = data.Contents[i]; // https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/substr // ํค(๊ฐ์ฒด ์ด๋ฆ)์ ๋ง์ง๋ง ์ธ๊ธ์๊ฐ mp4์ธ ๊ฒฝ์ฐ if (file.Key && file.Key.substr(-3, 3) === 'mp4') { urls.push(file); } } var result = { baseUrl: process.env.BASE_URL, // ๋ฒํท ์ ๊ทผ URL bucket: process.env.BUCKET, urls: urls }; next(null, result); // #3 ํจ์๋ก ์ ๋ฌ } exports.handler = function(event, context, callback) { async.waterfall([ createBucketParams, getVideosFromBucket, createList ], // #3 ํจ์ : [ ... ]์ ์ ์๋ ํจ์๊ฐ ๋ชจ๋ ์ ์ ์ํ ๋๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ์ ํธ์ถ function(err, result) { if (err) { callback(err); } else { callback(null, result); // ๋ฒํท์ ์ ์ฅ๋ ๊ฐ์ฒด ๋ชฉ๋ก์ ๋ฒํท ์ ์ ์ฃผ์, ๋ฒํท ARN๊ณผ ํจ๊ป ๋ฐํ } }); };
-
-
๋๋ค ํจ์ ์์ฑ ๋ฐ ๋ฐฐํฌ
-
npm run create
: An error occurred (InvalidParameterValueException) when calling the CreateFunction operation: The runtime parameter of nodejs4.3 is no longer supported for creating or updating AWS Lambda functions. We recommend you use the new runtime (nodejs12.x) while creating or updating functions. โ ๋ฐํ์ ๋ฒ์ ์ด ๋ฎ์์ ํจ์๋ฅผ ์์ฑํ ์ ์์ npm ERR! code ELIFECYCLE npm ERR! errno 254 npm ERR! get-video-list@1.0.0 create: `aws lambda create-function --function-name get-video-list --handler index.handler --memory-size 128 --runtime nodejs4.3 --role arn:aws:iam::199503606661:role/lambda-s3-execution-role --timeout 3 --publish --zip-file fileb://Lambda-Deployment.zip` npm ERR! Exit status 254 npm ERR! npm ERR! Failed at the get-video-list@1.0.0 create script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\i\AppData\Roaming\npm-cache\_logs\2021-03-17T06_42_58_746Z-debug.log
-
-
๋ฐํ์ ๋ฒ์ ๋ณ๊ฒฝ ํ ๋ค์ ๋๋ค ํจ์ ์์ฑ ๋ฐ ๋ฐฐํฌ
-
ํด๋น Lambda์์ ํ๊ฒฝ ๋ณ์ ํธ์ง
- BUCKET ํ๊ฒฝ ๋ณ์ โ ํธ๋์ค ์ฝ๋ฉ๋ ๊ฒฐ๊ณผ๊ฐ ์ ์ฅ๋ ๋ฒํท ์ด๋ฆ
- BASE_URL ํ๊ฒฝ ๋ณ์ โ ํธ๋์ค ์ฝ๋ฉ๋ ๊ฐ์ฒด์ ๊ฐ์ฒด URL์์ ํค๋ฅผ ์ ์ธํ ๋ถ๋ถ โ https://๋ฒํท์ด๋ฆ.s3.amazonaws.com
ํด๋น ํจ์๋ ์ธ๋ถ(ํจ์๋ฅผ ํธ์ถํ๋ ๊ณณ)์์ ์ ๋ฌํ๋ ๊ฐ์ ์ฌ์ฉํ๋ ๋ถ๋ถ์ด ์์ โ event ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๋ถ๋ถ์ด ์์โ ํ ์คํธ๋ฅผ ์ํํ ๋ ๋ณ๋์ ๊ฐ ์ค์ ์ด ํ์ ์์
์์ฝ:
waterfall์ ๊ฐ์ ํจํด ๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ฑ
๋ก์ปฌ ๋ฐ AWS์์ ํ๋ Lambda ํจ์ ํ ์คํธ