Adding Google Cloud Credentials to Heroku

Adding Google Cloud Credentials to Heroku

Written by Bryan Borgesano on Feb 23rd, 2021 Views Report Post

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.

  • 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 (2)