Cloud function Save order raw data to datastore
What is it
This cloud function is here to register raw data from microservices and the api inside datastore.
Dependencies
- Node 12
- gcloud cli
Install
- Install node dependencies
yarn lerna bootstrap
- Authenticate with Google Default Credential
gcloud auth application-default login
Your user will be used to deploy the serverless stack, you must have at least the following roles on the targeted project:
- Deployment Manager Editor
- Storage Admin
- Logging Admin
- Cloud Functions Developer
- copy .env.exemple
Development workflow
In your local machine, functions are run individually in tests. They are never run in a local stack.
Working on an existing function
-
Write your code changes
-
Test it with unit tests
-
Trigger the function locally in integration with the
dev
stack (cf local invocation) -
Deploy the stack:
yarn sls deploy
-
Test in integration with the
dev
stackYou can use
yarn sls invoke -f <functionName> -p <path/to/an/event/mock>
to trigger your function with a mocked payload
or trigger the service which will trigger the function with a real payload
Create a new function
-
Write an empty handler which log the event
-
Configure the triggers of the function
-
Deploy the stack:
yarn sls deploy
-
Trigger the function with the trigger
-
Get the logged event and put it in a
mock.json
to mock the event to help the local development -
Write a test per possible trigger event
-
Write your code changes
-
Test it with unit tests
-
Trigger the function locally in integration with the
dev
stack (cf local invocation) -
Deploy the stack:
yarn sls deploy
-
Test in the
dev
stackYou can use
yarn sls invoke -f <functionName> -p <path/to/an/event/mock>
to trigger your function with a mocked payload
or trigger the service which will trigger the function with a real payload
Local invocation
Functions Framework is used to locally invoke the functions. It starts a development server which handles the http request as CGP handle it and trigger the local cloud function in the closest way of the reality. Functions Framework only handle compiled JS.
- Compile and start the function in the development server
FUNCTION_NAME=<functionName> TYPE=<event|http> yarn local
This will build the code and start it in an express server
- Request the development server with a http request
You can use Curl or PostMan for example
curl --location --request POST 'http://localhost:8080/' \
--header 'Content-Type: application/json' \
--data-raw '{
"orderId": "77b787d4-e9eb-4797-b78f-53da679a831c",
"restaurantId": 623
}'
Good to know:
- To invoke a http triggered function with json body, you need to provide the header
Content-Type
withapplication/json
- To invoke an event triggered function, call the server with a json body composed of two attributes:
-
data
(required): the event -
context
(optional): the context
-
- You will really interact with the real stack. Never set production variables in your
.env
- The GCP libraries will authenticate them self with the Google Default Credential
- credentials from
GOOGLE_APPLICATION_CREDENTIALS
if exist - credentials created by
gcloud auth application-default login
otherwise (recommended)
- credentials from
- You need to rebuild (
yarn local
) between each code modifications. There is currently no watch.
Next steps
Automate this in serverless-google-cloudfunctions to run it in one command
sls invoke local -f $FUNCTION_NAME -p $PAYLOAD_PATH