Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
TimeToLiveDescription,
UpdateContinuousBackupsCommandInput,
ContinuousBackupsDescription,
ContinuousBackupsUnavailableException,
InternalServerError,
} from '@aws-sdk/client-dynamodb';
import {
getNextAtomicUpdate,
Expand All @@ -16,6 +18,7 @@ import {
getDeletionProtectionUpdate,
extractOldTableInputFromEvent,
isTtlModified,
processIsComplete,
} from '../resources/amplify-dynamodb-table/amplify-table-manager-lambda/amplify-table-manager-handler';
import * as ddbTableManagerLambda from '../resources/amplify-dynamodb-table/amplify-table-manager-lambda/amplify-table-manager-handler';
import * as CustomDDB from '../resources/amplify-dynamodb-table/amplify-table-types';
Expand All @@ -29,6 +32,22 @@ jest.spyOn(ddbTableManagerLambda, 'getLambdaTags').mockReturnValue(
{ Key: 'key3', Value: 'value3' },
]),
);

// Mock client-ssm
const mockDescribeTable = jest.fn();
const mockDescribeContinuousBackups = jest.fn();
const mockUpdateContinuousBackups = jest.fn();
jest.mock('@aws-sdk/client-dynamodb', () => {
return {
...jest.requireActual('@aws-sdk/client-dynamodb'),
DynamoDB: jest.fn().mockImplementation(() => ({
describeTable: (input: any) => mockDescribeTable(input),
describeContinuousBackups: (input: any) => mockDescribeContinuousBackups(input),
updateContinuousBackups: (input: any) => mockUpdateContinuousBackups(input),
})),
};
});

describe('Custom Resource Lambda Tests', () => {
describe('Get next GSI update', () => {
const endState: CustomDDB.Input = {
Expand Down Expand Up @@ -1142,4 +1161,215 @@ describe('Custom Resource Lambda Tests', () => {
expect(isTtlModified(oldTtl, newTtl)).toBe(true);
});
});
describe('processIsComplete', () => {
const createEvent = {
RequestType: 'Create',
ServiceToken: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
ResponseURL: '[redacted]',
StackId: 'mockStackId',
RequestId: 'mockRequestId',
LogicalResourceId: 'ResourceTable',
ResourceType: 'Custom::AmplifyDynamoDBTable',
ResourceProperties: {
tableName: 'mockTable',
ServiceToken: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
pointInTimeRecoverySpecification: { pointInTimeRecoveryEnabled: true },
},
PhysicalResourceId: 'mockTable',
Data: {
TableArn: 'arn:aws:dynamodb:ap-northeast-1:123456789100:table/mockTable',
TableStreamArn: 'arn:aws:dynamodb:ap-northeast-1:123456789100:table/mockTable/stream/2025-03-05T01:11:03.258',
TableName: 'mockTable',
},
} as const;

const updateEvent = {
RequestType: 'Update',
ServiceToken: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
ResponseURL: '[redacted]',
StackId: 'mockStackId',
RequestId: 'mockRequestId',
LogicalResourceId: 'ResourceTable',
ResourceType: 'Custom::AmplifyDynamoDBTable',
ResourceProperties: {
tableName: 'mockTable',
ServiceToken: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
pointInTimeRecoverySpecification: { pointInTimeRecoveryEnabled: true },
},
PhysicalResourceId: 'mockTable',
Data: {
TableArn: 'arn:aws:dynamodb:ap-northeast-1:123456789100:table/mockTable',
TableStreamArn: 'arn:aws:dynamodb:ap-northeast-1:123456789100:table/mockTable/stream/2025-03-05T01:11:03.258',
TableName: 'mockTable',
IsTableReplaced: true,
},
} as const;

const deleteEvent = {
RequestType: 'Delete',
ServiceToken: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
ResponseURL: '[redacted]',
StackId: 'mockStackId',
RequestId: 'mockRequestId',
LogicalResourceId: 'ResourceTable',
ResourceType: 'Custom::AmplifyDynamoDBTable',
ResourceProperties: {
tableName: 'mockTable',
ServiceToken: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
},
PhysicalResourceId: 'mockTable',
Data: {
TableArn: 'arn:aws:dynamodb:ap-northeast-1:123456789100:table/mockTable',
TableStreamArn: 'arn:aws:dynamodb:ap-northeast-1:123456789100:table/mockTable/stream/2025-03-05T01:11:03.258',
TableName: 'mockTable',
},
} as const;

beforeEach(() => {
jest.clearAllMocks();
});

it('should return IsComplete false when thrown ContinuousBackupsUnavailableException if requestType is `Create` with PITR enabled', async () => {
mockDescribeTable.mockResolvedValueOnce({
Table: {
TableStatus: 'ACTIVE',
ContinuousBackupsDescription: {
ContinuousBackupsStatus: 'DISABLED',
PointInTimeRecoveryDescription: {
PointInTimeRecoveryStatus: 'DISABLED',
},
},
},
});
mockDescribeContinuousBackups.mockResolvedValueOnce({
ContinuousBackupsDescription: {
ContinuousBackupsStatus: 'DISABLED',
PointInTimeRecoveryDescription: {
PointInTimeRecoveryStatus: 'DISABLED',
},
},
});
mockUpdateContinuousBackups.mockRejectedValueOnce(
new ContinuousBackupsUnavailableException({
message: 'Backups are being enabled for the table: mockTable. Please retry later',
$metadata: {},
}),
);
const { IsComplete } = await processIsComplete(createEvent, {
invokedFunctionArn: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
});
expect(IsComplete).toBe(false);
});
it('should throw error when thrown it other than ContinuousBackupsUnavailableException if requestType is `Create` with PITR enabled', async () => {
mockDescribeTable.mockResolvedValueOnce({
Table: {
TableStatus: 'ACTIVE',
ContinuousBackupsDescription: {
ContinuousBackupsStatus: 'DISABLED',
PointInTimeRecoveryDescription: {
PointInTimeRecoveryStatus: 'DISABLED',
},
},
},
});
mockDescribeContinuousBackups.mockResolvedValueOnce({
ContinuousBackupsDescription: {
ContinuousBackupsStatus: 'DISABLED',
PointInTimeRecoveryDescription: {
PointInTimeRecoveryStatus: 'DISABLED',
},
},
});
mockUpdateContinuousBackups.mockRejectedValueOnce(
new InternalServerError({
message: 'internal server error',
$metadata: {},
}),
);
await expect(
processIsComplete(createEvent, {
invokedFunctionArn: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
}),
).rejects.toThrow(InternalServerError);
});
it('should return IsComplete false when not thrown error if requestType is `Create` with PITR enabled', async () => {
mockDescribeTable.mockResolvedValueOnce({
Table: {
TableStatus: 'ACTIVE',
ContinuousBackupsDescription: {
ContinuousBackupsStatus: 'DISABLED',
PointInTimeRecoveryDescription: {
PointInTimeRecoveryStatus: 'DISABLED',
},
},
},
});
mockDescribeContinuousBackups.mockResolvedValueOnce({
ContinuousBackupsDescription: {
ContinuousBackupsStatus: 'DISABLED',
PointInTimeRecoveryDescription: {
PointInTimeRecoveryStatus: 'DISABLED',
},
},
});
const { IsComplete } = await processIsComplete(createEvent, {
invokedFunctionArn: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
});
expect(IsComplete).toBe(false);
});
it('should return IsComplete false when thrown ContinuousBackupsUnavailableException if replace table with PITR enabling', async () => {
mockDescribeTable.mockResolvedValueOnce({
Table: {
TableStatus: 'ACTIVE',
ContinuousBackupsDescription: {
ContinuousBackupsStatus: 'DISABLED',
PointInTimeRecoveryDescription: {
PointInTimeRecoveryStatus: 'DISABLED',
},
},
},
});
mockDescribeContinuousBackups.mockResolvedValueOnce({
ContinuousBackupsDescription: {
ContinuousBackupsStatus: 'DISABLED',
PointInTimeRecoveryDescription: {
PointInTimeRecoveryStatus: 'DISABLED',
},
},
});
mockUpdateContinuousBackups.mockRejectedValueOnce(
new ContinuousBackupsUnavailableException({
message: 'Backups are being enabled for the table: mockTable. Please retry later',
$metadata: {},
}),
);
const { IsComplete } = await processIsComplete(updateEvent, {
invokedFunctionArn: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
});
expect(IsComplete).toBe(false);
});
it('should return IsComplete false when table is not acitve', async () => {
mockDescribeTable.mockResolvedValueOnce({
Table: {
TableStatus: 'CREATING',
ContinuousBackupsDescription: {
ContinuousBackupsStatus: 'DISABLED',
PointInTimeRecoveryDescription: {
PointInTimeRecoveryStatus: 'DISABLED',
},
},
},
});
const { IsComplete } = await processIsComplete(createEvent, {
invokedFunctionArn: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
});
expect(IsComplete).toBe(false);
});
it('should return IsComplete true if requestType is `Delete`', async () => {
const { IsComplete } = await processIsComplete(deleteEvent, {
invokedFunctionArn: 'arn:aws:lambda:ap-northeast-1:123456789100:function:TableManagerCustomProviderframeworkisComplete',
});
expect(IsComplete).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {
AttributeDefinition,
ContinuousBackupsDescription,
ContinuousBackupsUnavailableException,
CreateGlobalSecondaryIndexAction,
CreateTableCommandInput,
DescribeTimeToLiveCommandOutput,
Expand Down Expand Up @@ -390,7 +391,7 @@ const processOnEvent = async (
* @param event CFN event
* @returns Response object with `isComplete` bool attribute to indicate the completeness of process
*/
const processIsComplete = async (
export const processIsComplete = async (
event: AWSCDKAsyncCustomResource.IsCompleteRequest,
context: TableManagerContext,
): Promise<AWSCDKAsyncCustomResource.IsCompleteResponse> => {
Expand Down Expand Up @@ -426,7 +427,16 @@ const processIsComplete = async (
const pointInTimeUpdate = getPointInTimeRecoveryUpdate(describePointInTimeRecoveryResult.ContinuousBackupsDescription, endState);
if (pointInTimeUpdate) {
console.log('Updating table with point in time recovery enabled');
await ddbClient.updateContinuousBackups(pointInTimeUpdate);
try {
await ddbClient.updateContinuousBackups(pointInTimeUpdate);
} catch (error) {
if (error instanceof ContinuousBackupsUnavailableException) {
console.log('Backups are being enabled for the table');
return notFinished;
} else {
throw error;
}
}
return notFinished;
}
// Need additional call if ttl is defined
Expand Down
Loading