When you create a web applications, regardless of the framework, you want to make them interactive, make them useful and therefore communicate with external systems to perform operations.
To perform these operations, we usually execute HTTP calls that allow our web application to be able to trigger operations.
Imagine that your application allows you to choose a city from a list to be able to display the current temperature.
To obtain this information, once the city is selected, we can execute an HTTP call that allows you to obtain the temperature.
To complete the process in the example you can create some code that handle the request, calls an external paid service to obtain the information and returns the requested data to your web application.
This code can be executed in a server-side application that has the task of answering the requests that will arrive.
As an alternative to creating an entire application that runs on a server, you can run the same code in Function as a Service (FaaS).
As seen in the previous chapters, this service abstracts the entire infrastructure part and allows you to run your code only when necessary, when there is actually a request to be fulfilled.
Effortless scalability: GCP Cloud Functions (GCF) manages the scaling complexities of your application, automatically allocating resources based on demand.
On Friday there will be more requests to your service to know if the weather will be nice on the weekend. You don't have to worry about increasing resources in your infrastructure to handle the many requests, it will be handled automatically for you.
Cost-effective: You only pay for the resources used when your function is actively running. This pay-as-you-go model significantly reduces costs compared to traditional server-based solutions, especially for applications with fluctuating traffic.
Built-in security: Google manages the underlying infrastructure and security of GCF, freeing you from the burden of securing your servers.
Cons
Cold start: When a function is called for the first time after a period of inactivity, there may be a slight delay in execution while the environment is initializing. This "cold start" can impact the responsiveness of the application for initial requests.
Debugging challenges: Debugging problems in serverless environments can be more challenging than traditional debugging methods, requiring a different mindset and approach.
Limited state management: GCF functions are stateless, meaning they do not retain data between invocations. If your code requires saving state, you may need to explore external solutions to overcome this issue.
The application calls an external paid service to know the temperature of a city. To reduce calls to this paid service and optimize costs, you can consider introducing a cache. As montioned GCF is stateless, you can consider using an external cache service that will help you achieve your goal.
Ensure you have Node.js and a package manager npm installed.
Install and set up the Google Cloud SDK. Here you can find instructions.
Create a new folder for your project.
Navigate into the folder using your terminal.
2. Initialize a new Node.js project
Initialize your project using npm
terminal
Install the necessary dependencies
terminal
typescript: TS compiler for your build process.
@google-cloud/functions-framework: Provides the framework for building Cloud Functions.
3. Create your TypeScript function file
Create a new file named index.ts in your project directory.
Add the following code:
index.ts
4. Configure tsconfig.json
Create a tsconfig.json file in the root of your project.
Add the following configuration to define how the TypeScript compiler should compile your code:
tsconfig.json
5. Build and Deploy the function to Google Cloud
Configure your package.json to include the build and deploy scripts:
package.json
Flags
--gen2: use Cloud Functions (2nd gen).
--region: flag specifies the region in which to deploy your function.
--runtime: flag specifies the runtime ID of a supported Node.js version to run your function.
--trigger-http: flag specifies that the function uses an HTTP trigger.
--allow-unauthenticated: flag specifies that the function can be called without authentication. Omitting this flag means that calls to the function require authentication, which is the default.
6. Test your function
Now you can run
terminal
After successful deployment, Google Cloud will provide you with a URL to access your function.
Open your web browser and paste the URL into the address bar.
You should see the output of your function, Hello from learnbydo.ing Cloud Function!.
For your GCP console you can see and manage your new Cloud Function.
Congratulations! You've now successfully ventured into the world of serverless computing with Google Cloud Functions.
By understanding the core concepts of Cloud Functions, you've equipped yourself with a powerful tool for developing and deploying code without the complexities of server management.
You've learned about the key components of a Cloud Function, including its trigger, execution environment, and output.
This knowledge opens up a vast realm of possibilities for building efficient, scalable, and cost-effective applications. You can now harness the power of serverless computing to automate tasks, streamline workflows, and create innovative applications that respond seamlessly to real-time events.
In the next lessons you will go into more detail on how to use and configure Google Cloud Function.
You'll look at how to manage environment variables, run tests locally, and much more.