Google credential json file on Serverless AWS

Hey everyone, I try to use DialogFlow (Google Cloud Dialogflow API) on my serverless project but the problem that I couldn’t find any solution for pushing google credential json file to serverless side. I followed this tutorial (it’s on google cloud website) and It works correctly on my local but not on lambda. I even tried to copy the file from webpack but It still doesn’t work. For DialogFlow, I’m using dialogflow v2 nodejs library.

node.js: 6.x

serverless: 1.26

====

serverless.yml

service: test-dialogflow-svc

plugins:
  - serverless-webpack
  - serverless-plugin-common-excludes
  - serverless-offline
  - serverless-offline-scheduler

package:
  individually: true
  include:
    - googleCredentials.json

custom:
  webpackIncludeModules: true
  serverless-offline:
    port: 3000

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: eu-west-2
  memorySize: 128
  timeout: 5
  environment:
    GOOGLE_APPLICATION_CREDENTIALS: './googleCredentials.json'

functions:
  hello:
    handler: src/handlers/helloworld.handler
    events:
      - http:
          path: hello
          method: get
    package:
      include:
        - googleCredentials.json

webpack.config.js

const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const WebpackPluginCopy = require('webpack-plugin-copy');

module.exports = {
  entry: slsw.lib.entries,
  target: 'node',
  resolve: {
    extensions: ['.js', '.json', '.ts', '.tsx']
  },
  externals: [nodeExternals()],
  module: {
    rules: [
      {
        test: /\.ts(x?)$/,
        use: [
          {
            loader: 'awesome-typescript-loader'
          }
        ]
      }
    ]
  },
  plugins: [ // I tried to copy file with webpack as well
     new WebpackPluginCopy([{
       copyPermissions: true,
       from: './googleCredentials.json'
     }])
  ],
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js'
  }
};

I had a similar problem some time ago with accessing google API from aws lambda.
I wanted to use Google Sheets API with a python library which also needs google credentials for authentication to access the API. It worked fine on my local machine but failed on aws lambda.

The problem was that this library wanted to write something back to the credentials.json file - which of course always failed, because it is not allowed to write back to files on aws lambda(?).
I assume this is necessary for the OAuth process to update some local keys(?).
But i am not an OAuth expert - so i wasn’t able to fix this…

The solution was that i setup an Google Service Account and used the credentials from this account (instead of the Google IAM credentials) and than it worked.

Maybe this also helps for your problem?

Thanks for your suggestion @Franky! I found the way how to use credential file actually but the problem is a serverless framework (or aws lambda) doesn’t support grpc (google cloud API is using this). And it always gives this error:

(rejection id: 2): Error: Cannot find module '/var/task/node_modules/grpc/src/node/extension_binary/node-v48-linux-x64-glibc/grpc_node.node'

I couldn’t find any solution for this unfortunately. I really don’t understand why It doesn’t work. I added to script on package.json for post install but It didn’t help as well.

"postinstall": "npm rebuild grpc --target=6.1.0 --target_arch=x64 --target_platform=linux --target_libc=glibc"

Yes the gRPC node library uses node c++ native modules (https://github.com/grpc/grpc-node). This article talks about using node native c++ modules on AWS Lambda:
https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/

I’m trying also to use some Google Cloud node client on AWS Lambda.
Could you explain how you manage to solve the initial problem with the Google credential .json file?

Hey @Franky I am having the exact similar problem as yours. My python script is trying to access google sheets. I am using Pygsheets package(even tried gspread) and these are the ways I tried to authorize https://stackoverflow.com/questions/51237099/pygsheets-how-can-i-authorize-google-sheets-in-aws-lambda

But successfully failed.
I see that you did it with the help of Google Service Account. Is there a documentation that you can provide on how to do this. Will be helpful.

Thank you

Hey Guys, any update about that? I facing the same issue with Python Dialogflow client…