์ด์ „ lab์„ ํ†ตํ•ด IBM Cloud Functions๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์•Œ์•„๋ณด์•˜๋‹ค.
WEB GUI ๋„ ์จ๋ณด์•˜๊ณ , CLI ๋กœ๋„ ํ•ด๋ณด์•˜๋‹ค. ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ๊ฐœ๋ฐœ์— ๋“ค์–ด๊ฐ€๋ฉด gui ๋‚˜ cli๋งŒ์œผ๋กœ๋Š”ย  ๋ถˆํŽธํ•˜๋‹ค.
์ด๋ฅผ ์ข€ ๋” ํšจ๊ณผ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” severless framwork ๋ฅผ ํ™œ์šฉํ•ด ๋ณด์ž.

**๊ธฐ๋ณธ์ ์ธ ํ™˜๊ฒฝ ์…‹ํŒ…์€ ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ํ•„์š”ํ•œ ์‚ฌ๋žŒ์€ ์ด์ „ ๋žฉ์„ ๋ณด๊ณ  ํ™˜๊ฒฝ ๊ตฌ์„ฑ์„ ํ•˜์ž.
GO [IBM Cloud Functions CLI ์‚ฌ์šฉํ•ด๋ณด๊ธฐ] **์†Œ์Šค ์ฝ”๋“œ๋Š” ์ด์ „ lab์—์„œ ์‚ฌ์šฉํ•œ ๊ฒƒ์„ ๊ทธ๋Œ€๋กœ ์“ฐ๊ฒ ๋‹ค. ํ•„์š”ํ•œ ์‚ฌ๋žŒ์€ ์ด์ „ ๋žฉ์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์ž.
GO [API gateway๋ฅผ ํ™œ์šฉํ•œ CRUD ๊ตฌํ˜„ํ•˜๊ธฐ]

๋ณธ ๊ณผ์ •์—์„œ๋Š” api gateway๋ฅผ ์ด์šฉํ•œ CRUD ๊ตฌํ˜„์‹ค์Šต์„ serverless framework๋ฅผ ํ†ตํ•ด์„œ ๊ตฌํ˜„ ํ•ด๋ณด๊ฒ ๋‹ค.

Step Summary
ยท serverless framework ์„ค์น˜
ยท serverless CRUD rest api ๊ตฌํ˜„ ๋ฐ deploy
ยท Test

[1st: Serverless Framework ์„ค์น˜]

1. bx login
ibm cloud(bluemix)์— ๋กœ๊ทธ์ธ ํ•œ๋‹ค.
$bx login -a api.ng.bluemix.net -o “yjh@kr.ibm.com” -s “korea”

bluemix single sign on ์„ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ์•„๋ž˜ command
$bx login -a api.ng.bluemix.net -o “yjh@kr.ibm.com” -s “korea” –sso

2. verify
์•„๋ž˜ ์ปค๋งจ๋“œ ์ˆ˜ํ–‰๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ๋‚˜์˜จ๋‹ค๋ฉด ์„ฑ๊ณต.
$bx wsk action invoke /whisk.system/utils/echo -p message hello –result
{“message”: “hello”ย }

3. serverless package๋ฅผ ์„ค์น˜ํ•œ๋‹ค.
$npm install -g serverless serverless-openwhisk

4. verify
์•„๋ž˜ ์ปค๋งจ๋“œ๊ฐ€ ์ˆ˜ํ–‰ ๋˜๋ฉด ์ž˜ ์„ค์น˜ ๋œ ๊ฒƒ์ด๋‹ค.
$serverless

5. nodejs project ์ƒ์„ฑ
์ด์ œ nodejs application์„ ๋งŒ๋“ค์–ด ๋ณด์ž.
$serverless create –template openwhisk-nodejs

์•„๋ž˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์€ ํŒŒ์ผ์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ ๋  ๊ฒƒ์ด๋‹ค.

[2nd: serverless.yml ๊ตฌ์„ฑ]
1. action ์†Œ์Šค ์ฝ”๋“œ ์ƒ์„ฑ
์ด์ „์— ์‚ฌ์šฉ ํ–ˆ๋˜ ํŒŒ์ผ์„ copy ํ•ด ์˜จ๋‹ค.
create-cat.js / read-cat.js / update-cat.js / delete-cat.js

2. serverless.yml ํŒŒ์ผ์„ editor์—์„œ ์—ฐ๋‹ค.
๋ชจ๋“  ์„ค์ •์„ ์ด ํŒŒ์ผ์— ๋ช…์‹œํ•ด์•ผ ํ•œ๋‹ค.
service ๋ถ€๋ถ„์— ์„œ๋น„์Šค๋ช…์„ ๊ธฐ์ž…ํ•œ๋‹ค. (์ž์‹ ์ด ์›ํ•˜๋Š” ์ด๋ฆ„์„ ๊ธฐ์ž… ํ•˜๋ฉด ๋œ๋‹ค)
service: serverless-rest-lab

functions ๋ถ€๋ถ„์„ ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™๊ฒŒ ์ˆ˜์ •ํ•œ๋‹ค.
handler ๋ถ€๋ถ„์— CRUD funtion์„ ๋ช…์‹œํ•˜๊ณ  events๋ถ€๋ถ„์— endpoint๋ฅผ method์™€ ํ•จ๊ป˜ ์ง€์ •ํ–ˆ๋‹ค.

functions:
ย  createCat:
ย ย ย  handler: create-cat.main
ย ย ย  parameters:
ย ย ย ย ย  CLOUDANT_DATABASE: "cats"
ย ย ย ย ย  CLOUDANT_INSTANCE: "Cloudant-Serverless"
ย ย ย ย ย  CLOUDANT_PASSWORD: "00e718ad40b4f0b074a01d7xxxxxd4be2d7e020077cced63"
ย ย ย ย ย  CLOUDANT_USERNAME: "e4ecfa2c-xxxxxxx7b5-32561351b6d1-bluemix"

ย ย ย  events: 
ย ย ย ย ย  - http: POST /api/serverless-cat
ย ย ย  
ย  readCat:
ย ย ย  handler: read-cat.main
ย ย ย  parameters:
ย ย ย ย ย  CLOUDANT_DATABASE: "cats"
ย ย ย ย ย  CLOUDANT_INSTANCE: "Cloudant-Serverless"
ย ย ย ย ย  CLOUDANT_PASSWORD: "00e718axxxxxxxxxda4c0663180d4be2d7e020077cced63"
ย ย ย ย ย  CLOUDANT_USERNAME: "e4ecfa2xxxxxxxxx7b5-32561351b6d1-bluemix"

ย ย ย  events: 
ย ย ย ย ย  - http: GET /api/serverless-cat

ย  updateCat:
ย ย ย  handler: update-cat.main
ย ย ย  parameters:
ย ย ย ย ย  CLOUDANT_DATABASE: "cats"
ย ย ย ย ย  CLOUDANT_INSTANCE: "Cloudant-Serverles
ย ย ย  ย ย CLOUDANT_PASSWORD: "00e718ad40b64f0b074a0xxxxxxe2d7e020077cced63"
ย ย ย ย ย  CLOUDANT_USERNAME: "e4ecxxxxxxxxb60-a7b5-32561351b6d1-bluemix"

ย ย ย  events: 
ย ย ย ย ย  - http: PUT /api/serverless-cat

ย  deleteCat:
ย ย ย  handler: delete-cat.main
ย ย ย  parameters:
ย ย ย ย ย  CLOUDANT_DATABASE: "cats"
ย ย ย ย ย  CLOUDANT_INSTANCE: "Cloudant-Serverless"
ย ย ย ย ย  CLOUDANT_PASSWORD: "00e718ad40b6dc94f0b074a01xxxxxxx4be2d7e020077cced63"
ย ย ย ย ย  CLOUDANT_USERNAME: "e4ecfa2c-282d-xxxxxx32561351b6d1-bluemix"

ย ย ย  events: 
ย ย ย ย ย  - http: DELETE /api/serverless-cat

ย 

3. dependency package install
dependency package๋ฅผ ์„ค์น˜ ํ•œ๋‹ค.
$npm install

4. deploy
$serverless deploy
๊ฒฐ๊ณผ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜ํƒ€๋‚œ๋‹ค.

๋งŒ์•ฝ์— ์†Œ์Šค์— ๋ฌธ์ œ๊ฐ€
์žˆ์–ด์„œ ์ˆ˜์ • ํ–ˆ๋‹ค๋ฉด
deploy๋ฅผ ๋‹ค์‹œ ํ•ด์•ผ ๋œ๋‹ค. (์‹ค์ œ ๊ฐœ๋ฐœ ์‹œ ๋งค๋ฒˆ deploy๋ฅผ ๋‹ค์‹œ ํ•˜๋Š”๊ฒŒ ๋ถˆํŽธ ํ•œ๋ฐ ์ด๋ถ€๋ถ„์€ serverless-offline ์ด๋ž€ package๋กœ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ์™ธ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํˆด๋“ค์„ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ์ข€ ๋” ํŽธํ•˜๊ฒŒ ๊ฐœ๋ฐœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฐจ์ฐจ ์†Œ๊ฐœ ํ•˜๊ธฐ๋กœ ํ•˜๊ฒ ๋‹ค.)

5. ์ •๋ณด ๋ณด๊ธฐ
ํ˜„์žฌ ๊ตฌ์„ฑ๋œ ์‚ฌํ•ญ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•œ๋ˆˆ์— ๋ชจ๋“  ๊ตฌ์„ฑ์„ ๋ณผ ์ˆ˜ ์žˆ์–ด ์ข‹๋‹ค.
๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ api endpoint๊ฐ€ ํ™•์ธ ๋œ๋‹ค.
$serverless info

[3rx: api test]
๋งŒ๋“ค์–ด์ง„ rest api ๊ฐ€ ์ œ๋Œ€๋กœ ๋™์ž‘ ํ•˜๋Š”์ง€ ํ™•์ธ ํ•˜์ž.
ํ…Œ์ŠคํŠธ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๋ณธ์ธ์ด ์„ ํ˜ธํ•˜๋Š” ํˆด์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ์•„๋ž˜ 3๊ฐ€์ง€๋ฅผ ๋งŽ์ด ์“ด๋‹ค.
1) curl – cli ํˆด
2) httpie – cli ํˆด
3) postman – labtop์šฉ gui tool

์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๋Š” curl ์„ ์‚ฌ์šฉํ•ด์™”์œผ๋‹ˆ curl๋กœ ํ…Œ์ŠคํŠธ ํ•ด๋ณด๊ฒ ๋‹ค.
(ํŒŒ๋ž€ ๋ถ€๋ถ„์€ ์ž์‹ ์˜ api endpoint๋ฅผ ์‚ฌ์šฉ)

1) doc ์ƒ์„ฑ
$curl -X POST -H ‘Content-Type: application/json’ -d ‘{“docid”:”test10″}’ https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/7bbe0aa3d0ab81a7ad43ddc9c03db9e0533d077842fb3645c22ec43323dxxxxx/serverless-rest-lab/api/serverless-cat

2) ์ƒ์„ฑ๋œ doc ํ™•์ธ
$curl -X GET https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/7bbe0aa3d0ab81a7ad43ddc9c03db9e0533d077842fb3645c22ec43323dxxxxx/serverless-rest-lab/api/serverless-cat?docid=test10

3) doc ์ˆ˜์ •
$curl -X PUT -H ‘Content-Type: application/json’ -d ‘{“docid”:”test10″,”name”:”damon1010″}’ https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/7bbe0aa3d0ab81a7ad43ddc9c03db9e0533d077842fb3645c22ec43323dxxxxx/serverless-rest-lab/api/serverless-cat

4) doc ์‚ญ์ œ
$curl -X DELETE -H ‘Content-Type: application/json’ -d ‘{“docid”:”test10″}’ https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/7bbe0aa3d0ab81a7ad43ddc9c03db9e0533d077842fb3645c22ec43323dxxxxx/serverless-rest-lab/api/serverless-cat

์ด์ƒ์œผ๋กœ serverless framework๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์•˜๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ข€ ๋” ํŽธํ•˜๊ฒŒ functions code๋ฅผ deploy ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
serverless framework์— ๋Œ€ํ•œ ์ •๋ณด๋Š” ์•„๋ž˜ ์ฐธ๊ณ  url์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
๋” ๋‹ค์–‘ํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ˆ url์„ ๋ฐฉ๋ฌธํ•ด ๋ณด์ž.

์ฐธ๊ณ :

https://console.bluemix.net/docs/openwhisk/openwhisk_goserverless.html#openwhisk_goserverless
https://github.com/serverless/examples

ํ† ๋ก  ์ฐธ๊ฐ€

์ด๋ฉ”์ผ์€ ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ์ž…๋ ฅ์ฐฝ์€ * ๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.