Quickstart: Deploy a Kotlin service to Cloud Run
Learn how to create a simple Hello World application, package it into a container image, upload the container image to Artifact Registry, and then deploy the container image to Cloud Run.
Before you begin
- 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.
-
Make sure that billing is enabled for your Google Cloud project.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- To set the default project for your Cloud Run service:
Replace PROJECT_ID with your Google Cloud project ID.gcloud config set project PROJECT_ID
If you are under a domain restriction organization policy restricting unauthenticated invocations for your project, you will need to access your deployed service as described under Testing private services.
Enable the Cloud Run Admin API and the Cloud Build API:
gcloud services enable run.googleapis.com \ cloudbuild.googleapis.com
After the Cloud Run Admin API is enabled, the Compute Engine default service account is automatically created.
- For Cloud Build to be able to build your sources, grant the
Cloud Build Service Account
role to the Compute Engine default service account by running the following:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/cloudbuild.builds.builder
Replace
PROJECT_NUMBER
with your Google Cloud project number, andPROJECT_ID
with your Google Cloud project ID. For detailed instructions on how to find your project ID, and project number, see Creating and managing projects.Granting the Cloud Build Service Account role to the Compute Engine default service account takes a couple of minutes to propagate.
Write the sample application
To write an application in Kotlin:
Create a Spring Boot application.
From the console, create a new empty web project using the cURL and unzip commands:
curl https://start.spring.io/starter.zip \ -d language=kotlin \ -d type=gradle-project-kotlin \ -d bootVersion=3.2.5 \ -d dependencies=webflux \ -d javaVersion=17 \ -d name=Demo \ -d artifactId=helloworld \ -d baseDir=helloworld \ -d packageName=demo \ -o helloworld.zip unzip helloworld.zip cd helloworld
This creates a Spring Boot project.
To use this cURL command on Microsoft Windows, you'll need one of following command lines, or optionally use the Spring Initializr (configuration preloaded) to generate the project:
Update the
DemoApplication
class insrc/main/kotlin/DemoApplication.kt
by adding a@RestController
to handle the/
mapping:Set the server port to be defined by the PORT environment variable in the
application.properties
:This code creates a basic web server that listens on the port defined by the
PORT
environment variable.Create a
project.toml
file in the root directory to specify a Java version:[[build.env]] name = "GOOGLE_RUNTIME_VERSION" value = "17"
Your app is finished and ready to be deployed.
Deploy to Cloud Run from source
Important: This quickstart assumes that you have owner or editor roles in the project you are using for the quickstart. Otherwise, refer to the Cloud Run Source Developer role for the required permissions for deploying a Cloud Run resource from source.
Deploy from source automatically builds a container image from source code and deploys it.
To deploy from source:
In your source code directory, deploy the current folder using the following command:
gcloud run deploy --source .
When you are prompted for the service name, press Enter to accept the default name, for example
helloworld
.If you are prompted to enable additional APIs on the project, for example, the Artifact Registry API, respond by pressing
y
.When you are prompted for region: select the region of your choice, for example
us-central1
.If you are prompted to create a repository in the specified region, respond by pressing
y
.If you are prompted to allow unauthenticated invocations: respond
y
. You might not see this prompt if there is a domain restriction organization policy that prevents it; for more details see the Before you begin section.
Then wait a few moments until the deployment is complete. On success, the command line displays the service URL.
Visit your deployed service by opening the service URL in a web browser.
Cloud Run locations
Cloud Run is regional, which means the infrastructure that
runs your Cloud Run services is located in a specific region and is
managed by Google to be redundantly available across
all the zones within that region.
Meeting your latency, availability, or durability requirements are primary
factors for selecting the region where your Cloud Run services are run.
You can generally select the region nearest to your users but you should consider
the location of the other Google Cloud
products that are used by your Cloud Run service.
Using Google Cloud products together across multiple locations can affect
your service's latency as well as cost.
Cloud Run is available in the following regions:
Subject to Tier 1 pricing
asia-east1
(Taiwan)asia-northeast1
(Tokyo)asia-northeast2
(Osaka)europe-north1
(Finland) Low CO2europe-southwest1
(Madrid) Low CO2europe-west1
(Belgium) Low CO2europe-west4
(Netherlands) Low CO2europe-west8
(Milan)europe-west9
(Paris) Low CO2me-west1
(Tel Aviv)us-central1
(Iowa) Low CO2us-east1
(South Carolina)us-east4
(Northern Virginia)us-east5
(Columbus)us-south1
(Dallas) Low CO2us-west1
(Oregon) Low CO2
Subject to Tier 2 pricing
africa-south1
(Johannesburg)asia-east2
(Hong Kong)asia-northeast3
(Seoul, South Korea)asia-southeast1
(Singapore)asia-southeast2
(Jakarta)asia-south1
(Mumbai, India)asia-south2
(Delhi, India)australia-southeast1
(Sydney)australia-southeast2
(Melbourne)europe-central2
(Warsaw, Poland)europe-west10
(Berlin) Low CO2europe-west12
(Turin)europe-west2
(London, UK) Low CO2europe-west3
(Frankfurt, Germany) Low CO2europe-west6
(Zurich, Switzerland) Low CO2me-central1
(Doha)me-central2
(Dammam)northamerica-northeast1
(Montreal) Low CO2northamerica-northeast2
(Toronto) Low CO2southamerica-east1
(Sao Paulo, Brazil) Low CO2southamerica-west1
(Santiago, Chile) Low CO2us-west2
(Los Angeles)us-west3
(Salt Lake City)us-west4
(Las Vegas)
If you already created a Cloud Run service, you can view the region in the Cloud Run dashboard in the Google Cloud console.
Congratulations! You have just deployed a container image from source code to Cloud Run. Cloud Run automatically and horizontally scales out your container image to handle the received requests, then scales in when demand decreases. You only pay for the CPU, memory, and networking consumed during request handling.
Clean up
Remove your test project
While Cloud Run does not charge when the service is not in use, you might still be charged for storing the container image in Artifact Registry. You can delete your container image or delete your Google Cloud project to avoid incurring charges. Deleting your Google Cloud project stops billing for all the resources used within that project.
- 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.
What's next
For more information on building a container from code source and pushing to a repository, see: