빠른 시작: gcloud CLI를 사용하여 Cloud Run 함수 배포
이 페이지에서는 gcloud CLI를 사용하여 HTTP Cloud Run 함수를 배포하는 방법을 보여줍니다.
시작하기 전에
- 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.
-
Install the Google Cloud CLI.
-
외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.
-
gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.
gcloud init
-
Create or select 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.create
permission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles.gcloud services enable artifactregistry.googleapis.com
cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com -
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/run.sourceDeveloper, roles/run.admin, roles/logging.viewer
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID
: Your project ID.USER_IDENTIFIER
: The identifier for your user account. For example,myemail@example.com
.ROLE
: The IAM role that you grant to your user account.
-
Install the Google Cloud CLI.
-
외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.
-
gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.
gcloud init
-
Create or select 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.create
permission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, Cloud Build, Cloud Run Admin API, and Cloud Logging APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles.gcloud services enable artifactregistry.googleapis.com
cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com -
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/run.sourceDeveloper, roles/run.admin, roles/logging.viewer
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID
: Your project ID.USER_IDENTIFIER
: The identifier for your user account. For example,myemail@example.com
.ROLE
: The IAM role that you grant to your user account.
- Cloud Run 서비스의 기본 프로젝트를 설정하려면 다음 명령어를 실행합니다.
PROJECT_ID를 이 빠른 시작에서 만든 프로젝트 이름으로 바꿉니다.gcloud config set project PROJECT_ID
프로젝트에서 인증되지 않은 호출을 제한하는 도메인 제한 조직 정책이 적용되는 경우 비공개 서비스 테스트의 설명대로 배포된 서비스에 액세스해야 합니다.
-
서비스 ID에 서비스 계정 사용자 역할이 부여되어 있는지 확인합니다. 기본적으로 서비스 ID는 Compute Engine 기본 서비스 계정입니다.
역할 부여
서비스 ID 리소스에 대한 액세스 권한을 부여하려면
gcloud iam service-accounts add-iam-policy-binding
명령어를 사용하여 강조 표시된 변수를 적절한 값으로 바꿉니다.gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_EMAIL \ --member=user:PRINCIPAL \ --role=roles/iam.serviceAccountUser
다음을 바꿉니다.
- SERVICE_ACCOUNT_EMAIL: 서비스 ID로 사용 중인 서비스 계정 이메일 주소입니다(예:
- Compute Engine 기본 서비스 계정:
PROJECT_NUMBER-compute@developer.gserviceaccount.com
- 생성한 서비스 계정:
SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
- Compute Engine 기본 서비스 계정:
- PRINCIPAL: 사용자 식별자입니다. 일반적으로 Cloud Run 리소스를 배포하는 Google 계정 이메일 주소입니다.
- SERVICE_ACCOUNT_EMAIL: 서비스 ID로 사용 중인 서비스 계정 이메일 주소입니다(예:
- Cloud Build 서비스 계정에 다음 IAM 역할을 부여합니다.
Cloud Build 서비스 계정에 필요한 역할을 보려면 클릭
이 동작을 재정의하지 않는 한 Cloud Build는 자동으로 Compute Engine 기본 서비스 계정을 기본 Cloud Build 서비스 계정으로 사용하여 소스 코드와 Cloud Run 리소스를 빌드합니다. Cloud Build에서 소스를 빌드할 수 있게 하려면 관리자에게 프로젝트의 Compute Engine 기본 서비스 계정에 Cloud Run 빌더(
roles/run.builder
)를 부여해 달라고 요청하세요.gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/run.builder
PROJECT_NUMBER
를 Google Cloud프로젝트 번호로,PROJECT_ID
를 Google Cloud프로젝트 ID로 바꿉니다. 프로젝트 ID와 프로젝트 번호를 찾는 방법에 대한 자세한 내용은 프로젝트 만들기 및 관리를 참조하세요.Compute Engine 기본 서비스 계정에 Cloud Run 빌더 역할을 부여하면 전파되는 데 몇 분 정도 걸립니다.
- Cloud Run 가격 책정을 검토하거나 가격 계산기로 비용을 추정합니다.
helloworld
이라는 새 디렉터리를 만든 후 디렉터리를 다음으로 변경합니다.mkdir helloworld cd helloworld
helloworld
디렉터리에package.json
파일을 만들어 Node.js 종속 항목을 지정합니다.다음 Node.js 샘플을 사용하여
helloworld
디렉터리에index.js
파일을 만듭니다.helloworld
이라는 새 디렉터리를 만든 후 디렉터리를 다음으로 변경합니다.mkdir helloworld cd helloworld
helloworld
디렉터리에requirements.txt
파일을 만들어 Python 종속 항목을 지정합니다.그러면 샘플에 필요한 패키지가 추가됩니다.
다음 Python 샘플을 사용하여
helloworld
디렉터리에main.py
파일을 만듭니다.helloworld
이라는 새 디렉터리를 만든 후 디렉터리를 다음으로 변경합니다.mkdir helloworld cd helloworld
go.mod
파일을 만들어 Go 모듈을 선언합니다.다음 Go 코드 샘플을 사용하여
helloworld
디렉터리에hello_http.go
파일을 만듭니다.helloworld
이라는 새 디렉터리를 만든 후 디렉터리를 다음으로 변경합니다.mkdir helloworld cd helloworld
소스 디렉터리와 소스 파일을 포함하도록 다음과 같은 프로젝트 구조를 만듭니다.
mkdir -p ~/helloworld/src/main/java/functions touch ~/helloworld/src/main/java/functions/HelloWorld.java
다음 Java 코드 샘플로
HelloWorld.java
파일을 업데이트합니다.helloworld
디렉터리에pom.xml
파일을 만들고 다음 Java 종속 항목을 추가합니다.helloworld
이라는 새 디렉터리를 만든 후 디렉터리를 다음으로 변경합니다.mkdir helloworld cd helloworld
app.rb
라는 파일을 만들고 이 파일에 다음 코드를 붙여넣습니다.Gemfile
이라는 파일을 만들고 이 파일에 다음을 복사하여 붙여넣습니다.Bundler 2.0 이상이 설치되지 않은 경우 Bundler를 설치합니다.
다음을 실행하여
Gemfile.lock
파일을 생성합니다.bundle install
helloworld
이라는 새 디렉터리를 만든 후 디렉터리를 다음으로 변경합니다.mkdir helloworld cd helloworld
index.php
라는 파일을 만들고 이 파일에 다음 코드를 붙여넣습니다.Cloud Shell을 사용하지 않는 경우
composer.json
파일을 만들고 다음 코드를 이 파일에 붙여넣습니다..NET SDK를 설치합니다.
콘솔에서 dotnet 명령어를 사용하여 빈 웹 프로젝트를 새로 만듭니다.
dotnet new web -o helloworld-csharp
디렉터리를
helloworld-csharp
로 변경합니다.프로젝트 파일
helloworld-csharp.csproj
의 샘플 코드를 다음으로 바꿉니다.Program.cs
파일의 샘플 코드를 다음으로 바꿉니다.샘플 코드가 포함된 디렉터리에서 다음 명령어를 실행하여 함수를 배포합니다.
Node.js
gcloud run deploy nodejs-http-function \ --source . \ --function helloGET \ --base-image nodejs22 \ --region REGION \ --allow-unauthenticated
REGION을 함수를 배포하려는 서비스의 Google Cloud리전으로 바꿉니다. 예를 들면
europe-west1
입니다.Python
gcloud run deploy python-http-function \ --source . \ --function hello_get \ --base-image python313 \ --region REGION \ --allow-unauthenticated
REGION을 함수를 배포하려는 서비스의 Google Cloud리전으로 바꿉니다. 예를 들면
europe-west1
입니다.Go
gcloud run deploy go-http-function \ --source . \ --function HelloGet \ --base-image go125 \ --region REGION \ --allow-unauthenticated
REGION을 함수를 배포하려는 서비스의 Google Cloud리전으로 바꿉니다. 예를 들면
europe-west1
입니다.자바
pom.xml
파일이 포함된 디렉터리에서 다음 명령어를 실행합니다.gcloud run deploy java-http-function \ --source . \ --function functions.HelloWorld \ --base-image java21 \ --region REGION \ --allow-unauthenticated
REGION을 함수를 배포하려는 서비스의 Google Cloud리전으로 바꿉니다. 예를 들면
europe-west1
입니다.Ruby
gcloud run deploy ruby-http-function \ --source . \ --function hello_get \ --base-image ruby34 \ --region REGION \ --allow-unauthenticated
REGION을 함수를 배포하려는 서비스의 Google Cloud리전으로 바꿉니다. 예를 들면
europe-west1
입니다.PHP
gcloud run deploy php-http-function \ --source . \ --function helloGet \ --base-image php84 \ --region REGION \ --allow-unauthenticated
REGION을 함수를 배포하려는 서비스의 Google Cloud리전으로 바꿉니다. 예를 들면
europe-west1
입니다..NET
gcloud run deploy csharp-http-function \ --source . \ --function HelloWorld.Function \ --base-image dotnet8 \ --region REGION \ --allow-unauthenticated
REGION을 함수를 배포하려는 서비스의 Google Cloud리전으로 바꿉니다. 예를 들면
europe-west1
입니다.배포가 완료되면 Google Cloud CLI에 서비스가 실행 중인 URL이 표시됩니다. 브라우저에서 URL을 열어 함수의 출력을 확인합니다.
Google Cloud 콘솔에서 Cloud Run으로 이동합니다.
서비스 목록에서 삭제할 서비스를 찾은 다음 체크박스를 클릭하여 선택합니다.
삭제를 클릭합니다. 이렇게 하면 서비스의 모든 버전이 삭제됩니다.
- SERVICE: 서비스 이름입니다.
- REGION: Google Cloud 서비스의 리전입니다.
Google Cloud 콘솔을 사용하여 샘플 함수를 Cloud Run에 배포하려면 빠른 시작: Google Cloud 콘솔을 사용하여 Cloud Run에 함수 배포를 참조하세요.
Google Cloud 콘솔과 Google Cloud CLI를 사용하여 함수를 배포하고 트리거를 만들려면 함수 배포를 참조하세요.
기존 함수를 보고 삭제하려면 서비스 버전 관리를 참조하세요.
자체 도구 모음에서 함수 컨테이너를 빌드하고 Cloud Run에 배포하려면 함수 빌드를 참조하세요.
Eventarc로 트리거를 만들려면 Eventarc로 트리거 만들기를 참조하세요.
샘플 함수 작성
애플리케이션을 작성하려면 다음 단계를 따르세요.
Node.js
Python
Go
자바
Ruby
PHP
.NET
함수 배포하기
중요: 이 빠른 시작에서는 빠른 시작에 사용 중인 프로젝트에 소유자 역할이나 편집자 역할이 있다고 가정합니다. 그렇지 않은 경우 소스에서 Cloud Run 리소스를 배포하는 데 필요한 권한은 Cloud Run 소스 개발자 역할을 참조하세요.
Cloud Run 함수를 배포하려면 다음 단계를 따르세요.
삭제
Google Cloud 계정에 추가 비용이 청구되지 않도록 하려면 이 빠른 시작으로 배포한 모든 리소스를 삭제합니다.
저장소 삭제
배포된 서비스를 사용하지 않으면 Cloud Run에서 요금을 청구하지 않습니다. 하지만 Artifact Registry에 컨테이너 이미지를 저장하는 요금은 부과될 수 있습니다. Artifact Registry 저장소를 삭제하려면 Artifact Registry 문서의 저장소 삭제 단계를 따르세요.
서비스 삭제
Cloud Run 서비스는 요청을 수신할 때까지 비용이 발생하지 않습니다. Cloud Run 서비스를 삭제하려면 다음 단계 중 하나를 따르세요.
콘솔
서비스를 삭제하려면 다음 안내를 따르세요.
gcloud
서비스를 삭제하려면 다음 명령어를 실행합니다.
gcloud run services delete SERVICE --region REGION
다음을 바꿉니다.
테스트 프로젝트 삭제
Google Cloud 프로젝트를 삭제하면 해당 프로젝트의 모든 리소스에 대한 청구가 중단됩니다. 프로젝트의 모든 Google Cloud 리소스를 해제하려면 다음 단계를 따르세요.
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID