-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk): add deletion protection to production ddb tables
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 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,125 @@ | ||
import { DynamoDB } from 'aws-sdk'; | ||
import { Key, WriteRequest } from 'aws-sdk/clients/dynamodb'; | ||
|
||
interface CopyProps { | ||
sourceTableName: string; | ||
sourceDdbClient: DynamoDB.DocumentClient; | ||
sourceDdb: DynamoDB; | ||
destinationTableName: string; | ||
destinationDdbClient: DynamoDB.DocumentClient; | ||
destinationDdb: DynamoDB; | ||
} | ||
|
||
async function main() { | ||
const config: DynamoDB.ClientConfiguration = { | ||
region: 'us-west-1', | ||
accessKeyId: '', | ||
secretAccessKey: '', | ||
}; | ||
|
||
const sourceTableName = 'prod-us-west-1-Backend-antalmanacuserdataddbprodE407436D-Q87Q74FD29CS'; | ||
const sourceDdbClient = new DynamoDB.DocumentClient(config); | ||
const sourceDdb = new DynamoDB(config); | ||
|
||
sourceDdb.describeTable( | ||
{ | ||
TableName: sourceTableName, | ||
}, | ||
(err, data) => { | ||
if (err) { | ||
console.error(err); | ||
} else { | ||
console.log(data); | ||
} | ||
} | ||
); | ||
|
||
const destinationTableName = 'antalmanac-backend-production-userdataddbAB232634-1OXTJU2H7VZWT'; | ||
const destinationDdbClient = new DynamoDB.DocumentClient(config); | ||
const destinationDdb = new DynamoDB(config); | ||
|
||
destinationDdb.describeTable( | ||
{ | ||
TableName: destinationTableName, | ||
}, | ||
(err, data) => { | ||
if (err) { | ||
console.error(err); | ||
} else { | ||
console.log(data); | ||
} | ||
} | ||
); | ||
|
||
const props: CopyProps = { | ||
sourceTableName, | ||
sourceDdbClient, | ||
sourceDdb, | ||
destinationTableName, | ||
destinationDdbClient, | ||
destinationDdb, | ||
}; | ||
|
||
copyStartingFrom(props); | ||
} | ||
|
||
function copyStartingFrom(props: CopyProps, ExclusiveStartKey?: Key) { | ||
const { sourceTableName, sourceDdbClient, destinationTableName, destinationDdbClient } = props; | ||
|
||
console.log(`Starting scan... from :${ExclusiveStartKey}`); | ||
|
||
sourceDdbClient.scan( | ||
{ | ||
TableName: sourceTableName, | ||
Limit: 25, | ||
ExclusiveStartKey, | ||
}, | ||
(err, data) => { | ||
console.log('Completed scan!'); | ||
|
||
console.log({ err }); | ||
console.log({ data }); | ||
|
||
const { LastEvaluatedKey } = data; | ||
|
||
const items = data.Items?.map((Item): WriteRequest => { | ||
return { | ||
PutRequest: { | ||
Item, | ||
}, | ||
}; | ||
}); | ||
|
||
if (items == null || LastEvaluatedKey == null) { | ||
console.log('No more items to copy!'); | ||
return; | ||
} | ||
|
||
console.log('Copying items...'); | ||
|
||
destinationDdbClient.batchWrite( | ||
{ | ||
RequestItems: { | ||
[destinationTableName]: items, | ||
}, | ||
}, | ||
(err, data) => { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
|
||
console.log({ data }); | ||
console.log('Copied items!'); | ||
console.log('Waiting until next copy...'); | ||
|
||
setTimeout(() => { | ||
copyStartingFrom(props, LastEvaluatedKey); | ||
}, 200); | ||
} | ||
); | ||
} | ||
); | ||
} | ||
|
||
main(); |
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