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

ruriruriya

[Serverless] ์„œ๋ฒ„๋ฆฌ์Šค ํด๋” ์ƒ์„ฑ ํ›„ VSCODE ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ณธ๋ฌธ

๐Ÿ“ŒServerless

[Serverless] ์„œ๋ฒ„๋ฆฌ์Šค ํด๋” ์ƒ์„ฑ ํ›„ VSCODE ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ•

๋ฃจ๋ฆฌ์•ผใ…‘ 2023. 12. 15. 06:02
๋ฐ˜์‘ํ˜•

์„œ๋ฒ„๋ฆฌ์Šค๋กœ ํด๋”๋ฅผ ์ƒ์„ฑํ–ˆ์œผ๋‹ˆ
VSCODE์œผ๋กœ ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ ๋ฐฐํฌ๊นŒ์ง€ ํ•ด๋ณด์ž.

์„œ๋ฒ„๋ฆฌ์Šค๋กœ ํด๋” ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜ ํฌ์ŠคํŒ… ์ฐธ๊ณ 

https://ruriruriya.tistory.com/115

 

[Serverless]์„œ๋ฒ„๋ฆฌ์Šค๋กœ ํ”„๋กœ์ ํŠธ ํด๋” ๋งŒ๋“ค๊ธฐ

Serverless ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ํ”„๋กœ์ ํŠธ ํด๋”๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž. ํด๋” ์ƒ์„ฑ ์ดํ›„ git๊ณผ ์—ฐ๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. 1. cmd ์—์„œ ๊ฒฝ๋กœ ์ง€์ • ํ”„๋กœ์ ํŠธ ํด๋”๋ฅผ ๋งŒ๋“ค ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•œ๋‹ค. $ cd (์ด๋™ํ•  ๊ฒฝ๋กœ(ํด๋”๋ฅผ ๋งŒ๋“ค)) 2. severless

ruriruriya.tistory.com

 

1. VSCODE์—์„œ ํด๋” ์—ด๊ธฐ

VSCODE๋ฅผ ๋จผ์ € ์—ด๊ณ  

[File]- [Open Folder...] ํด๋ฆญํ•ด์„œ ํ•ด๋‹น ํด๋”๋ฅผ ์ง€์ •ํ•ด์„œ ์—ฐ๋‹ค.

2. serverless.yml ์ˆ˜์ •

severless.yml ์—์„œ provider ๋ฅผ ์ˆ˜์ • ๋ฐ ์ถ”๊ฐ€ํ•œ๋‹ค.

- runtime: ์€ ๊ธฐ๋ณธ python3.9๋กœ ๋˜์–ด ์žˆ๋Š”๋ฐ ๋ณธ์ธ์—๊ฒŒ ๋งž๋Š” ๊ฐ€์ƒํ™˜๊ฒฝ์˜ ํŒŒ์ด์ฌ ๋ฒ„์ „์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

- region :์€ ์„œ์šธ๋กœ ํ•œ๋‹ค. ap-northeast-2

๊ทธ๋ฆฌ๊ณ  ์ €์žฅ.

 

3. ๋กœ์ปฌ์—์„œ ํ…Œ์ŠคํŠธ

๋ฐฐํฌ ์ „์— ๋กœ์ปฌ์—์„œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•œ๋‹ค.

๋จผ์ € ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋ฅผ ํ•ด๋‹น ๊ฐ€์ƒํ™˜๊ฒฝ์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

VSCODE ํ„ฐ๋ฏธ๋„ Terminal - New Terminal ์—์„œ ๊ฐ€์ƒํ™˜๊ฒฝ์œผ๋กœ ์ด๋™ ํ›„
flask run  ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

ํฌ์ŠคํŠธ๋งจ์œผ๋กœ ์ด๋™ํ•ด์„œ

๋กœ์ปฌ url๊ณผ ํฌํŠธ๋ฒˆํ˜ธ๋ฅผ ๋„ฃ๊ณ  app.py ๋‚ด์˜ ํ•จ์ˆ˜ ์‹คํ–‰ํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•œ๋‹ค.

์•„๋ž˜๋Š” ์„œ๋ฒ„๋ฆฌ์Šค๋กœ ํด๋”๋ฅผ ๋งŒ๋“ค๋ฉด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” app.py ํŒŒ์ผ์ด๋‹ค.
์•ˆ์˜ ์ฝ”๋“œ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

from flask import Flask, jsonify, make_response

app = Flask(__name__)


@app.route("/")
def hello_from_root():
    return jsonify(message='Hello from root!')


@app.route("/hello")
def hello():
    return jsonify(message='Hello from path!')


@app.errorhandler(404)
def resource_not_found(e):
    return make_response(jsonify(error='Not found!'), 404)

ํฌ์ŠคํŠธ๋งจ(Postman)์—์„œ ๋กœ์ปฌ ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋‹ˆ
body์— ์„ฑ๊ณต ๋ฉ”์„ธ์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค..

4. AWS ๋ฐฐํฌ ๋ฐ ํ…Œ์ŠคํŠธ

sls deploy ๋ช…๋ น์–ด ์‹คํ–‰์œผ๋กœ ๋ฐฐํฌํ•˜์—ฌ
endpoint ์ฃผ์†Œ๋กœ ๋‹ค์‹œ ํฌ์ŠคํŠธ๋งจ์—์„œ ํ…Œ์ŠคํŠธ ํ•œ๋‹ค.

ํฌํŠธ์Šค๋งจ์—์„œ endpoint url ๋กœ ํ…Œ์ŠคํŠธ ์„ฑ๊ณต!

๋ฐ˜์‘ํ˜•