-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support to auto replicate tables
- Loading branch information
Showing
5 changed files
with
250 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = (arc) => { | ||
let currentRegion | ||
arc.aws.forEach((element) => { | ||
if (element[0] == 'region') { | ||
currentRegion = element[1] | ||
} | ||
}) | ||
|
||
return { | ||
appName: arc.app[0], | ||
currentRegion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = (multiRegion) => { | ||
if (!Array.isArray(multiRegion)) { | ||
throw ReferenceError('Invalid multi region params') | ||
} | ||
|
||
let primaryRegion | ||
if (Array.isArray(multiRegion[0]) && multiRegion[0][0] == 'primary') { | ||
primaryRegion = multiRegion[0][1] | ||
} | ||
else { | ||
throw ReferenceError('Invalid multi region params: Missing primary region') | ||
} | ||
|
||
let replicaRegions | ||
if (multiRegion[1] !== undefined && Array.isArray(multiRegion[1].replicas)) { | ||
replicaRegions = multiRegion[1].replicas | ||
} | ||
else { | ||
throw ReferenceError('Invalid multi region params: Missing replica regions') | ||
} | ||
|
||
return { primaryRegion, replicaRegions } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// eslint-disable-next-line | ||
let aws = require('aws-sdk') // Assume AWS-SDK is installed via Arc | ||
let { toLogicalID } = require('@architect/utils') | ||
let { updater } = require('@architect/utils') | ||
|
||
module.exports = async (arc, stage, dryRun, appName, primaryRegion, currentRegion) => { | ||
const update = updater('MultiRegion') | ||
update.start(`Fetching replicated tables in the replica region (${currentRegion})...`) | ||
|
||
let dynamoReplica = new aws.DynamoDB({ region: currentRegion }) // The current region is a replica region | ||
let ssmPrimary = new aws.SSM({ region: primaryRegion }) | ||
|
||
let tableNames = [] | ||
arc.tables.forEach((table) => { | ||
tableNames = tableNames.concat(Object.keys(table)) | ||
}) | ||
|
||
let tables = [] | ||
for (let tableName of tableNames) { | ||
let PhysicalTableName // aka the physical table name | ||
try { | ||
// Arc app physical table names are stored in SSM service discovery | ||
let Name = `/${toLogicalID(appName)}${toLogicalID(stage)}/tables/${tableName}` | ||
let { Parameter } = await ssmPrimary.getParameter({ Name }).promise() | ||
PhysicalTableName = Parameter.Value | ||
|
||
let { Table } = await dynamoReplica.describeTable({ TableName: PhysicalTableName }).promise() | ||
tables.push({ | ||
arn: Table.TableArn, | ||
logicalName: tableName, | ||
physicalName: PhysicalTableName, | ||
}) | ||
} | ||
catch (err) { | ||
if (err.name === 'ParameterNotFound') { | ||
const message = `${tableName} not found on ${currentRegion}` | ||
if (dryRun) { | ||
update.warn(`${message} (Maybe because is a dry-run)`) | ||
} | ||
else { | ||
update.error(message) | ||
throw (err) | ||
} | ||
} | ||
else if (err.name === 'ResourceNotFoundException') { | ||
const message = `DynamoDB table not found: ${PhysicalTableName}` | ||
if (dryRun) { | ||
update.warn(`${message} (Maybe because is a dry-run)`) | ||
} | ||
else { | ||
update.error(message) | ||
throw (err) | ||
} | ||
} | ||
else { | ||
throw (err) | ||
} | ||
} | ||
} | ||
|
||
update.done(`Replicated tables in replica region fetched`, tableNames) | ||
|
||
return tables | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// eslint-disable-next-line | ||
let aws = require('aws-sdk') // Assume AWS-SDK is installed via Arc | ||
let { toLogicalID } = require('@architect/utils') | ||
let { updater } = require('@architect/utils') | ||
|
||
module.exports = async (arc, stage, dryRun, appName, primaryRegion, replicaRegions, currentRegion) => { | ||
const update = updater('MultiRegion') | ||
const start = Date.now() | ||
const done = () => update.done(`Replication updated in ${(Date.now() - start) / 1000} seconds`) | ||
update.status(`Updating replication on primary region (${currentRegion})`) | ||
|
||
let dynamoPrimary = new aws.DynamoDB({ region: primaryRegion }) | ||
let ssmPrimary = new aws.SSM({ region: primaryRegion }) | ||
|
||
let tableNames = [] | ||
arc.tables.forEach((table) => { | ||
tableNames = tableNames.concat(Object.keys(table)) | ||
}) | ||
|
||
for (let tableName of tableNames) { | ||
let PhysicalTableName | ||
try { | ||
// Arc app physical table names are stored in SSM service discovery | ||
let Name = `/${toLogicalID(appName)}${toLogicalID(stage)}/tables/${tableName}` | ||
let { Parameter } = await ssmPrimary.getParameter({ Name }).promise() | ||
PhysicalTableName = Parameter.Value | ||
|
||
let { Table } = await dynamoPrimary.describeTable({ TableName: PhysicalTableName }).promise() | ||
|
||
let replicateUpdates = [] | ||
|
||
replicaRegions.forEach((replicaRegion) => { | ||
if (!Table.Replicas || Table.Replicas.findIndex((replica) => replica.RegionName == replicaRegion) < 0) { | ||
replicateUpdates.push({ Create: { RegionName: replicaRegion } }) | ||
} | ||
}) | ||
|
||
if (Table.Replicas) { | ||
Table.Replicas.forEach((replica) => { | ||
if (!replicaRegions.includes(replica.RegionName)) { | ||
replicateUpdates.push({ Delete: { RegionName: replica.RegionName } }) | ||
} | ||
}) | ||
} | ||
|
||
const createRegions = replicateUpdates.filter((param) => param.Create).map((param) => param.Create.RegionName) | ||
const deleteRegions = replicateUpdates.filter((param) => param.Delete).map((param) => param.Delete.RegionName) | ||
|
||
update.status( | ||
`Initializing replication for table ${tableName}`, | ||
`Creating replication on regions ... ${createRegions.length > 0 ? createRegions.join(',') : '(skipped)'}`, | ||
`Deleting replication on regions ... ${deleteRegions.length > 0 ? deleteRegions.join(',') : '(skipped)'}` | ||
) | ||
|
||
update.start(`Replicating table ${tableName}...`) | ||
|
||
if (replicateUpdates.length > 0 && !dryRun) { | ||
try { | ||
for (let replicateUpdate of replicateUpdates) { | ||
await dynamoPrimary.updateTable({ | ||
TableName: PhysicalTableName, | ||
ReplicaUpdates: [ replicateUpdate ] | ||
}).promise() | ||
|
||
do { // Wait to avoid errors with busy tables | ||
await new Promise(r => setTimeout(r, 5000)); | ||
({ Table } = await dynamoPrimary.describeTable({ TableName: PhysicalTableName }).promise()) | ||
} while ( | ||
!Table.Replicas || | ||
Table.Replicas.findIndex((replica) => [ 'CREATING', 'UPDATING', 'DELETING' ].includes(replica.ReplicaStatus)) >= 0 | ||
) | ||
} | ||
} | ||
catch (error) { | ||
update.error(`While replicate table ${tableName} !`) | ||
throw (error) | ||
} | ||
update.done(`Replication updated for table ${tableName}`) | ||
} | ||
else { | ||
update.done(`Skipping replication update for table ${tableName}`) | ||
} | ||
} | ||
catch (err) { | ||
if (err.name === 'ParameterNotFound') { | ||
const message = `${tableName} not found on ${currentRegion}` | ||
if (dryRun) { | ||
update.warn(`${message} (Maybe because is a dry-run)`) | ||
} | ||
else { | ||
update.error(message) | ||
throw (err) | ||
} | ||
} | ||
else if (err.name === 'ResourceNotFoundException') { | ||
const message = `DynamoDB table not found: ${PhysicalTableName}` | ||
if (dryRun) { | ||
update.warn(`${message} (Maybe because is a dry-run)`) | ||
} | ||
else { | ||
update.error(message) | ||
throw (err) | ||
} | ||
} | ||
else { | ||
throw (err) | ||
} | ||
} | ||
} | ||
|
||
done() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters