How to create 2nd gen Cloud Functions in GCP

This post explains how to create 2nd gen HTTPS triggered Cloud Function in GCP.

GCP 2nd gen Cloud Functions

In August, 2022 Google Cloud announced the general availability of the 2nd generation of Cloud Functions. With this announcement now Cloud Functions offers two product versions Cloud Functions (1st gen), the original version, and Cloud Functions (2nd gen), a new version built on Cloud Run and Eventarc to provide an enhanced feature set. With the general availability of the 2nd generation of Cloud Functions you can start using 2nd gen Cloud Functions for new workloads, while continuing to use your existing 1st gen Cloud Functions.


See Also: 1st vs 2nd gen Cloud Functions

See Also: How to create 1st gen Cloud functions

Steps to create 2nd gen HTTPS Cloud Function


1. Navigate to Google Cloud Console https://console.cloud.google.com/.


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. In the Runtime select Python 3.9, write below code in hello_http entry point and click on DEPLOY.

   
  import functions_framework

  @functions_framework.http
  def hello_http(request):
      request_json = request.get_json(silent=True)
      request_args = request.args
  
      if request_json and 'name' in request_json:
          name = request_json['name']
      elif request_args and 'name' in request_args:
          name = request_args['name']
      else:
          name = '2nd gen Cloud Function Test'
      return name
    
   

2nd-gen-cloud-functions


10. Once the function deployment is complete, you should see function URL on the console.

2nd-gen-cloud-functions


11. Click on the function URL and you should see below response in the browser.

2nd-gen-cloud-functions


Category: GCP