GCF locally with env variables
GCF locally with env variables
4 snippets
12 mins
Fabio Biondi
Giorgio Boa
FullStack Developer @ Qarik
Microsoft MVP
 

# Env variables

Let's start with the example from the previous lesson, the implementation of a Cloud Function that queries an external paid service.
Most external services restrict the ability to be publicly queried via an API key that is usually released by the service itself.
 
An API key is a unique identifier that allows you to use an API. It acts like a digital passport, granting you permission to access the service. This is a good practice because it allows you to protect yourself from malicious external attacks that are usually put in place to overload the server with requests.
 
So to implement your functionality you need an API key that you can use. Once you have it you need to decide where to store it in order to use it. It is good practice to avoid incorporating sensitive data such as API keys directly into your code. In fact, this is where environment variables come to your aid.
Environment variables are key-value pairs that provide configuration data to your application. They are accessible at runtime and generally available for the entire Cloud function. Environment variables help you manage sensitive information, configure settings and adapt your code to different environments (development, testing, production) without changing the code itself. We also have a maintainability advantage because you keep the configuration separate from the code for a cleaner organization.
To set environment variables with gcloud functions deploy command you can use the --set-env-vars flag. You can modify the deploy script like this:
package.json
 

Flags

--env-vars-file: with this flag you can supply a YAML file that contains your env variables.
.env.yaml
--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.
 
You can use your environment variables in your function easly:
index.ts

# Local testing

Local testing is essential for Cloud Functions because it allows you to quickly detect errors and debug your code before deploying to Google Cloud. Key benefits of local testing:
Faster iteration: You can quickly test your changes without having to deploy to Cloud Functions each time. Easier debugging: Local development environments offer more **powerful debugging toolsv than the Cloud Function console. Improved code quality: Writing tests locally helps you catch errors early in the development cycle, resulting in more robust code. They also help avoid unexpected behavior in production.

# Run Google Cloud Function locally

Based on the previous lesson you can easly run your GCF locally. The only thing you need to do is run the following command in your terminal:
terminal
Now you can send requests to your **local server **. Open your web browser and paste http://localhost:8080/ into the address bar. You should see the output of your function, Hello from learnbydo.ing Cloud Function!.

# Conclusion

Developing and testing Google Cloud Functions locally with environment variables provides numerous benefits, allowing developers to iterate faster, debug more effectively, and maintain consistency between development and production environments. By leveraging tools like the gcloud CLI, functions-framework and local environment variable management, you can streamline your development workflow and ensure your Cloud Functions are robust and reliable. As your project grows, consider exploring advanced techniques like Dockerization for containerized development and testing, or utilizing a local development server for a more realistic emulation of the cloud environment. Ultimately, the key is to find the right balance between speed and fidelity to maximize your development efficiency and create high-quality, scalable Cloud Functions. In the next lesson you will implement some logic in your function, you will use Gemini AI within your function.
Logo Learn By Doing Light Mode
Topics
Courses
Bookmarks
there are no topics

Send your feedback

EMAIL
ADD CODE SNIPPET
ADD IMAGE/SCREENSHOTS
VOTE