Cloud Functions
Cloud serverless processing (often called serverless computing) is an execution model where cloud providers automatically manage the underlying infrastructure, allowing developers to build and run application code without provisioning, configuring, or maintaining servers.
Despite the name, servers still exist; they are simply completely abstracted away from the user.
Core Characteristics
No Server Management: The provider handles OS patching, provisioning, hardware maintenance, and capacity planning.
Event-Driven Execution: Code runs in response to triggers—such as an API HTTP request, a file upload to cloud storage, or a scheduled timer.
Automatic Scaling (Scale-to-Zero): The system instantly scales up compute resources during high traffic and scales back down to zero when idle.
Pay-per-Use Billing: Charges are strictly calculated based on the exact execution time and resources consumed while the code is active. You do not pay for idle capacity.
Primary Models & Delivery Mechanisms
Microservices (or microservice architecture) is a software design approach where an application is built as a collection of small, independent, and loosely coupled services.
Instead of building a single, massive codebase (a monolith), the application's features are broken down into distinct, self-contained services that each handle one specific business function (e.g., user authentication, payment processing, or order tracking).
Core Characteristics
Single Responsibility: Each service focuses on a single domain or function.
Independent Deployment: Teams can update, fix, or deploy individual services without redeploying the entire application.
Technology Agnostic: Different services can be written in different programming languages or use different database types (e.g., Python for machine learning, Node.js for real-time chat).
API Communication: Services interact with one another through lightweight protocols, usually standard HTTP/REST APIs, gRPC, or asynchronous message queues (e.g., Kafka, RabbitMQ).
Microservices describe how you structure an application, while serverless computing describes how you run it.
While they are distinct architectural concepts, they complement each other naturally—serverless is often the ideal infrastructure choice for running microservices.
How They Connect
Natural Fit for Small Units of Code: Microservices break an application into small, single-purpose components. Serverless platforms (like FaaS or serverless containers) are explicitly designed to execute small, focused workloads on demand.
Granular Scaling: Because microservices operate independently, traffic to an application rarely hits every feature equally. Serverless allows each microservice to auto-scale on its own—scaling payment processing up during a sale without wasting compute on underutilized profile services.
Operational Independence: Microservices aim to minimize dependencies between development teams. Serverless furthers this by removing infrastructure management, letting teams deploy their individual services without configuring servers, load balancers, or clusters.
Architectural Comparison
Serverless Microservice Patterns
Function-based (FaaS): Each individual API endpoint is backed by a single serverless function (e.g., an HTTP GET request triggers a function that fetches a user profile).
Container-based: An entire microservice containing multiple endpoints is packaged into a lightweight container and run on a serverless container platform, scaling down to zero when inactive.
Google Cloud Functions is Google Cloud’s lightweight, event-driven Function-as-a-Service (FaaS) platform. It lets developers deploy single-purpose snippets of code without managing servers, OS updates, or infrastructure provisioning.
Core Functionality
Automatic Infrastructure: Write your code in languages like Python, Node.js, Go, or Java; Google automatically handles containerization, scaling, and runtime maintenance.
Event-Driven Triggers: Functions spin up in response to specific cloud events, such as an HTTP API call, a file upload to Cloud Storage, a message on Cloud Pub/Sub, or a database modification in Firestore.
Instant Auto-Scaling: Scales automatically from zero to thousands of concurrent executions when traffic spikes, and immediately scales back down to zero when idle.
Sub-Second Billing: Charges are calculated strictly for the memory, CPU, and execution time consumed while the function processes a request (down to 100ms increments).
Common Use Cases
Data Processing Pipelines: Automatically generating image thumbnails or transforming logs whenever a file lands in a storage bucket.
Webhooks & Lightweight APIs: Serving as small, serverless backends for mobile apps or third-party webhooks (e.g., Stripe, Slack bots).
Real-time Stream Processing: Parsing IoT telemetry or event streams pushed from Cloud Pub/Sub.
Automation & Task Scheduling: Running periodic maintenance tasks, cleanup scripts, or cloud resource audits.
HTML examples of cloud functions
Note https://form-values-517129368909.us-east1.run.app is the link created when you build a cloud function
==========================================
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form function test</title>
</head>
<body>
<h1>Student Profile test</h1>
<form name="myForm" method="post" action="https://form-values-517129368909.us-east1.run.app">
<p>Student Name : </p>
<p><input type="text" name="my-name" value="Your name"></p>
<p>Major : </p>
<p><input type="text" name="my-major" value="Your major"></p>
<p><input type="submit"></p>
</form>
</body>
</html>
===================================================
Create a cloud python function to process form data
Go to cloud run
In the Write a function section click python
Enter form-values for Service name
Us-east1 for Region
Click Allow public access to let web users execute
Then hit Create
Enter functions-framework>=3.0.0 in the requirements.txt file
Python program to return HTML formatted values
========================================
import functions_framework
@functions_framework.http
def parse_student_form(request):
# Extract form fields sent via POST
student_name = request.form.get('my-name', 'Not provided')
student_major = request.form.get('my-major', 'Not provided')
# Construct the HTML response page
html_content = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Profile Received</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 40px;
background-color: #f4f4f9;
}}
.container {{
background: white;
padding: 20px 30px;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
max-width: 500px;
}}
h1 {{
color: #333;
}}
table {{
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}}
th, td {{
padding: 12px;
text-align: left;
border-bottom: 1px solid #ddd;
}}
th {{
background-color: #4CAF50;
color: white;
}}
tr:hover {{ background-color: #f5f5f5; }}
</style>
</head>
<body>
<div class="container">
<h1>Submission Received</h1>
<p>Below is the data processed from your form submission:</p>
<table>
<tr>
<th>Field Description</th>
<th>Form Field Name</th>
<th>Submitted Value</th>
</tr>
<tr>
<td><strong>Student Name</strong></td>
<td><code>my-name</code></td>
<td>{student_name}</td>
</tr>
<tr>
<td><strong>Major</strong></td>
<td><code>my-major</code></td>
<td>{student_major}</td>
</tr>
</table>
<br>
<a href="javascript:history.back()">← Back to Form</a>
</div>
</body>
</html>
"""
# Return HTML string with explicit text/html header
return (html_content, 200, {'Content-Type': 'text/html'})
=====================================================
Paste code in main.pl change Function entry point to parse_student_form
Click Save and redeploy
All processes should be green if successful
From cloud shell copy formtest.html to cloud storage bucket
$ gcloud storage cp formtest.html gs://cloud-storage-exam/
Copying file://formtest.html to gs://cloud-storage-exam/formtest.html
Completed files 1/1 | 455.0B/455.0B
john_iacovacci1@cloudshell:~/webform (cloud-project-examples)$
Go to bucket and execute html
No comments:
Post a Comment