Skip to content

refactor(parameters): move table seeding into AwsCustomResource #1317

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

Merged
merged 4 commits into from
Feb 21, 2023
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
134 changes: 75 additions & 59 deletions packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import path from 'path';
import { AttributeType } from 'aws-cdk-lib/aws-dynamodb';
import { App, Stack, Aspects } from 'aws-cdk-lib';
import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb';
import { marshall } from '@aws-sdk/util-dynamodb';
import { v4 } from 'uuid';
import {
generateUniqueName,
Expand All @@ -24,7 +22,7 @@ import {
TEARDOWN_TIMEOUT,
TEST_CASE_TIMEOUT
} from './constants';
import { createDynamoDBTable } from '../helpers/parametersUtils';
import { createDynamoDBTable, putDynamoDBItem } from '../helpers/parametersUtils';

const runtime: string = process.env.RUNTIME || 'nodejs18x';

Expand All @@ -37,8 +35,6 @@ const stackName = generateUniqueName(RESOURCE_NAME_PREFIX, uuid, runtime, 'dynam
const functionName = generateUniqueName(RESOURCE_NAME_PREFIX, uuid, runtime, 'dynamoDBProvider');
const lambdaFunctionCodeFile = 'dynamoDBProvider.class.test.functionCode.ts';

const dynamoDBClient = new DynamoDBClient({});

const invocationCount = 1;

// Parameters to be used by Parameters in the Lambda function
Expand Down Expand Up @@ -209,102 +205,122 @@ describe(`parameters E2E tests (dynamoDBProvider) for runtime: ${runtime}`, () =
ddbTabelGetMultipleCustomKeys,
]));

// Deploy the stack
await deployStack(integTestApp, stack);

// Seed tables with test data
// Test 1
await dynamoDBClient.send(new PutItemCommand({
TableName: tableGet,
Item: marshall({
putDynamoDBItem({
stack,
id: 'my-param-test1',
table: ddbTableGet,
item: {
id: 'my-param',
value: 'foo',
}),
}));
},
});

// Test 2
await dynamoDBClient.send(new PutItemCommand({
TableName: tableGetMultiple,
Item: marshall({
putDynamoDBItem({
stack,
id: 'my-param-test2-a',
table: ddbTableGetMultiple,
item: {
id: 'my-params',
sk: 'config',
value: 'bar',
}),
}));
await dynamoDBClient.send(new PutItemCommand({
TableName: tableGetMultiple,
Item: marshall({
},
});
putDynamoDBItem({
stack,
id: 'my-param-test2-b',
table: ddbTableGetMultiple,
item: {
id: 'my-params',
sk: 'key',
value: 'baz',
}),
}));
},
});

// Test 3
await dynamoDBClient.send(new PutItemCommand({
TableName: tableGetCustomkeys,
Item: marshall({
putDynamoDBItem({
stack,
id: 'my-param-test3',
table: ddbTableGetCustomKeys,
item: {
[keyAttr]: 'my-param',
[valueAttr]: 'foo',
}),
}));
},
});

// Test 4
await dynamoDBClient.send(new PutItemCommand({
TableName: tableGetMultipleCustomkeys,
Item: marshall({
putDynamoDBItem({
stack,
id: 'my-param-test4-a',
table: ddbTabelGetMultipleCustomKeys,
item: {
[keyAttr]: 'my-params',
[sortAttr]: 'config',
[valueAttr]: 'bar',
}),
}));
await dynamoDBClient.send(new PutItemCommand({
TableName: tableGetMultipleCustomkeys,
Item: marshall({
},
});
putDynamoDBItem({
stack,
id: 'my-param-test4-b',
table: ddbTabelGetMultipleCustomKeys,
item: {
[keyAttr]: 'my-params',
[sortAttr]: 'key',
[valueAttr]: 'baz',
}),
}));
},
});

// Test 5
await dynamoDBClient.send(new PutItemCommand({
TableName: tableGet,
Item: marshall({
putDynamoDBItem({
stack,
id: 'my-param-test5',
table: ddbTableGet,
item: {
id: 'my-param-json',
value: JSON.stringify({ foo: 'bar' }),
}),
}));
},
});

// Test 6
await dynamoDBClient.send(new PutItemCommand({
TableName: tableGet,
Item: marshall({
putDynamoDBItem({
stack,
id: 'my-param-test6',
table: ddbTableGet,
item: {
id: 'my-param-binary',
value: 'YmF6', // base64 encoded 'baz'
}),
}));

},
});
// Test 7
await dynamoDBClient.send(new PutItemCommand({
TableName: tableGetMultiple,
Item: marshall({
putDynamoDBItem({
stack,
id: 'my-param-test7-a',
table: ddbTableGetMultiple,
item: {
id: 'my-encoded-params',
sk: 'config.json',
value: JSON.stringify({ foo: 'bar' }),
}),
}));
await dynamoDBClient.send(new PutItemCommand({
TableName: tableGetMultiple,
Item: marshall({
},
});
putDynamoDBItem({
stack,
id: 'my-param-test7-b',
table: ddbTableGetMultiple,
item: {
id: 'my-encoded-params',
sk: 'key.binary',
value: 'YmF6', // base64 encoded 'baz'
}),
}));
},
});

// Test 8 & 9 use the same items as Test 1

// Deploy the stack
await deployStack(integTestApp, stack);

// and invoke the Lambda function
invocationLogs = await invokeFunction(functionName, invocationCount, 'SEQUENTIAL');

Expand Down
34 changes: 33 additions & 1 deletion packages/parameters/tests/helpers/parametersUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Stack, RemovalPolicy, CustomResource, Duration } from 'aws-cdk-lib';
import { Provider } from 'aws-cdk-lib/custom-resources';
import { PhysicalResourceId, Provider } from 'aws-cdk-lib/custom-resources';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
Expand All @@ -14,6 +14,11 @@ import {
CfnEnvironment,
CfnHostedConfigurationVersion,
} from 'aws-cdk-lib/aws-appconfig';
import {
AwsCustomResource,
AwsCustomResourcePolicy
} from 'aws-cdk-lib/custom-resources';
import { marshall } from '@aws-sdk/util-dynamodb';

export type CreateDynamoDBTableOptions = {
stack: Stack
Expand Down Expand Up @@ -201,10 +206,37 @@ const createSSMSecureString = (options: CreateSSMSecureStringOptions): IStringPa
return param;
};

export type PutDynamoDBItemOptions = {
stack: Stack
id: string
table: Table
item: Record<string, unknown>
};

const putDynamoDBItem = async (options: PutDynamoDBItemOptions): Promise<void> => {
const { stack, id, table, item } = options;

new AwsCustomResource(stack, id, {
onCreate: {
service: 'DynamoDB',
action: 'putItem',
parameters: {
TableName: table.tableName,
Item: marshall(item),
},
physicalResourceId: PhysicalResourceId.of(id),
},
policy: AwsCustomResourcePolicy.fromSdkCalls({
resources: [table.tableArn],
}),
});
};

export {
createDynamoDBTable,
createBaseAppConfigResources,
createAppConfigConfigurationProfile,
createSSMSecureString,
createSecureStringProvider,
putDynamoDBItem,
};