This quickstart discusses how to build and deploy a function using Cloud Run, a serverless platform on Google Cloud.
The instructions in this quickstart can be run from a terminal on your own machine or from Cloud Shell, an online development and operations environment, using the Cloud Shell Editor.
Using the Cloud Shell Editor is beyond the scope of this quickstart, but this codelab can help you get started.
The only prerequisites are to have a Google Cloud project ID and the Cloud
SDK (gcloud
) installed and configured (authenticated) on your machine.
Go to the project selector page. Select an existing or create a new project. Note your project ID for later.
Creating a new project is highly recommended because you can destroy all resources when you're finished just by deleting the project.
Make sure that billing is enabled for your project (see how).
Install and configure gcloud.
If you need help installing or configuring gcloud
on your machine, see
Quickstart: Getting started with Cloud SDK. Don't forget to run
the gcloud auth login
command to authenticate your terminal.
Note:
gcloud
is preinstalled in Cloud Shell.
Update your gcloud
configuration to use your project ID.
$ gcloud config set core/project <PROJECT-ID>
Updated property [core/project].
- Run
dartfn
to create a new project using thehelloworld
generator (see Installing and using dartfn) - Clone this repo or download a zip archive and extract the contents
- Change directory to
examples/hello
.
- Change directory to
Use the gcloud run deploy
command to build and deploy your function in a
single step.
Note: If your Cloud Run service will connect to Cloud SQL via the
Cloud SQL Auth Proxy, then it is important to select the gen2
Cloud Run
execution environment.
gcloud run deploy NAME \
--source=PATH \ # can use $PWD or . for current dir
--project=PROJECT \ # the Google Cloud project ID
--region=REGION \ # ex: us-central1
--platform=managed \ # for Cloud Run
--allow-unauthenticated # for public access
--execution-environment=gen2 # helpful if using CloudSQL
Since the project ID was saved to the gcloud
configuration in the
Set your gcloud project section, you can also do the
same for platform and region and omit the arguments from the command line.
For Cloud Run, the platform must be set to
managed
. For a list of regions, rungcloud compute regions list
.
gcloud config set run/platform managed
gcloud config set run/region us-central1
For example:
$ gcloud run deploy hello --allow-unauthenticated --source=.
Building using Dockerfile and deploying container to Cloud Run service [hello] in project [dart-demo] region [us-central1]
✓ Building and deploying new service... Done.
✓ Uploading sources...
✓ Building Container... Logs are available at [https://console.cloud.google.com/cloud-build/builds/df7f07d1-d88b-4443-a2b1-bdfd3cdab15b?project=700116488077].
✓ Creating Revision... Revision deployment finished. Waiting for health check to begin.
✓ Routing traffic...
✓ Setting IAM Policy...
Done.
Service [hello] revision [hello-00001-yen] has been deployed and is serving 100 percent of traffic.
Service URL: https://hello-gpua4upw6q-uc.a.run.app
Congratulations! You have successfully built and deployed your function to Cloud Run. You can now access your function at the Service URL that is printed in the last line of output.
Cloud Run does not charge when the service is not in use. However, you might incur charges while the image is still stored in the Container Registry.
You can either just delete the image or delete the entire project (which will free up all resources and stop any other charges from incurring) from the Manage resources page.