Skip to content
This repository was archived by the owner on Dec 1, 2022. It is now read-only.

Adding region & table(s) envvar overrides #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CapacityCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class CapacityCalculator extends CapacityCalculatorBase {

// Get the region
getCloudWatchRegion() {
return Region;
return process.env.DDB_AUTOSCALE_REGION || Region;
}

getStatisticSettings(): StatisticSettings {
Expand Down
12 changes: 8 additions & 4 deletions src/Provisioner.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ export default class Provisioner extends ProvisionerConfigurableBase {

// Get the region
getDynamoDBRegion(): string {
return Region;
return process.env.DDB_AUTOSCALE_REGION || Region;
}

// Gets the list of tables which we want to autoscale
async getTableNamesAsync(): Promise<string[]> {
// Option 1 - Tables defined by an environment variable if defined
if ('DDB_AUTOSCALE_TABLES' in process.env && typeof process.env.DDB_AUTOSCALE_TABLES === 'string') {
return process.env.DDB_AUTOSCALE_TABLES.split(',');
}

// Option 1 - All tables (Default)
// Option 2 - All tables (Default)
return await this.db.listAllTableNamesAsync();

// Option 2 - Hardcoded list of tables
// Option 3 - Hardcoded list of tables
// return ['Table1', 'Table2', 'Table3'];

// Option 3 - DynamoDB / S3 configured list of tables
// Option 4 - DynamoDB / S3 configured list of tables
// return await ...;
}

Expand Down
34 changes: 34 additions & 0 deletions src/provisioning/ProvisionerConfigurableBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@ import type {
TableConsumedCapacityDescription,
} from '../flow/FlowTypes';

import { SNS } from 'aws-sdk';
import { log } from '../Global';

export default class ProvisionerConfigurableBase extends ProvisionerBase {
logSnsMessage(arn: ?string, subject: string, message: string) {
if (arn) {
const sns = new SNS();
const params = {
Message: message,
Subject: subject,
TopicArn: arn
};

sns.publish(params, (...args) => { log('Sent through an SNS message', args, params); });
}
}

// eslint-disable-next-line no-unused-vars
isReadCapacityIncrementRequired(data: TableProvisionedAndConsumedThroughput): boolean {
Expand Down Expand Up @@ -128,19 +143,38 @@ export default class ProvisionerConfigurableBase extends ProvisionerBase {
newProvisionedThroughput.ReadCapacityUnits = this
.calculateIncrementedReadCapacityValue(params);

const subject = `Increasing RCU for ${params.TableName}`;
const message = `Increasing read capacity units from ${params.ProvisionedThroughput.ReadCapacityUnits} to ${newProvisionedThroughput.ReadCapacityUnits}`;

this.logSnsMessage(process.env.DDB_AUTOSCALE_SNS_ARN, subject, message);

} else if (this.isReadCapacityDecrementRequired(params)) {
newProvisionedThroughput.ReadCapacityUnits = this
.calculateDecrementedReadCapacityValue(params);

const subject = `Decreasing RCU for ${params.TableName}`;
const message = `Decreasing read capacity units from ${params.ProvisionedThroughput.ReadCapacityUnits} to ${newProvisionedThroughput.ReadCapacityUnits}`;

this.logSnsMessage(process.env.DDB_AUTOSCALE_SNS_ARN, subject, message);
}

// Adjust write capacity
if (this.isWriteCapacityIncrementRequired(params)) {
newProvisionedThroughput.WriteCapacityUnits = this
.calculateIncrementedWriteCapacityValue(params);

const subject = `Increasing WCU for ${params.TableName}`;
const message = `Increasing write capacity units from ${params.ProvisionedThroughput.WriteCapacityUnits} to ${newProvisionedThroughput.WriteCapacityUnits}`;

this.logSnsMessage(process.env.DDB_AUTOSCALE_SNS_ARN, subject, message);
} else if (this.isWriteCapacityDecrementRequired(params)) {
newProvisionedThroughput.WriteCapacityUnits = this
.calculateDecrementedWriteCapacityValue(params);

const subject = `Decreasing WCU for ${params.TableName}`;
const message = `Decreasing write capacity units from ${params.ProvisionedThroughput.WriteCapacityUnits} to ${newProvisionedThroughput.WriteCapacityUnits}`;

this.logSnsMessage(process.env.DDB_AUTOSCALE_SNS_ARN, subject, message);
}

if (newProvisionedThroughput.ReadCapacityUnits ===
Expand Down