๐Ÿฆ "serverless" ๋กœ ๋‚ ์•„๋‹ค๋‹ˆ๊ธฐ

jwn4492ยท2020๋…„ 6์›” 21์ผ
22
post-thumbnail

๐Ÿฏ serverless ์‚ฌ์šฉํ•˜๋ฉด์„œ ์žฅ์ ๊ณผ ๊ฟ€ํŒ, ์‚ฝ์งˆ๊ธฐ๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

์‹œ์ž‘์— ์•ž์„œ์„œ

serverless ๋Š” ๋™์ ์œผ๋กœ ๋จธ์‹  ์ž์›์„ ํ™œ๋‹นํ•˜๊ณ  ์‚ฌ์šฉํ•œ ๋งŒํผ ๋ˆ์„ ๋‚ด๋Š” ํด๋ผ์šฐ๋“œ ์ปดํ“จํŒ… ์‹คํ–‰๋ชจ๋ธ๋กœ ๊ฐœ๋ฐœ์ž๋Š”
์„œ๋ฒ„์— ๋Œ€ํ•ด ์‹ ๊ฒฝ์„ ์•ˆ ์“ฐ๊ณ  ๊ฐœ๋ฐœ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ์„œ๋น„์Šค ์ž…๋‹ˆ๋‹ค.

์ด ๊ธ€์—์„œ๋Š” AWS ๋žŒ๋‹ค์™€ ์„œ๋ฒ„๋ฆฌ์Šค ๊ด€๋ฆฌ๋„๊ตฌ์ธ serverless ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

serverless ๋ฅผ ๋„์ž…ํ•˜๊ณ  ์–ป์€ ์žฅ์ 

๐Ÿ’ฐ ๋น„์šฉ ์ ˆ์•ฝ

์ œ๊ฐ€ ์šด์˜ํ•˜๊ณ  ์žˆ๋Š” ์ฑ—๋ด‡ ์„œ๋น„์Šค๋Š” ํ•˜๋ฃจ์— ์•ฝ 200๊ฐœ ์ •๋„์˜ ์š”์ฒญ๋งŒ ์˜ค๋Š” ์•„์ฃผ ์ž‘์€ ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค.
serverless ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์—๋Š” GCP ์—์„œ ๊ฐ€์žฅ ๋‚ฎ์€ ์‚ฌ์–‘์ธ f1-micro( vCPU 1๊ฐœ, 0.6GB ๋ฉ”๋ชจ๋ฆฌ )
์—์„œ ์ž‘๋™ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋งค๋‹ฌ ์•ฝ 8000์› ์ •๋„์˜ ๋น„์šฉ์ด์˜€๋˜ ๊ฑธ๋กœ ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค. ( ํ”„๋ฆฌํ‹ฐ์–ด ์˜€... )
์ด๊ฑธ ์„œ๋ฒ„๋ฆฌ์Šค๋กœ ์˜ฎ๊ฒจ ๊ฐ€๊ฒฉ์„ ์ ˆ๊ฐํ•˜๊ณ ์ž ํ–ˆ๊ณ  ๋น„๊ต์  ์„œ๋ฒ„๋ฆฌ์Šค๋ฅผ ์ž˜ ์ง€์›ํ•˜๊ณ  ์žˆ๋˜ AWS ๋กœ ์ด์‚ฌ๋ฅผ ๊ฐ€๊ธฐ๋กœ
๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ์—ด์‹ฌํžˆ express ์ฝ”๋“œ๋ฅผ ํ•จ์ˆ˜๋ณ„๋กœ ๋‚˜๋ˆ„์–ด serverless ์— ์˜ฌ๋ฆฌ๋‹ˆ ํ”„๋ฆฌํ‹ฐ์–ด๋ฅผ ์ œ์™ธํ•˜๊ณ  ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ
(monthly): 0.03 USD ๋ผ๋Š” ์•ฝ 36์› ์ด๋ผ๋Š” ์–ด๋งˆ์–ด๋งˆํ•œ ๋‚ฎ์€ ๊ฐ€๊ฒฉ์œผ๋กœ ์„œ๋น„์Šค๋ฅผ ์šด์˜ ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๐ŸŒŸ ์™„์ „ ์‰ฌ์šด ๋ฐฐํฌ

๋‹จ์ง€ ๊ฐ€๊ฒฉ์˜ ์žฅ์ ๋งŒ ์ƒ๊ฐํ•˜๊ณ  ์žˆ๋˜ ์ €๋Š” serverless ๋กœ ๋„˜์–ด์™€ ์™œ ์ž๋™ ๋ฐฐํฌ ๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š”์ง€ ์•Œ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
sls deploy ๋ช…๋ น์–ด ํ•˜๋‚˜๋กœ ์ž๋™์œผ๋กœ ๋ฐฐํฌ๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ๋ณด๊ณ  ์—„์ฒญ ํŽธํ–ˆ์Šต๋‹ˆ๋‹ค.
์˜ˆ์ „์—๋Š” ์ปดํ“จํ„ฐ ์—”์ง„์„ ์‚ฌ์šฉํ•  ๋•Œ ssh๋กœ ์—ฐ๊ฒฐํ•˜๊ณ  github ์— ์˜ฌ๋ฆฌ๊ณ  clone ํ•˜๊ณ ๋ฅผ ๋ฐ˜๋ณตํ–ˆ๋Š”๋ฐ
๋ช…๋ น์–ด ํ•œ๊ฐœ๋กœ ์ž๋™์œผ๋กœ ๋ฐฐํฌ๋˜๋‹ˆ ์—…๋ฐ์ดํŠธ๋„ ์žฆ์•„์ง€๋ฉด์„œ ์˜ค๋ฅ˜ ํ•ด๊ฒฐ๋„ ๋น ๋ฅด๊ณ  ์š”์ฒญํ•œ ๊ธฐ๋Šฅ๋„ ๋น ๋ฅด๊ฒŒ ๋งŒ๋“ค๊ณ  ๋ฐฐํฌํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ๋งŒ์กฑ๋„๊ฐ€ ๋†’์•„์กŒ์Šต๋‹ˆ๋‹ค.

๐Ÿ“ˆ ๋Œ€์‹œ๋ณด๋“œ์™€ ๐Ÿ“„ ๋กœ๊ทธ

์ปดํ“จํ„ฐ ์—”์ง„์„ ์‚ฌ์šฉํ• ๋•Œ๋Š” ์š”์ฒญ ์ˆ˜, ์˜ค๋ฅ˜ ๋ฐœ์ƒ, ์‘๋‹ต ์†๋„๊ฐ€ ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€ ํ™•์ธ ํ•  ๋ฐฉ๋ฒ•์ด ์—†์—ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‹ค๊ฐ€ serverless ๋กœ ๋„˜์–ด์˜ค๋‹ˆ ๋ฐ‘์— ์‚ฌ์ง„์ฒ˜๋Ÿผ ํ•œ๋ˆˆ์— ๋ณด์—ฌ์ฃผ๋‹ˆ ์„œ๋น„์Šค์˜ ์‚ฌ์šฉ๋„๋ž‘ ์˜ค๋ฅ˜ ์ˆ˜๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์–ด์„œ ํŽธํ–ˆ์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ ์ „์—๋Š” pm2๋ฅผ ์ด์šฉํ•ด์„œ ๋กœ๊ทธ๋ฅผ ๋‚จ๊ฒผ๋Š”๋ฐ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ด์•ผ ํ• ๋•Œ๋งˆ๋‹ค ssh๋กœ ์—ฐ๊ฒฐํ•ด์•ผํ•˜๋Š”๊ฒŒ ๊ท€์ฐฎ์•˜๊ณ 
vim์„ ํ†ตํ•ด ๋กœ๊ทธ๋ฅผ ๋ณด๋Š”๊ฒŒ ๋ถˆํŽธํ–ˆ์Šต๋‹ˆ๋‹ค. AWS lambda ์—์„œ๋Š” ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ๋งŒ ํ•ด๋„ ๋ฐ‘์— ์‚ฌ์ง„์ฒ˜๋Ÿผ ๋กœ๊ทธ๋ฅผ ์ฐ์–ด์ฃผ๋Š”๊ฒŒ ํŽธํ–ˆ์Šต๋‹ˆ๋‹ค.

์–ป์€ ๋‹จ์ ...

๐Ÿ”ฅ ์ดˆ๊ธฐ ์ง€์—ฐ ์‹œ๊ฐ„

AWS lambda ๋Š” ์š”์ฒญ์ด ์—ฐ์†์ ์œผ๋กœ ์˜ค์ง€ ์•Š์œผ๋ฉด Cold ์ƒํƒœ๊ฐ€ ๋˜์–ด Response ๋Šฆ์–ด์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ๋„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์–ธ์–ด๋ณ„๋กœ ๋‹ค๋ฅด๊ธด ํ•˜์ง€๋งŒ Node.js ๋Š” ๋น ๋ฅธํŽธ์ด์—ฌ์„œ ์‹ ๊ฒฝ ์“ธ ์ •๋„๋Š” ์•„๋‹ˆ์˜€์Šต๋‹ˆ๋‹ค.

๊ฟ€ํŒ

๐Ÿ“ฆ webpack

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์‹คํ–‰์†๋„๋ฅผ ๋น ๋ฅด๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด webpack ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค.
์›๋ž˜ ๋ฐฑ์—”๋“œ๋Š” ๋”ฐ๋กœ ์ฝ”๋“œ ์••์ถ•์ด ํ•„์š”์—†์—ˆ์ง€๋งŒ ๋žŒ๋‹ค๋Š” ๋”ฐ๋กœ๋”ฐ๋กœ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— webpack์„ ์‚ฌ์šฉํ•ด์„œ ์ฝ”๋“œ ์ฝ๋Š” ์‹œ๊ฐ„์„ ๋‹จ์ถ•์‹œ์ผœ ์ „๋ณด๋‹ค ๋น ๋ฅธ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

serverless ํ”Œ๋Ÿฌ๊ทธ์ธ์ธ serverless-webpack ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๐ŸŒŽ serverless-http

TMI velog๋„ serverless-http๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด์š”!

serverless ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜์‹ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ์ƒ์‚ฐ์„ฑ ํ•˜๋ฝ, ํ•จ์ˆ˜ ๊ฐœ์ˆ˜ ์ œํ•œ ๋ฐ ํ”Œ๋žซํผ์— ์ข…์†๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ํด๋ผ์šฐ๋“œ๋กœ ์ด์‚ฌ๋ฅผ ๊ฐˆ๋•Œ ๋‹ค์‹œ ์ž‘์„ฑํ•ด์•ผ ํ•  ๊ฒฝ์šฐ๋„ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿด ๋•Œ servereless ๋ฅผ express, koa ๊ฐ™์€ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ์š”
serverless-http ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค

// src/serverless.ts
import serverless from 'serverless-http';
import { APIGatewayProxyHandler } from 'aws-lambda';

import app from './app';

const serverlessApp = serverless(app);

export const handler: APIGatewayProxyHandler = async(event, context) => {
  const response = await serverlessApp(event, context);

  return response;
};
# serverless.yml

functions:
  app:
    handler: src/serverless.handler
    events:
      - http:
          path: /
          method: ANY
      - http:
          path: /{any+}
          method: ANY

์œ„ ์ฝ”๋“œ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด express, koa๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ๋„ serverelss๋ฅผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

( + express ๋Š” ์‚ฌ์šฉํ•˜๊ธฐ ๋ฌด๊ฒ๋‹ค๋Š” ํ‰๊ฐ€๋„ ์กด์žฌํ•˜๊ณ  koa๋ณด๋‹ค ๋Š๋ฆฝ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‹คํ–‰์‹œ๊ฐ„์— ๋น„๋ก€ํ•ด ๊ฐ€๊ฒฉ์ด ์ธก์ •๋˜๋Š” serverless ์—์„œ๋Š” koa.js ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค. )

์ •๋ฆฌ

์œ„ ๊ฐ™์€ ์žฅ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๊ฐœ์ธ์ ์œผ๋กœ websocket ๊ณ„์† ์—ฐ๊ฒฐํ•ด์•ผ ํ•˜๋Š” ์ผ์ด
์•„๋‹ˆ๋ฉด serverless ์‚ฌ์šฉ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค!

๋” ์ฝ์–ด๋ณด๊ธฐ

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2020๋…„ 7์›” 14์ผ

db์—ฐ๊ฒฐ๋„ ํ•˜์…ง๋‚˜์š”?

1๊ฐœ์˜ ๋‹ต๊ธ€