Ruby(1세대)를 사용하여 HTTP Cloud Run 함수 만들기 및 배포
이 가이드에서는 Ruby 런타임을 사용하여 Cloud Run 함수를 작성하는 과정을 안내합니다. Cloud Run Functions에는 다음과 같은 두 가지 유형이 있습니다.
- 표준 HTTP 요청에서 호출하는 HTTP 함수
- Pub/Sub 주제의 메시지 또는 Cloud Storage 버킷의 변경사항과 같이 클라우드 인프라의 이벤트를 처리하는 데 사용되는 이벤트 기반 함수
이 샘플은 간단한 HTTP 함수를 만드는 방법을 보여줍니다.
시작하기 전에
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Build APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Build APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. - gcloud CLI를 설치하고 초기화합니다.
gcloud구성요소를 업데이트 및 설치합니다.gcloud components update
- 개발 환경을 준비합니다.
로컬 시스템에 함수 코드를 저장할 디렉토리를 만듭니다.
Linux 또는 Mac OS X
mkdir ~/helloworld cd ~/helloworldWindows
mkdir %HOMEPATH%\helloworld cd %HOMEPATH%\helloworld다음 콘텐츠로
helloworld디렉터리에app.rb파일을 만듭니다.이 함수 예시는 HTTP 요청에 제공된 이름을 사용하여 인사말을 반환합니다. 제공된 이름이 없으면 'Hello World!'를 반환합니다.
함수 배포가 완료되면
httpsTrigger.url속성을 기록하거나 다음 명령어를 사용하여 찾을 수 있습니다.gcloud functions describe hello_http예를 들면 다음과 같습니다.
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http
브라우저에서 해당 URL을 방문하면 'Hello World!' 메시지가 표시됩니다.
예를 들어 다음 URL을 사용하여 HTTP 요청에 이름을 전달해 보세요.
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http?name=NAME
그러면 'Hello
NAME!' 메시지가 표시됩니다.
함수 만들기
종속 항목 지정
Ruby의 종속 항목은 bundler로 관리되며 Gemfile이라는 파일로 표현됩니다.
함수를 배포하면 Cloud Run Functions가 bundler를 사용하여 Gemfile 및 Gemfile.lock에 선언된 종속 항목을 다운로드하고 설치합니다.
Gemfile은 선택적 버전 제약조건과 함께 함수에 필요한 패키지를 나열합니다. Cloud Run 함수의 경우 이러한 패키지 중 하나가 functions_framework gem이어야 합니다.
이 실습에서는 함수 코드가 포함된 app.rb 파일과 동일한 디렉터리에 다음 콘텐츠로 Gemfile이라는 파일을 만듭니다.
source "https://rubygems.org" gem "functions_framework", "~> 0.7"
다음 명령어를 실행하여 functions_framework gem 및 기타 종속 항목을 설치합니다.
bundle install
로컬에서 빌드 및 테스트
함수를 배포하기 전에 이를 빌드하고 로컬로 테스트할 수 있습니다.
다음 명령어를 실행하여 functions-framework-ruby 실행 파일을 사용해 hello_http 함수를 실행하는 로컬 웹 서버를 시작합니다.
bundle exec functions-framework-ruby --target hello_http
# ...starts the web server in the foreground
이 기능이 성공적으로 빌드되면 함수 작동을 확인하는 웹브라우저에 방문할 수 있는 URL(http://localhost:8080/)이 표시됩니다. Hello World! 메시지가 표시됩니다.
또는 다른 터미널 창에서 curl을 사용하여 이 함수에 요청을 보낼 수 있습니다.
curl localhost:8080
# Output: Hello World!
Ruby 함수 프레임워크 문서의 함수 테스트를 참조하세요.
함수 배포하기
HTTP 트리거를 사용하여 함수를 배포하려면 helloworld 디렉터리에서 다음 명령어를 실행합니다.
gcloud functions deploy hello_http --no-gen2 --runtime ruby34 --trigger-http --allow-unauthenticated
--allow-unauthenticated 플래그를 사용하면 인증 없이 함수에 도달할 수 있습니다.
인증을 요청하려면 플래그를 생략합니다.
배포된 함수 테스트
로그 보기
Cloud Logging UI에서 또는 Google Cloud CLI를 통해 Cloud Run Functions 로그를 볼 수 있습니다.
명령줄 도구를 사용하여 로그 보기
gcloud CLI를 사용하여 함수 로그를 보려면
logs read 명령어 뒤에 함수 이름을 사용합니다.
gcloud functions logs read hello_http
다음과 유사한 결과가 출력됩니다.
LEVEL NAME EXECUTION_ID TIME_UTC LOG D helloHttp rvb9j0axfclb 2019-09-18 22:06:25.983 Function execution started D helloHttp rvb9j0axfclb 2019-09-18 22:06:26.001 Function execution took 19 ms, finished with status code: 200
Logging 대시보드에서 로그 보기
Google Cloud 콘솔에서도 Cloud Run Functions의 로그를 볼 수 있습니다.