Commit 820559a8 authored by Erik Schwartz's avatar Erik Schwartz 🉐
Browse files

Merge branch 'handle-env' into 'master'

Explicitly require AWS env variables

See merge request !6
parents 8cd3acf7 43a34887
...@@ -11,38 +11,29 @@ Tested with: [Node.js v14 LTS](https://nodejs.org/) and [AWS SDK for Javascript] ...@@ -11,38 +11,29 @@ Tested with: [Node.js v14 LTS](https://nodejs.org/) and [AWS SDK for Javascript]
1. Download and install Node.js LTS 1. Download and install Node.js LTS
2. Clone this repository 2. Clone this repository
3. Install dependences: `npm ci --production` 3. Install dependences: `npm ci --production`
4. [Optional] Copy the example file: `cp settings/demo.json.EXAMPLE settings/foo.json`
5. [Optional] Edit `settings/foo.json` to specify an AWS Region and IAM service account
### Export Node.js variables ### Export AWS variables
```bash ```bash
_node=/path/to/node-v14.x.y export AWS_ACCESS_KEY_ID='bbaa'
export NODE_PATH=${_node}/lib/node_modules export AWS_SECRET_ACCESS_KEY='yyzz'
export PATH=${_node}/bin:$PATH export AWS_REGION='us-east-2'
``` ```
## Use it ## Run
### Run without args to see options
```bash ```bash
node app.js node app.js --name BazNotebook
Usage: app.js --name baz [--profile foo.json]
``` ```
The `--name` option should specify the SageMaker notebook name. If `--profile` is not provided, the caller should export AWS environment variables: The `--name` option should specify the SageMaker notebook name to be stopped.
* [Setting the AWS Region](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-region.html)
* [Loading credentials from environment variables](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html)
This approach can be used for CI build jobs.
--- ---
# IAM policy # AWS IAM
IAM policy suggestion for AWS service account:
The IAM service account that you configure in `settings/foo.json` will need certain authorizations. Suggested starting point for an IAM policy:
```json ```json
{ {
"Version": "2012-10-17", "Version": "2012-10-17",
......
...@@ -6,11 +6,13 @@ ...@@ -6,11 +6,13 @@
// VARIABLE DEFINITIONS // VARIABLE DEFINITIONS
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const appDebug = require('debug')('iq:sm');
const path = require('path'); const path = require('path');
const AWS = require('aws-sdk'); const process = require('process');
const appDebug = require('debug')('iq:sm');
const argv = require('minimist')(process.argv.slice(2)); const argv = require('minimist')(process.argv.slice(2));
const defaultApiVersion = '2017-07-24'; const AWS = require('aws-sdk');
const AWS_ENV = ['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_REGION'];
const AWS_API_VERSION = '2017-07-24';
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// FUNCTIONS // FUNCTIONS
...@@ -28,13 +30,28 @@ function printUsageAndExit() { ...@@ -28,13 +30,28 @@ function printUsageAndExit() {
* Audit CLI args to confirm notebook name was provided. * Audit CLI args to confirm notebook name was provided.
* *
* @param {object} o - CLI args. * @param {object} o - CLI args.
* @returns {boolean} True if valid args.
*/ */
function auditNotebookName(o) { function areCliArgsValid(o) {
appDebug('CLI args:'); appDebug('CLI args:');
appDebug(o); appDebug(o);
const prop = 'name'; const prop = 'name';
if (!o || !o.hasOwnProperty(prop)) printUsageAndExit(); if (!o || !o.hasOwnProperty(prop)) return false;
if (!o[prop] || typeof o[prop] !== 'string') printUsageAndExit(); if (!o[prop] || typeof o[prop] !== 'string') return false;
return true;
}
/**
* Check whether variable is set in the environment.
*
* @param {string} name - Name of environment variable.
* @returns {boolean} True if variable is set and has a truthy value.
*/
function isSetInEnv(name) {
appDebug(`Checking if ${name} is set`);
if (!process.env.hasOwnProperty(name)) return false;
if (!process.env[name]) return false;
return true;
} }
/** /**
...@@ -73,6 +90,11 @@ function bringDownInstance(sm, notebook) { ...@@ -73,6 +90,11 @@ function bringDownInstance(sm, notebook) {
// MAIN LOGIC // MAIN LOGIC
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
auditNotebookName(argv); if (!areCliArgsValid(argv)) printUsageAndExit();
const sm = new AWS.SageMaker({apiVersion: defaultApiVersion});
AWS_ENV.forEach(x => {
if (!isSetInEnv(x)) throw new Error(`Set ${x} in your environment`);
});
const sm = new AWS.SageMaker({apiVersion: AWS_API_VERSION});
bringDownInstance(sm, argv.name); bringDownInstance(sm, argv.name);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment