6

Google Cloud Functions Tutorial : Using the Local Functions Emulator

 3 years ago
source link: https://rominirani.com/google-cloud-functions-tutorial-using-the-local-functions-emulator-bd351bbe61de
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Google Cloud Functions Tutorial : Using the Local Functions Emulator

This is part of a Google Cloud Functions Tutorial Series. Check out the series for all the articles.

If you have been following the series so far, you would have noticed that to test out the function, you need to deploy the function first and then test it out, via the Cloud Console or via the gcloud tool.

This could get tedious at times since the deploy instruction takes a while to complete and during development, you would like to perform multiple iterations. It would be great to have a local environment where you could both deploy the Cloud Function.

The Local Functions Emulator is just that. It gives you a local service to which you can deploy your functions and test them out before deploying it to the Google Cloud Functions live environment.

To get started with the Local Functions Emulator, you will need to have your local setup in place as explained in an earlier post. I will repeat the relevant section here for your reference:

Local Development Setup

For your local development environment, you will need to have the Node and npm tools setup. Please install them from https://nodejs.org/en/download/ and install it for your environment.

On my machine, I have the following setup:

$ node --version
v8.9.3$ npm --version
6.4.1

Keep in mind that at the time of writing, Google Cloud Functions support multiple runtime environments that allow you to write your Cloud Functions in JavaScript, Python or Go (Alpha). We are using JavaScript in this tutorial and depending on Node.js v6 or Node.js v8 that you prefer, you will need support for that in your local environment.

Once you have Node.js installed, you need to setup the Local Functions Emulator. This is a tool made available by Google to help you test your functions locally without the need to deploy to the live environment every time. We will cover this in a post later in the series. For now, install the Local Functions Emulator via the command given below:

npm install -g @google-cloud/functions-emulator

Finally, I have Visual Studio Code on my machine. It is a great and fast IDE that helps me develop and test stuff locally. I recommend it but you are free to use an IDE of your choice.

Now that we have our Local Emulator setup, let us see what we can do with it.

Start and Stop the Emulator

First up, ensure that the correct project id is set for the local emulator by giving the following command:

$ functions config set projectId PROJECT_ID
...

The next step to do is to start the Local Emulator. This is an absolute must before you can use it.

$ functions startStarting Google Cloud Functions Emulator...
Google Cloud Functions Emulator STARTED
No functions deployed ¯\_(ツ)_/¯. Run functions deploy --help for how to deploy a function.

Deploying / Invoking a Local Emulator function

To test this out, go to the root folder of the Github cloned repository for this series. Go to the helloworld-http folder as shown below:

$ cd helloworld-http

Now, we can deploy this function locally as given below:

$ functions deploy helloGET --trigger-http

This produces the output as shown below:

Image for post
Image for post

You can see that the function was successfully deployed. You can check out a list of functions deployed locally via the following command:

$ functions list
Image for post
Image for post

To invoke a function locally, you can use the call method on the functions command as shown below. Several examples are shown below:

Invoke without passing parameters

$ functions call helloGET
ExecutionId: 67c11945-f4db-430d-8b8e-f6df9ac7e4e5
Result: Hello World!

Invoke with passing parameters

$ functions call helloGreeting --data '{"name":"Local Emulator"}'
ExecutionId: db18db5f-9836–4cb9-b067–6a06588974d2
Result: Hello Local Emulator

Viewing local emulator function execution logs

$ functions logs read
2018–04–01T13:06:58.946Z — info: User function triggered, starting execution
2018–04–01T13:06:58.952Z — info: Execution took 8 ms, user function completed successfully
2018–04–01T13:08:24.163Z — info: User function triggered, starting execution
2018–04–01T13:08:24.168Z — info: Execution took 4 ms, user function completed successfully
...

Other functions commands

There are several other commands of utility while working with the Local Emulator. They are given below:

//[To delete a function]$ functions delete FUNCTION_NAME//[To clear the logs]$ functions logs clear//[To stop the emulator process]$ functions stop

The last one is particular important to stop the Local Emulator gracefully. You will find that if you stop and start the Emulator, it will persist your local functions that you had deployed earlier. You do not need to deploy them again locally.

Proceed to the next part : Debug your Functions locally or go back to the Google Cloud Functions Tutorial Home Page.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK