12/04 Google Cloud Functions with Cloud SDK

6136 단어 GCPServerlessgcftech
이 글은 Serverless Hello World Advent Calendar 2020의 네 번째 날이다.

Cloud SDK 설치


% echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
% sudo apt-get install apt-transport-https ca-certificates gnupg
% curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
% sudo apt-get update && sudo apt-get install google-cloud-sdk
% gcloud init

함수 만들기


클라우드 펀션은 코드 생성기 등을 따로 준비하지 않아 솔직하게 쓴다.
% mkdir helloworld  
% cd helloworld
% npm init
% npm install escape-html
샘플은 원형을 유지한다공식 문서.
const escapeHtml = require('escape-html');

/**
 * HTTP Cloud Function.
 *
 * @param {Object} req Cloud Function request context.
 *                     More info: https://expressjs.com/en/api.html#req
 * @param {Object} res Cloud Function response context.
 *                     More info: https://expressjs.com/en/api.html#res
 */
exports.helloHttp = (req, res) => {
  res.send(`Hello ${escapeHtml(req.query.name || req.body.name || 'World')}!`);
};

프로그램 설계

gcloud functions deploy 명령을 사용하여 처리합니다.
첫 번째 매개 변수는 위에서 정의한 함수의 이름에 대응합니다.
% gcloud functions deploy helloHttp --runtime nodejs12 --trigger-http --allow-unauthenticated
Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
Deploying function (may take a while - up to 2 minutes)...⠛                                        
For Cloud Build Stackdriver Logs, visit: https://console.cloud.google.com/logs/viewer?XXXX
Deploying function (may take a while - up to 2 minutes)...done.                                    
availableMemoryMb: 256
buildId: 8fe487da-0dc2-41f1-abc4-ee6aa33bfbc9
entryPoint: helloHttp
httpsTrigger:
  url: https://us-central1-helloworld-297620.cloudfunctions.net/helloHttp
ingressSettings: ALLOW_ALL
labels:
  deployment-tool: cli-gcloud
name: projects/helloworld-297620/locations/us-central1/functions/helloHttp
runtime: nodejs12
serviceAccountEmail: [email protected]
sourceUploadUrl: XXXX
status: ACTIVE
timeout: 60s
updateTime: '2020-12-04T20:33:41.354Z'
versionId: '2'
Stackdriver Logs의 URL이 출력되기 때문에 depro 처리의 상세한 내용을 볼 수 있습니다.부모 컨테이너를 제거하고 그 위에 라이브러리 등을 확장하여 컨테이너로 GCR(Container Registry)에 로그인합니다.

동작 확인


그럼 바로 두드리러 갈게요.
% curl -v 'https://us-central1-helloworld-297620.cloudfunctions.net/helloHttp?name=world'
*   Trying 216.239.36.54:443...
* TCP_NODELAY set
* Connected to us-central1-helloworld-297620.cloudfunctions.net (216.239.36.54) port 443 (#0)

...(省略)...

* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x564bb282adb0)
> GET /helloHttp?name=world HTTP/2
> Host: us-central1-helloworld-297620.cloudfunctions.net
> user-agent: curl/7.68.0
> accept: */*
> 
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
< HTTP/2 200 
< content-type: text/html; charset=utf-8
< etag: W/"c-00hq6RNueFa8QiEjhep5cJRHWAI"
< function-execution-id: b7te2qdzmu5o
< x-powered-by: Express
< x-cloud-trace-context: cef5bfab5659c38ee825fb503329a21e;o=1
< date: Fri, 04 Dec 2020 20:39:31 GMT
< server: Google Frontend
< content-length: 12
< alt-svc: h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
< 
* Connection #0 to host us-central1-helloworld-297620.cloudfunctions.net left intact
Hello world!%
안전운전 확인.

좋은 웹페이지 즐겨찾기