Skip to content

Commit

Permalink
feat(cdk): add deletion protection to production ddb tables
Browse files Browse the repository at this point in the history
  • Loading branch information
ap0nia committed Feb 4, 2024
1 parent 19e8e33 commit b78e427
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
125 changes: 125 additions & 0 deletions apps/antalmanac/tools/copy_ddb.ts
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();
2 changes: 2 additions & 0 deletions apps/cdk/src/stacks/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class BackendStack extends Stack {
},
billingMode: dynamnodb.BillingMode.PAY_PER_REQUEST,
removalPolicy,
deletionProtection: true,
});

const authUserDataDDB = new dynamnodb.Table(this, `authuserdata-ddb`, {
Expand All @@ -53,6 +54,7 @@ export class BackendStack extends Stack {
},
billingMode: dynamnodb.BillingMode.PAY_PER_REQUEST,
removalPolicy,
deletionProtection: true,
});

const handler = new lambda.Function(this, 'lambda', {
Expand Down

0 comments on commit b78e427

Please sign in to comment.