This is a short tutorial explaining how to deploy a cloud function using the google cloud CLI.
Set up Google Cloud CLI on your laptop
A prerequisite for this is that you have installed gcloud on your local machine. Check out this blog post that walks you through that process!
Deploying a Cloud Function using the Google Cloud CLI
- First, let’s create a folder for this. Create a folder called “gcloud”. I’m using vscode for this walk-through but you can use your preferred text editor.
- Next, add a python script called “main.py”. A cloud function needs to have main.py in order for it to work. It’s the main file that the cloud function executes. A function parameter “request” is needed for HTTP triggered cloud functions.
Here’s the snippets you can copy and paste:
def gcloud_test_function(request): return 'This is my test function!'
- And you’ll type the following in the command line / terminal to deploy your cloud function. Please note that you configure each parameter differently depending on your needs. Go to this documentation for more information on that!
gcloud functions deploy my-test-function \ --gen2 \ --region=us-central1 \ --runtime=python39 \ --entry-point=gcloud_test_function \ --trigger-http
- You might asked to enable the API if you’ve never created a cloud function before (you might’ve enabled already in GCP). After enabling that and run the above command, your function will be deployed! This is what I see in the command line / terminal:
- Lastly, let’s make sure your cloud function has been successfully deployed. Go to cloud functions in your google cloud console. You should see your cloud function deployed.
That’s it! You’ve successfully deployed a cloud function from your local machine.