Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ddb deletion protection on imported tables #3158

Open
wants to merge 3 commits into
base: main
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
18 changes: 15 additions & 3 deletions packages/amplify-graphql-api-construct-tests/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
sleep,
updateApiSchema,
} from 'amplify-category-api-e2e-core';
import { DynamoDBClient, DeleteTableCommand, ListTablesCommand } from '@aws-sdk/client-dynamodb';
import { DynamoDBClient, DeleteTableCommand, ListTablesCommand, UpdateTableCommand } from '@aws-sdk/client-dynamodb';

/**
* Retrieve the path to the `npx` executable for interacting with the aws-cdk cli.
Expand Down Expand Up @@ -215,6 +215,7 @@ export const createGen1ProjectForMigration = async (

// TODO: GEN1_GEN2_MIGRATION
// Construct the DataSourceMappingOutput with the AWS SDK
// Set table deletion protection to true for all tables
// Remove this block when the feature flag is released
// Start block
const client = new DynamoDBClient({ region: process.env.CLI_REGION || 'us-west-2' });
Expand All @@ -226,12 +227,19 @@ export const createGen1ProjectForMigration = async (
ExclusiveStartTableName = response.LastEvaluatedTableName;
tables.push(...response.TableNames);
} while (ExclusiveStartTableName);
const tableNameMapping = tables
const tablesForApi = tables
// filter all tables by the API ID
.filter((tableName) => tableName.includes(GraphQLAPIIdOutput))
.filter((tableName) => tableName.includes(GraphQLAPIIdOutput));

const tableNameMapping = tablesForApi
// extract the model name from the table name and create the mapping
.map((tableName) => [tableName.match(/(^.*?)-/)[1], tableName]);
const DataSourceMappingOutput = JSON.stringify(Object.fromEntries(tableNameMapping));

// set deletion protection to true for all tables
await Promise.allSettled(
tablesForApi.map((tableName) => client.send(new UpdateTableCommand({ TableName: tableName, DeletionProtectionEnabled: true }))),
);
// End block

return {
Expand All @@ -248,5 +256,9 @@ export const createGen1ProjectForMigration = async (
*/
export const deleteDDBTables = async (tableNames: string[]): Promise<void> => {
const client = new DynamoDBClient({ region: process.env.CLI_REGION || 'us-west-2' });
// disable deletion protection before deleting the tables
await Promise.allSettled(
tableNames.map((tableName) => client.send(new UpdateTableCommand({ TableName: tableName, DeletionProtectionEnabled: false }))),
);
await Promise.allSettled(tableNames.map((tableName) => client.send(new DeleteTableCommand({ TableName: tableName }))));
};
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('ModelTransformer:', () => {
expect(authorTable.DeletionPolicy).toBe('Retain');
expect(authorTable.Properties.isImported).toBe(true);
expect(authorTable.Properties.tableName).toBe('Author-myApiId-myEnv');
expect(authorTable.Properties.deletionProtectionEnabled).toBe(true);
validateModelSchema(parse(out.schema));

// Outputs should contain a reference to the Arn to the entry point (onEventHandler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export class AmplifyDynamoModelResourceGenerator extends DynamoModelResourceGene
stream: StreamViewType.NEW_AND_OLD_IMAGES,
encryption: TableEncryption.DEFAULT,
removalPolicy,
deletionProtection: isTableImported,
...(context.isProjectUsingDataStore() ? { timeToLiveAttribute: '_ttl' } : undefined),
...(isTableImported ? { isImported: true } : undefined),
});
Expand Down
Loading