Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Googlescript and AWS SDK

I want to interact with Amazon Web Services DynamoDB in a Google Sheet via code in a GoogleScript. However, I cannot figure out how to integrate the AWS SDK. I am hoping to avoid having to write a library to handle the integration via the AWS HTTP API, as there are JavaScript and Java SDKs available for the SDK. Help?

(I've done some pretty extensive Google and Stack Overflow searches. This is the closest thing I've found to an answer, but that is the Google App Engine, not the Google Apps Script.)

Thanks!

like image 301
heights1976 Avatar asked Dec 31 '15 18:12

heights1976


People also ask

Can I use JavaScript for AWS?

AWS SDK for JavaScript is a set of open-source, free-to-use JavaScript libraries that integrate with AWS services. They support API development, higher-level abstractions, and three types of applications: JavaScript for browser. Node.

Is Appscript the same as JavaScript?

Apps Script is a coding language designed so you can do more with Google applications like Drive, Docs, Sheets, Calendar, Gmail, and more. It is based on JavaScript and runs in the cloud rather than on your device.

Is Google Apps Script an API?

The Google Apps Script API lets you programmatically create, modify, and deploy Apps Script projects—actions that otherwise require you to use the Apps Script editor. Your apps can use the API to manage your script projects, create and deploy new script versions, and monitor script executions.


3 Answers

I just made a function that does the basic authentication for any api request you want to make. You still might have to get some of the headers in order but it does most of the hard work for you.

For example:

function myFunction() {
  AWS.init("MY_ACCESS_KEY", "MY_SECRET_KEY");
  var instanceXML = AWS.request('ec2', 'us-east-1', 'DescribeInstances', {"Version":"2015-10-01"});
  ...
}

I put it in a repo with some documentation for use. Here's the link: https://github.com/smithy545/aws-apps-scripts

like image 96
smithy545 Avatar answered Sep 24 '22 12:09

smithy545


I recently added my own derivative of the "AWS via Google Apps Script" here: https://github.com/neilobremski/gas/blob/main/GasAWS.js

The JavaScript code here is a bit more modern and specifically uses the Utilities available in Google Apps Script for the SHA-256 stuff rather than the abandoned (but excellent!) Crypto-JS library.

Usage is the same except I moved the region parameter and have a global default. I did this because often resources are in the same region and repeatedly specifying it is a pain.

like image 38
Neil C. Obremski Avatar answered Sep 24 '22 12:09

Neil C. Obremski


Responding if anyone else finds this useful, one of the responses above gave a good hint for me to get started but not able to add comment on it.

Steps in below tutorial walks through setting up API Gateway, Lambda and DynamoDb. Once setup, you can directly use URLFetch in GAS to your API Gateway.

http://docs.aws.amazon.com/lambda/latest/dg/with-on-demand-https-example.html

Code in AWS link is complete and needs no additional setup to work directly using URL Fetch. You may however want to enable security on API Gateway.

On GAS side -

var headers = {
  "x-api-key" : AWS_KEY
};    

var event = {};
event.operation = "create";
event.tableName = AWS_DB_NAME;
event.payload.Item = yourDataObjwithKey;

var options = {
  "method":"POST",
  "headers":headers,
  "payload":JSON.stringify(event)
};    

var url = AWS_GW;
var response = UrlFetchApp.fetch(url, options);
like image 37
pdholakia Avatar answered Sep 24 '22 12:09

pdholakia