How to make http cloud function only accessible from cloud endpoints
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Cloud functions and Cloud endpoints are two powerful components available in Google Cloud Platform (GCP) that can work together to deliver scalable and secure applications. Cloud Functions allows you to run backend code in response to HTTPS requests or events without provisioning or managing servers. Cloud Endpoints, on the other hand, is a system that provides tools for generating APIs, controlling access, and monitoring usage. By combining these two, you can ensure that your HTTP cloud functions are accessible only via Cloud Endpoints, enhancing both functionality and security.
Background: What are HTTP Cloud Functions and Cloud Endpoints?
HTTP Cloud Functions are stateless functions that are triggered by HTTP requests. These functions can be written in Node.js, Python, Go, or Java, and are a way to execute code in response to web-based events without a complex infrastructure.
Cloud Endpoints provide API management features such as authentication, monitoring, and API keys. They act as a gateway through which requests are processed and forwarded to the appropriate back-end service, like Cloud Functions.
Configuring Cloud Endpoints to Secure HTTP Cloud Functions
Step 1: Deploy Your HTTP Cloud Function
First, deploy your HTTP Cloud Function on GCP. Below is a simple example in Node.js.
Deploy this function with the gcloud command:
Note the URL provided upon successful deployment as it will be needed for the Cloud Endpoint configuration.
Step 2: Enable Cloud Endpoints
Ensure you've enabled the Cloud Endpoints API in your GCP project. Configure an OpenAPI specification document for your endpoint. This specification will detail the configuration of the API's paths, operations, and security rules.
Here's a basic OpenAPI example:
Replace YOUR-CLOUD-FUNCTIONS-REGION and YOUR-PROJECT-ID with the appropriate values.
Step 3: Deploy the Cloud Endpoints Configuration
Deploy the Cloud Endpoints configuration using the following command:
Step 4: Secure the Function
Now, update the Cloud Function to reject requests not routed through Cloud Endpoints:
Step 5: Test the Configuration
Test the secured setup by invoking the Cloud Function via the Cloud Endpoint with an API key.
Summary and Key Considerations
| Step | Description | Key Consideration |
| 1 | Deploy the Cloud Function | Ensure function is online and reachable |
| 2 | Setup and configure Cloud Endpoints | Properly configure API specs |
| 3 | Deploy Cloud Endpoints spec | Check for deployment success |
| 4 | Secure the Cloud Function | Ensure proper conditional checks |
| 5 | Test via Cloud Endpoint | Verify secure access via Endpoint |
Additional Points
- Monitoring and Logging: Utilize GCP's monitoring and logging capabilities to track usage and detect abnormal access patterns.
- Updates and Maintenance: Regularly update both the endpoints definition and cloud functions to accommodate changes in APIs and security patches.
Using Cloud Endpoints in conjunction with Cloud Functions is a robust method to create secure, scalable, and highly manageable serverless applications that capitalize on the strengths of GCP.
Related reading
- How to make MSCK REPAIR TABLE execute automatically in AWS Athena
- How to make Terraform to read AWS Credentials file?
- How to make use of Kubernetes port names?
- How to making async calls to Amazon Bedrock
- How to make HTTP Post request with JSON body in Swift?
- How to make HTTP request in Swift?
- How to make impersonate work with kubernetes go-client
- How to mock JWT authentication in a Spring Boot Unit Test?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.