The Challenge
You need a simple HTTP endpoint that executes code without managing servers. Cloud Functions provide event-driven serverless execution with automatic scaling, a managed HTTPS endpoint, and pay-per-request pricing.
What You'll Build
- → Cloud Function with HTTP trigger and public HTTPS endpoint
- → Automatic scaling from zero to match incoming traffic
- → Public access configured for external invocation
- → Trigger URL exported for integration with other services
- → Pay-per-request pricing with no idle costs
Try This Prompt in Pulumi Neo
Run this prompt in Neo to deploy your infrastructure, or edit it to customize.
Best For
Architecture Overview
This architecture deploys a single Cloud Function with an HTTP trigger, which is the simplest serverless compute unit on Google Cloud. You provide the function code, and Google handles provisioning, scaling, TLS termination, and request routing. The result is a public HTTPS endpoint that executes your code in response to HTTP requests and scales to zero when idle.
Cloud Functions uses HttpCallbackFunction, which lets you define the function’s behavior inline or reference a local file. The function receives the HTTP request object with headers, query parameters, and body, processes it, and returns a response. Google’s infrastructure handles everything between the client’s HTTPS request and your function’s invocation.
The function is configured with public access by granting the cloudfunctions.invoker role to all users. This means anyone with the URL can invoke the function, which is the appropriate configuration for public APIs, webhooks, and other endpoints that receive requests from external systems. For internal or authenticated endpoints, you would restrict this IAM binding.
Cloud Function
The Cloud Function is the compute unit. It runs your code in a managed environment with a specific language runtime. Each invocation is independent, and Google provisions instances as needed to handle concurrent requests. When there are no requests, the function scales to zero, and you incur no charges.
Cold starts occur when Google needs to provision a new instance after a period of inactivity. For most HTTP-triggered functions, cold start latency is in the low hundreds of milliseconds. If your function is called regularly, instances stay warm and respond with minimal latency.
HTTP Trigger
The HTTP trigger provides a stable HTTPS endpoint managed by Google. The URL follows a predictable pattern based on your project, region, and function name. TLS termination is handled automatically, so your function code does not need to manage certificates. The endpoint supports all standard HTTP methods (GET, POST, PUT, DELETE), and your function can inspect the method to implement RESTful routing.
IAM and Access Control
Access to the function is controlled through IAM bindings. Granting cloudfunctions.invoker to allUsers makes the function publicly accessible. For production services that should not be publicly exposed, you can restrict invocation to specific service accounts, authenticated users, or other Google Cloud services.
When Cloud Functions are invoked by other Google services (like Pub/Sub or Cloud Scheduler), you grant the invoker role to the specific service account rather than to all users. This follows the principle of least privilege while still enabling event-driven architectures.
Common Customizations
- Add authentication: Remove the
allUsersbinding and require Google identity tokens for invocation, restricting access to authenticated callers. - Connect to a database: Add environment variables with connection details for Cloud SQL or Firestore, and grant the function’s service account appropriate database permissions.
- Increase timeout and memory: Adjust the function’s resource allocation if your handler performs complex processing, makes external API calls, or handles large payloads.
- Add Cloud Scheduler: Create a Cloud Scheduler job to invoke the function on a cron schedule for periodic tasks like data cleanup or report generation.
Related Prompts
Deploy Multi-Language Serverless Functions
You need to deploy functions in different programming languages for different use cases within the same project. Cloud …
Deploy Cloud Run Services with Custom Containers
You need to deploy containerized applications as serverless services with automatic scaling, HTTPS endpoints, and …
Deploy a Kubernetes Microservices Application
You need to run a multi-service application on Kubernetes where each component scales independently and communicates …
Create a Serverless Video Thumbnail Extractor
You need to automatically process video files when they are uploaded, extracting thumbnails or frames without managing …