Matthew P
Matthew P

Reputation: 735

Communicate internally between Google Cloud Functions?

We've created a Google Cloud Function that is essentially an internal API. Is there any way that other internal Google Cloud Functions can talk to the API function without exposing a HTTP endpoint for that function?

We've looked at PubSub but as far as we can see, you can send a request (per say!) but you can't receive a response.

Ideally, we don't want to expose a HTTP endpoint due to the extra security ramifications and we are trying to follow a microservice approach so every function is its own entity.

Upvotes: 3

Views: 2067

Answers (2)

Martin Omander
Martin Omander

Reputation: 3604

I sympathize with your microservices approach and trying to keep your services independent. You can accomplish this without opening all your functions to HTTP. Chris Richardson describes a similar case on his excellent website microservices.io:

You have applied the Database per Service pattern. Each service has its own database. Some business transactions, however, span multiple services so you need a mechanism to ensure data consistency across services. For example, lets imagine that you are building an e-commerce store where customers have a credit limit. The application must ensure that a new order will not exceed the customer’s credit limit. Since Orders and Customers are in different databases the application cannot simply use a local ACID transaction.

He then goes on:

An e-commerce application that uses this approach would create an order using a choreography-based saga that consists of the following steps:

  1. The Order Service creates an Order in a pending state and publishes an OrderCreated event.
  2. The Customer Service receives the event attempts to reserve credit for that Order. It publishes either a Credit Reserved event or a CreditLimitExceeded event.
  3. The Order Service receives the event and changes the state of the order to either approved or cancelled.

Basically, instead of a direct function call that returns a value synchronously, the first microservice sends an asynchronous "request event" to the second microservice which issues a "response event" that the first service picks up. You would use Cloud PubSub to send and receive the messages.

You can read more about this under the Saga pattern on his website.

Upvotes: 2

Doug Stevenson
Doug Stevenson

Reputation: 317372

The most straightforward thing to do is wrap your API up into a regular function or object, and deploy that extra code along with each function that needs to use it. You may even wish to fully modularize the code, as you would expect from an npm module.

Upvotes: 0

Related Questions