Using Environment Variables in Google Cloud App Engine for Nodejs

Posted By : Ishaan Madan | 09-Jan-2020

Google App Engine is a web framework that is amongst one of the services provided by the GCP (Google Cloud Platform). Its main purpose is to help in developing as well as hosting the web applications in Google Datacenters. App Engine makes use of a file name "app.yaml" to store and define environment variables for the application.

But, since app.yaml being part of the code needs to be committed to version control systems like GIT, Node.js web developers may tend to avoid storing secret information in the file. To solve this dilemma one a may make the use of google cloud storage. The Blog explains how to use google storage bucket to store environment variables and using them in our application.

 

Pre-requisites:

  • Operating System - Ubuntu.
  • Google Cloud CLI (latest stable version).
  • NPM and Nodejs (latest stable version).
  • Editor (Nano/VS code/Gedit)

 

Implementation:

Step 1:- Storing Information in Google Cloud Storage

Select,  Google Cloud -> Storage -> Browser.

Creating a Storage Bucket. The name of the bucket generally includes:

  • envvars
  • <project name>
  • <domain> include

For Instance, we create an application with a project named as “blogs”, to enlist all the blogs. I would name it envvars.blogs.oodles.com.


In Google Cloud, the bucket names are unique throughout the platform and are not specific to a company or project. However, the names ending with a domain are reserved and ownership of them needs to be verified.

 

Create a .env file and upload it to this bucket. Example:

#.env
MY_API_KEY=xxxx-xxxx-xxxx-xxxx
MY_OTHER_API_KEY=xxxx-xxxx-xxxx

        

 

Step 2:- Import the buket content to process.env

Installing Dependencies

 
npm install --save @google-cloud/storage  dotenv

 

Adding the prestart hook to the file package.json

#package.json
"scripts": {
  "prestart": "node getEnv",
  "start": "node index.js"
}
        

 

 

Create and Adding Script in getEnv.js to Download .env from Bucket:

 use strict;

const fs = require('fs')

const dotEnvExists = fs.existsSync('.env')
if (dotEnvExists) {
  console.log('getEnv.js: .env exists, probably running on development environment')
  process.exit()
}

// On Google Cloud Platform authentication is handled for us
const gcs = require('@google-cloud/storage')()

const bucketName = `envvars.${process.env.GCLOUD_PROJECT}.gunargessner.com`
console.log(`Downloading .env from bucket "${bucketName}"`)
gcs
  .bucket(bucketName)
  .file('.env')
  .download({ destination: '.env' })
  .then(() => {
    console.info('getEnv.js: .env downloaded successfully')
  })
  .catch(e => {
    console.error(`getEnv.js: There was an error: ${JSON.stringify(e, undefined, 2)}`)
  })
        

 

Usage: Using dotenv to load the content from .env into process.env

 use strict'

require('dotenv').config() // Loads .env

// ...

        

 

 

Conclusion:

Using the bucket in google cloud storage service we have hidden the secret information from the version control systems.  

 

Thanks

 

 

About Author

Author Image
Ishaan Madan

Ishaan, a skilled technical project manager, excels at breaking down complex projects into manageable tasks. With a background in both technology and project management, he offers a unique perspective to every project he undertakes. His effective communication skills enable him to collaborate seamlessly with both technical and non-technical stakeholders, ensuring everyone is aligned towards shared objectives. He has hands-on experience in utilizing agile methodologies like Scrum and Kanban to drive project management and foster team collaboration. He leverages project management tools such as JIRA, Trello, and Clickup to monitor progress, manage tasks, and facilitate communication among team members and stakeholders. Moreover, his proficiency in full-stack development empowers him to comprehend the technical aspects of projects and provide guidance to developers when necessary. He demonstrates expertise in utilizing popular Python frameworks like Django and Flask, along with data analysis and manipulation libraries such as NumPy and Pandas. On the front-end, Ishaan adeptly employs JavaScript libraries like React and Angular to craft visually appealing and user-friendly interfaces. Additionally, he possesses proficiency in HTML, CSS, and JavaScript for designing responsive and mobile-friendly layouts.

Request for Proposal

Name is required

Comment is required

Sending message..