App Engine スタンダード環境で Go 1.11 アプリを作成する
リージョン ID
REGION_ID は、アプリの作成時に選択したリージョンに基づいて Google が割り当てる省略形のコードです。一部のリージョン ID は、一般的に使用されている国や州のコードと類似しているように見える場合がありますが、このコードは国または州に対応するものではありません。2020 年 2 月以降に作成されたアプリの場合、REGION_ID.r は App Engine の URL に含まれています。この日付より前に作成されたアプリの場合、URL のリージョン ID は省略可能です。
詳しくは、リージョン ID をご覧ください。
このページでは、短いメッセージを表示する基本的な App Engine アプリケーションをデプロイする方法を説明します。App Engine での Go の概要については、アプリの作成をご覧ください。
App Engine が最適なコンピューティング オプションかどうか不明な場合は、 Google Cloud上でアプリケーションをホストするためのその他の方法をご覧ください。
費用
このガイドの実行に伴う費用はありません。このサンプルアプリを実行するだけなら、無料の割り当てを超過することはありません。
始める前に
- 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 Build API. 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.
- 
      Install the Google Cloud CLI. 
- 
          外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。 
- 
        gcloud CLI を初期化するには、次のコマンドを実行します。 gcloud init
- 
    
    
      
        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 Build API. 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.
- 
      Install the Google Cloud CLI. 
- 
          外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。 
- 
        gcloud CLI を初期化するには、次のコマンドを実行します。 gcloud init
- プロジェクトで App Engine アプリケーションを初期化し、そのリージョンを選択します。 - gcloud app create --project=[YOUR_PROJECT_ID]- 表示された指示に従って、App Engine アプリケーションを配置するリージョンを選択します。 
- 次の前提条件をインストールします。 
- 次のコマンドを実行して、Go 1.11 用の App Engine 拡張機能を含む gcloud コンポーネントをインストールします。 - gcloud components install app-engine-go
- gcloud app describeコマンドを実行します。
- Google Cloud コンソールの App Engine ダッシュボードを開きます。ページの上部にリージョンが表示されます。 
- Hello World サンプルアプリ リポジトリのクローンを作成します。 - git clone https://github.com/GoogleCloudPlatform/golang-samples- または、zip ファイルとしてサンプルをダウンロードし、ファイルを解凍します。 
- サンプルコードが入っているディレクトリに移動します。 - cd golang-samples/appengine/go11x/helloworld/
- golang-samples/appengine/go11x/helloworldディレクトリから次のコマンドを実行し、Hello World アプリを App Engine にデプロイします。- gcloud app deploy
- https://PROJECT_ID.REGION_ID.r.appspot.comで公開されているライブアプリを表示します。- gcloud app browse
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
追加の前提条件
App Engine のロケーション
App Engine はリージョナルです。つまり、アプリを実行するインフラストラクチャは特定のリージョンに配置され、そのリージョン内のすべてのゾーンで冗長的に利用できるように Google が管理しています。
レイテンシ、可用性、耐久性の要件を満たすことが、アプリを実行するリージョンを選択する際の主な要素になります。一般的には、アプリのユーザーに最も近いリージョンを選択できますが、App Engine が使用可能なロケーションと、アプリが使用するその他のGoogle Cloud プロダクトおよびサービスのロケーションを考慮する必要があります。使用するサービスが複数のロケーションにまたがっていると、アプリのレイテンシだけでなく、料金にも影響する可能性があります。
設定したアプリのリージョンは変更できません。
すでに App Engine アプリケーションを作成している場合は、次のいずれかの方法でそのリージョンを表示できます。
このクイックスタートでは、読者が Go プログラミング言語に精通しており、すでに Go がインストールされていることを前提としています。
Hello World アプリをダウンロードする
Go 用のシンプルな Hello World アプリが用意されており、Google Cloud Platform へのアプリのデプロイをすぐに試すことができます。次の手順に沿って Hello World をローカルマシンにダウンロードしてください。
サンプルアプリをダウンロードし、アプリのディレクトリに移動するには:
App Engine に Hello World をデプロイする
Hello, World! メッセージが、App Engine インスタンス上で実行されているウェブサーバーによって配信されます。
これで完了です。App Engine に Go ウェブサービスが正常にデプロイされました。
クリーンアップ
引き続き App Engine の詳細を確認する場合は、 Google Cloud プロジェクトを保存して、アプリの構築に進むことができます。
あるいは、 Google Cloud プロジェクトを削除して、プロジェクト内で使用しているすべてのリソースへの課金を停止することもできます。
次のステップ
App Engine にウェブサービスをデプロイすることができました。次にアプリの作成を参照して、App Engine での Go のさらに詳しい手順を理解しましょう。