Google Cloud Functions with HTTP Trigger Using C# and .NET

Reading Time: 5 minutes

Google Cloud Functions is a serverless platform that allows you to execute code in response to events. With the flexibility of HTTP triggers, developers can build lightweight, scalable APIs and microservices effortlessly. Leveraging the capabilities of C# and .NET, this article explores how to harness the potential of Google Cloud Functions to create efficient, serverless HTTP endpoints. Step by step tutorial to create, build and run Google Cloud Functions.

Cloud Functions (2nd gen) Overview

Cloud Functions (2nd Generation) is the next generation of Google Cloud Functions powered by Cloud Run and Eventarc. If you are new to Google Cloud Run refer article Google Cloud Run: Containers To Production In Seconds – Google Cloud Tutorials .

The new generation cloud functions give advance controls for performance and scalability and more control around function runtime and triggers from 125+ Eventarc sources.

Increased compute with granular controls

  • 6x longer request processing: 2nd Generation cloud functions supports runtime greater than 5 default 5 minutes runtime. This allows you to run longer request workloads such as processing large stream of data from cloud storage or Big query. For HTTP functions it supports up to 60 minutes of runtime. For Event driven functions this is up to 10 minutes.
  • 4x Larger instances: Utilize up to 16GB of RAM and 4 virtual CPUs with 2nd generation Cloud Functions to support larger in-memory operations, more compute-intensive tasks, and increased parallel processing. 32 GB of RAM and 8 VCPU configuration is currently in preview (March 2024).
  • Concurrency: Minimize cold starts and improve latency with support for 1000 concurrent request with single instance of cloud function.
  • Extensibility and portability: Since 2nd Generation functions are built on top of Cloud Run scalable infrastructure, it lets you move your function to cloud run or even kubernetes if change is required.
  • More regions: 2nd Generation functions supports all regions of 1st generation and additional regions including Finland (europe-north1) and Netherlands (europe-west4).
  • Traffic splitting: Supports multiple versions of your function to run in parallel, split traffic between different versions.
  • Pre-warmed instances: To minimize cold starts provide pre warmed instances. It ensures low application bootstrap time.

 Event coverage and CloudEvents support

  • Event sources: 2nd Generation functions can be triggered from 125 + event sources through Eventarc and events from custom sources (By publishing to Pub/Sub directly).
  • CloudEvent format:  All event-driven functions adhere to industry standard CloudEvents ( cloudevents.io), regardless of the source, to ensure a consistent developer experience.

In this article will walk you through creating a cloud function that responds to HTTP call.

Hands on Lab Environment Setup

Sign into Google Cloud console https://console.cloud.google.com/ and create a new GCP project. Google cloud free tier account provides free credits worth 400 $ to learn and explore google cloud services.

Learn more about Free tier account set up Unlock $400 Google Cloud Free Credits: Your Ultimate Guide To Free GCP Access – Google Cloud Tutorials .

In google cloud console click cloud shell icon at the top right tool bar.

Execute the below command to set ProjectId.

gcloud config set project [YOUR-PROJECT-ID]

Google cloud shell needs permissions to use your credentials for gcloud CLI command.

Click Authorize to grant permissions.

Google Cloud Project and Region Updated

To enable all the required services execute below command.

gcloud services enable \
  artifactregistry.googleapis.com \
  cloudfunctions.googleapis.com \
  cloudbuild.googleapis.com \
  eventarc.googleapis.com \
  run.googleapis.com \
  logging.googleapis.com \
  pubsub.googleapis.com

API'S enabled

Creating a Google Cloud Function with an HTTP Trigger in C#

Functions Framework for .NET is an open source FaaS (Function as a Service) framework for writing portable .NET function.

The Functions Framework lets you write lightweight functions that run in many different environments, including:

  • Google Cloud Functions
  •  local development machine
  • Cloud Run and Cloud Run on GKE
  • Knative-based environments

We will use function framework for .NET and its template to create and deploy cloud functions in C#.

In google Cloud functions search “Cloud Functions ” and selection 2nd generation cloud functions.

Set the function name and choose a region for deploying the function.

Select trigger type as “HTTPS” and allow unauthenticated invocations.

Cloud Function Gen 2 set up

In next select rutime .NET 6 and use inline editor to write code in C#.

Configure Memory, CPU and auto scaling options.

Cloud Functions auto scaling

In this demo we will use default generated code. Review code and click on deploy.

Once your function is deployed, you can test it by sending an HTTP request to the function’s endpoint.

Cloud Function Inline Editor

Managing Google Cloud Functions

  • Updating Functions: To update your function, make changes to your code and redeploy using the same gcloud functions deploy. Google Cloud will handle the update process, ensuring your function is always running the latest version of your code.
  • Monitoring and Logging : Google Cloud provides integrated monitoring and logging through Operations Suite. You can view logs directly in the Google Cloud Console or use the gcloud command-line tool to retrieve log entries.
  • Securing Cloud Functions: Securing your function is crucial to prevent unauthorized access. You can use Identity and Access Management (IAM) roles to control access to your functions. Additionally, consider implementing authentication and authorization within your function code to verify incoming requests

Google Cloud Functions Pricing

Google Cloud Functions pricing is structured to offer scalability and flexibility, allowing you to pay only for the resources you use. The pricing model is divided into several components, including invocations, compute time, and networking.

Invocations:

Charges: You are billed for the number of times your function is invoked.

Free Tier: Google offers a free tier of 2 million invocations per month.

Compute Time:

Charges: Compute time is calculated from the time your function is triggered until it terminates, rounded up to the nearest 100 milliseconds. The cost depends on the amount of memory and CPU allocated to your function.

Resource Allocation:You can allocate memory in increments from 128MB to 16GB. The allocated memory also determines the CPU allocation and the corresponding pricing.

Free Tier: Google provides a free tier that includes 400,000 GB-seconds per month, which combines both CPU and memory usage.

Networking:

Egress: There are charges for networking resources, particularly for data sent out of Google Cloud’s network. Ingress is typically free, but egress is subject to Google Cloud’s standard networking rates.

FAQ

How do I secure my 2nd Gen Cloud Functions?

Securing 2nd Gen Cloud Functions involves implementing best practices such as using IAM roles for fine-grained access control, securing function triggers, validating input data, and using environment variables for sensitive information. Additionally, consider using VPC Service Controls for enhanced network security and data exfiltration prevention.

Can I access VPC resources from my 2nd Gen Cloud Function?

Yes, one of the key features of 2nd Gen Cloud Functions is the ability to connect to VPC networks, allowing your functions to access resources within your private network securely. This is particularly useful for functions that need to interact with databases, APIs, or other services hosted within your VPC.

Are 2nd Gen Cloud Functions more expensive than 1st Gen?

The pricing model for 2nd Gen Cloud Functions considers execution time, memory, and CPU usage, offering a more granular billing approach. Depending on your function’s resource requirements and usage patterns, costs may vary compared to 1st Gen. It’s essential to evaluate and monitor your functions to understand the pricing implications.

How do I specify memory and CPU resources for 2nd Gen Cloud Functions?

When deploying a 2nd Gen Cloud Function, you can specify the memory and CPU allocation. The platform provides various combinations of memory and CPU options, allowing you to tailor resources to your function’s needs for optimal performance and cost-efficiency.

Scroll to Top