0
0
GCPcloud~5 mins

HTTP triggered functions in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your code to run only when someone visits a web address or sends a web request. HTTP triggered functions let you do this easily by running your code when an HTTP request happens.
When you want to create a simple web API that runs your code on demand.
When you need to respond to webhooks from other services like payment or messaging platforms.
When you want to build a lightweight backend for a web or mobile app without managing servers.
When you want to quickly test or run code by visiting a URL in your browser.
When you want to handle form submissions or user input sent over the web.
Config File - main.py
main.py
def hello_world(request):
    """HTTP Cloud Function.
    Args:
        request (flask.Request): The request object.
    Returns:
        A response string.
    """
    return 'Hello, World!'

This Python file defines a simple HTTP triggered function named hello_world. It takes a web request and returns a plain text response saying 'Hello, World!'. This function runs whenever an HTTP request is sent to its URL.

Commands
This command deploys the HTTP triggered function named 'hello_world' using Python 3.9 runtime. It sets the function to run when it receives an HTTP request and allows anyone to call it without signing in.
Terminal
gcloud functions deploy hello_world --runtime python39 --trigger-http --allow-unauthenticated
Expected OutputExpected
Deploying function (may take a while)... Deploying function (hello_world)...done. availableMemoryMb: 256 entryPoint: hello_world httpsTrigger: url: https://REGION-PROJECT_ID.cloudfunctions.net/hello_world runtime: python39 serviceAccountEmail: PROJECT_ID@appspot.gserviceaccount.com status: ACTIVE timeout: 60s updateTime: '2024-06-01T12:00:00Z' versionId: '1'
--runtime - Specifies the language runtime version to use
--trigger-http - Sets the function to be triggered by HTTP requests
--allow-unauthenticated - Allows public access without requiring login
This command sends a simple HTTP GET request to the deployed function's URL to test if it responds correctly.
Terminal
curl https://REGION-PROJECT_ID.cloudfunctions.net/hello_world
Expected OutputExpected
Hello, World!
Key Concept

If you remember nothing else from this pattern, remember: HTTP triggered functions run your code whenever someone sends a web request to their URL.

Common Mistakes
Not using the --trigger-http flag when deploying the function
Without this flag, the function won't respond to HTTP requests and won't have a public URL.
Always include --trigger-http when deploying functions meant to respond to web requests.
Forgetting to add --allow-unauthenticated for public access
Without this flag, only authenticated users can call the function, blocking public access.
Add --allow-unauthenticated if you want anyone to access the function without signing in.
Not returning a response from the function
If the function does not return a response, the caller will get an error or no output.
Make sure your function returns a string or valid HTTP response.
Summary
Deploy the function with gcloud using --trigger-http and --allow-unauthenticated flags.
Test the function by sending an HTTP request to its URL using curl.
The function runs your code and returns a response whenever it receives an HTTP request.