빠른 시작: gcloud CLI를 사용하여 Cloud Run 함수 배포

이 페이지에서는 gcloud CLI를 사용하여 HTTP Cloud Run 함수를 배포하는 방법을 보여줍니다.

시작하기 전에

  1. 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.
  2. Install the Google Cloud CLI.

  3. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  4. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  5. 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 the resourcemanager.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.

  6. Verify that billing is enabled for your Google Cloud project.

  7. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com
  8. 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.
  9. Install the Google Cloud CLI.

  10. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  11. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  12. 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 the resourcemanager.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.

  13. Verify that billing is enabled for your Google Cloud project.

  14. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com run.googleapis.com logging.googleapis.com
  15. 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.
  16. Cloud Run 서비스의 기본 프로젝트를 설정하려면 다음 명령어를 실행합니다.
     gcloud config set project PROJECT_ID
    PROJECT_ID를 이 빠른 시작에서 만든 프로젝트 이름으로 바꿉니다.
  17. 프로젝트에서 인증되지 않은 호출을 제한하는 도메인 제한 조직 정책이 적용되는 경우 비공개 서비스 테스트의 설명대로 배포된 서비스에 액세스해야 합니다.

  18. 서비스 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
    • PRINCIPAL: 사용자 식별자. 일반적으로 Cloud Run 리소스를 배포하는 이메일 주소입니다.
  19. 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 빌더 역할을 부여하면 전파되는 데 몇 분 정도 걸립니다.

  20. Cloud Run 가격 책정을 검토하거나 가격 계산기로 비용을 추정하세요.
  21. 샘플 함수 작성

    애플리케이션을 작성하려면 다음 단계를 따르세요.

    Node.js

    1. helloworld이라는 새 디렉터리를 만든 후 디렉터리를 다음으로 변경합니다.

         mkdir helloworld
         cd helloworld
      

    2. helloworld 디렉터리에 package.json 파일을 만들어 Node.js 종속 항목을 지정합니다.

      {
        "name": "nodejs-docs-samples-functions-hello-world-get",
        "version": "0.0.1",
        "private": true,
        "license": "Apache-2.0",
        "author": "Google Inc.",
        "repository": {
          "type": "git",
          "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
        },
        "engines": {
          "node": ">=16.0.0"
        },
        "scripts": {
          "test": "c8 mocha -p -j 2 test/*.test.js --timeout=6000 --exit"
        },
        "dependencies": {
          "@google-cloud/functions-framework": "^3.1.0"
        },
        "devDependencies": {
          "c8": "^10.0.0",
          "gaxios": "^6.0.0",
          "mocha": "^10.0.0",
          "wait-port": "^1.0.4"
        }
      }
      
    3. 다음 Node.js 샘플을 사용하여 helloworld 디렉터리에 index.js 파일을 만듭니다.

      const functions = require('@google-cloud/functions-framework');
      
      // Register an HTTP function with the Functions Framework that will be executed
      // when you make an HTTP request to the deployed function's endpoint.
      functions.http('helloGET', (req, res)