Adding Google Cloud Credentials to Heroku

Adding Google Cloud Credentials to Heroku

's avatar
Bryan Borgesano ・February 23, 2021
1 minute read ・Updated February 24, 2021

THE PROBLEM

If your project is using a package that loads credentials from a file instead of an environmental variable, configuration with Heroku can be confusing.

I ran into this issue when adding @google-cloud/storage to my project. The solution I found that kept my credentials hidden and secure was using this build pack from buyersight which generates the file on Heroku when you deploy.

GOOGLE CLOUD AUTH WITH JSON FILE

If you haven't enabled a service account, downloaded your credential file and set it up in node, watch the video below by Ben Awad for a great explanation on how to do that (start at 2:00 minutes).

RENAME YOUR CREDENTIAL FILE

Your project must be set up to read the data from "google-credentials.json" for this to work. So re-name your file from the given file name, something like "apollo-test-ac46eef8fd13.json" to "google-credentials.json".

ENABLE BUILD PACK

Next, you need to enable this build pack using either:

CLI INSTALLATION

  • Replace your-app-name with your app name
heroku buildpacks:set https://github.com/buyersight/heroku-google-application-credentials- buildpack.git -a your-app-name

OR

HEROKU DASHBOARD

Add build pack on Heroku dashboard

SET UP CONFIG VARS ON HEROKU

After that, the config variables need to be set up.

Checkout our latest product - the ultimate tailwindcss page creator 🚀
  • GOOGLE_APPLICATION_CREDENTIALS

    • This will hold the value "google-credentials.json"
  • GOOGLE_CREDENTIALS

    • This will hold the contents of the service account credential JSON file (copy and paste the JSON object into the text box)

Heroku config vars

DEPLOY YOUR APP

Now that the build pack is enabled and the config variables are set up, the app can be deployed and the google-credentials.json file will be generated on Heroku's file system.

CHECK THAT THE FILE WAS GENERATED

You can check this was completed successfully by navigating to the Heroku console, running 'bash' then 'ls' to see if the file is there.

Heroku bash

SUCCESS

If you see google-credentials.json, you have configured it successfully!

Your new feature will now work on your Heroku server while keeping your credentials hidden.

Great Success!

Comments (0)