How to create Cloud Functions in GCP from Cloud Storage Buckets

This post explains how to create HTTPS triggered 1st gen and 2nd gen GCP Cloud Functions from Cloud Storage Bucket.


See Also: 1st vs 2nd gen Cloud Functions


Prerequisite

To follow this tutorials you need a Cloud Storage Bucket, refer Steps to create bucket in GCP for creating a new Cloud storage bucket.

1st gen Cloud Function from Storage Bucket

1. Create a file with name main.py and write below code in it.

   
def my_function(req):
  return f"hello from gcptutorials.com"
 

2. Zip the main.py and upload the zipped file to Cloud Storage bucket.

cloud-function-from-bucket


3. In the search bar type Cloud Functions and select Cloud Functions from the results.


create gcp cloud functions


4. Click on CREATE FUNCTION.

create gcp cloud functions


5. Enabled the required APIs.

create gcp cloud functions


6. Select the Environment, provide Function Name, select Region. From the Trigger select HTTP and note down the function URL.


create gcp cloud functions


7. In Authentication select Allow unauthenticated invocations and click on SAVE

create gcp cloud functions


8. Keep the default in Runtime, build, connection and security settings and click on NEXT.

create gcp cloud functions



8. Select following options and click on DEPLOY.

  • Runtime: Python 3.9
  • Source code: Zip from Cloud Storage
  • Entry point: my_function
  • Cloud Storage location: Select the uploaded zip file

cloud-function-from-bucket


9. Once the deployment is complete, got to TRIGGER, click on Trigger URL and you should see function output on the screen.

cloud-function-from-bucket

2nd gen Cloud Function from Storage Bucket

1. Create a file with name main.py and write below code in it.

   
def my_function(req):
  return f"hello from gcptutorials.com"
 

2. Zip the main.py and upload the zipped file to Cloud Storage bucket.

cloud-function-from-bucket


2. In the search bar type Cloud Functions and select Cloud Functions from the results.

2nd-gen-cloud-functions


3. Click on CREATE FUNCTION.

2nd-gen-cloud-functions


4. Enabled the required APIs.

2nd-gen-cloud-functions


5. Select the 2nd gen from the Environment dropdown, provide Function Name, select Region.

2nd-gen-cloud-functions


6. In the HTTPS trigger select Allow unauthenticated invocations.

2nd-gen-cloud-functions


7. Keep the default settings in Runtime, build, connection and security settings and click on NEXT.

2nd-gen-cloud-functions


8. Enable the required Artifact Registry API.

2nd-gen-cloud-functions



9. Select following options and click on DEPLOY.

  • Runtime: Python 3.9
  • Source code: Zip from Cloud Storage
  • Entry point: my_function
  • Cloud Storage location: Select the uploaded zip file

cloud-function-from-bucket


10. Once the deployment is complete, got to TRIGGER, click on Trigger URL to run the function.


Category: GCP