Create a Simple HTTP Triggered Cloud Function on GCP
📖 Scenario: You are building a small cloud function on Google Cloud Platform (GCP) that responds to HTTP requests. This function will greet users by name when they visit a URL with their name as a parameter.
🎯 Goal: Build a deployable HTTP triggered Cloud Function on GCP that reads a name from the URL query and returns a greeting message.
📋 What You'll Learn
Create a Python function named greet that accepts an HTTP request.
Extract the name parameter from the HTTP request query string.
Return a greeting message including the name parameter or a default greeting if no name is provided.
Add the necessary configuration to deploy the function as an HTTP trigger.
💡 Why This Matters
🌍 Real World
HTTP triggered cloud functions are used to build APIs, webhooks, and lightweight backend services that respond to web requests instantly.
💼 Career
Understanding how to create and deploy HTTP triggered functions is essential for cloud developers and engineers working with serverless architectures on GCP.
Progress0 / 4 steps
1
Create the basic HTTP function
Create a Python function called greet that accepts a single parameter request. Inside the function, return the string 'Hello World!'.
GCP
Hint
Define a function named greet with one parameter request. Return the string 'Hello World!'.
2
Extract the name parameter from the request
Inside the greet function, create a variable called name that gets the name parameter from request.args. Use request.args.get('name').
GCP
Hint
Use request.args.get('name') to get the name from the URL query parameters.
3
Return a personalized greeting
Modify the greet function to return 'Hello ' + name if name is not null. Otherwise, return 'Hello World!'. Use an if statement to check name.
GCP
Hint
Use an if statement to check if name is not null. Return a greeting accordingly.
4
Add the HTTP trigger configuration
Add a comment at the top of the file that says # Deploy this function with --trigger-http to indicate it is an HTTP triggered function.
GCP
Hint
Add a comment to remind how to deploy this function as an HTTP trigger.
Practice
(1/5)
1. What happens when an HTTP triggered function in GCP receives a web request?
easy
A. The function only logs the request but does not respond.
B. The function stores the request without running any code.
C. The function runs the code and sends back a response.
D. The function waits for a manual trigger to run.
Solution
Step 1: Understand the trigger type
HTTP triggered functions run automatically when they receive a web request.
Step 2: Identify the function behavior
They process the request and return a response to the caller.
Final Answer:
The function runs the code and sends back a response. -> Option C
Quick Check:
HTTP trigger = runs code + responds [OK]
Hint: HTTP trigger means code runs on web request [OK]
Common Mistakes:
Thinking the function only logs requests
Assuming manual trigger is needed
Believing the function stores requests without running
2. Which of the following is the correct way to define an HTTP triggered function in Python for GCP Cloud Functions?
easy
A. def my_function(data, context):
B. def my_function(event, context):
C. def my_function():
D. def my_function(request):
Solution
Step 1: Recall function signature for HTTP triggers
HTTP triggered functions accept a single 'request' parameter representing the web request.
Step 2: Match the correct signature
Only 'def my_function(request):' matches this pattern.
Final Answer:
def my_function(request): -> Option D
Quick Check:
HTTP function signature = def func(request) [OK]
Hint: HTTP functions take one 'request' parameter [OK]
Common Mistakes:
Using event and context parameters (for background triggers)
Defining function without parameters
Using wrong parameter names
3. Given this Python HTTP function code snippet, what will be the HTTP response body when a GET request is made?
The function checks if the request method is 'GET'.
Step 2: Determine response for GET
For 'GET', it returns the string 'Hello, World!'.
Final Answer:
"Hello, World!" -> Option A
Quick Check:
GET request returns greeting [OK]
Hint: GET request returns 'Hello, World!' string [OK]
Common Mistakes:
Confusing method names case sensitivity
Expecting a JSON response without formatting
Ignoring else branch
4. Identify the error in this HTTP triggered function code snippet:
def process_request(request):
if request.method == 'POST'
return 'Received POST'
else:
return 'Only POST allowed', 405
medium
A. Missing colon ':' after if statement
B. Incorrect return type for HTTP function
C. Wrong parameter name for request
D. No error in the code
Solution
Step 1: Check syntax of if statement
The if statement is missing a colon ':' at the end.
Step 2: Confirm syntax error impact
Without the colon, the code will cause a syntax error and fail to deploy.
Final Answer:
Missing colon ':' after if statement -> Option A
Quick Check:
if statement needs ':' [OK]
Hint: Check colons after if statements [OK]
Common Mistakes:
Ignoring syntax errors
Assuming return types are wrong
Confusing parameter names
5. You want to create an HTTP triggered function that accepts JSON data in a POST request and returns the value of the 'name' field. Which code snippet correctly handles this in Python?
hard
A. def get_name(request):
data = request.get_json()
return f"Hello, {data.name}!"
B. def get_name(request):
data = request.get_json()
return f"Hello, {data['name']}!"
C. def get_name(request):
data = request.get_json
return f"Hello, {data['name']}!"
D. def get_name(request):
data = request.json()
return f"Hello, {data.name}!"
Solution
Step 1: Use correct method to parse JSON
request.get_json() is the correct method to parse JSON body in GCP HTTP functions.
Step 2: Access JSON data correctly
JSON data is a dictionary, so access fields with data['name'], not data.name.
Final Answer:
def get_name(request):
data = request.get_json()
return f"Hello, {data['name']}!" -> Option B
Quick Check:
Use get_json() and dict access [OK]
Hint: Use get_json() and dict keys for JSON data [OK]
Common Mistakes:
Using request.json() instead of get_json()
Accessing JSON fields with dot notation
Forgetting parentheses on get_json
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.