Skip to content

Commit bc9b1d4

Browse files
authored
refactor(parameters): move table seeding into AwsCustomResource (#1317)
* chore: add run e2e * Revert "chore: add run e2e" This reverts commit 6ba8d61. * tests: refactored table seeding * chore: fix linting
1 parent 6294f94 commit bc9b1d4

File tree

2 files changed

+108
-60
lines changed

2 files changed

+108
-60
lines changed

Diff for: packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts

+75-59
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import path from 'path';
77
import { AttributeType } from 'aws-cdk-lib/aws-dynamodb';
88
import { App, Stack, Aspects } from 'aws-cdk-lib';
9-
import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb';
10-
import { marshall } from '@aws-sdk/util-dynamodb';
119
import { v4 } from 'uuid';
1210
import {
1311
generateUniqueName,
@@ -24,7 +22,7 @@ import {
2422
TEARDOWN_TIMEOUT,
2523
TEST_CASE_TIMEOUT
2624
} from './constants';
27-
import { createDynamoDBTable } from '../helpers/parametersUtils';
25+
import { createDynamoDBTable, putDynamoDBItem } from '../helpers/parametersUtils';
2826

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

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

40-
const dynamoDBClient = new DynamoDBClient({});
41-
4238
const invocationCount = 1;
4339

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

212-
// Deploy the stack
213-
await deployStack(integTestApp, stack);
214-
215208
// Seed tables with test data
216209
// Test 1
217-
await dynamoDBClient.send(new PutItemCommand({
218-
TableName: tableGet,
219-
Item: marshall({
210+
putDynamoDBItem({
211+
stack,
212+
id: 'my-param-test1',
213+
table: ddbTableGet,
214+
item: {
220215
id: 'my-param',
221216
value: 'foo',
222-
}),
223-
}));
217+
},
218+
});
224219

225220
// Test 2
226-
await dynamoDBClient.send(new PutItemCommand({
227-
TableName: tableGetMultiple,
228-
Item: marshall({
221+
putDynamoDBItem({
222+
stack,
223+
id: 'my-param-test2-a',
224+
table: ddbTableGetMultiple,
225+
item: {
229226
id: 'my-params',
230227
sk: 'config',
231228
value: 'bar',
232-
}),
233-
}));
234-
await dynamoDBClient.send(new PutItemCommand({
235-
TableName: tableGetMultiple,
236-
Item: marshall({
229+
},
230+
});
231+
putDynamoDBItem({
232+
stack,
233+
id: 'my-param-test2-b',
234+
table: ddbTableGetMultiple,
235+
item: {
237236
id: 'my-params',
238237
sk: 'key',
239238
value: 'baz',
240-
}),
241-
}));
239+
},
240+
});
242241

243242
// Test 3
244-
await dynamoDBClient.send(new PutItemCommand({
245-
TableName: tableGetCustomkeys,
246-
Item: marshall({
243+
putDynamoDBItem({
244+
stack,
245+
id: 'my-param-test3',
246+
table: ddbTableGetCustomKeys,
247+
item: {
247248
[keyAttr]: 'my-param',
248249
[valueAttr]: 'foo',
249-
}),
250-
}));
250+
},
251+
});
251252

252253
// Test 4
253-
await dynamoDBClient.send(new PutItemCommand({
254-
TableName: tableGetMultipleCustomkeys,
255-
Item: marshall({
254+
putDynamoDBItem({
255+
stack,
256+
id: 'my-param-test4-a',
257+
table: ddbTabelGetMultipleCustomKeys,
258+
item: {
256259
[keyAttr]: 'my-params',
257260
[sortAttr]: 'config',
258261
[valueAttr]: 'bar',
259-
}),
260-
}));
261-
await dynamoDBClient.send(new PutItemCommand({
262-
TableName: tableGetMultipleCustomkeys,
263-
Item: marshall({
262+
},
263+
});
264+
putDynamoDBItem({
265+
stack,
266+
id: 'my-param-test4-b',
267+
table: ddbTabelGetMultipleCustomKeys,
268+
item: {
264269
[keyAttr]: 'my-params',
265270
[sortAttr]: 'key',
266271
[valueAttr]: 'baz',
267-
}),
268-
}));
272+
},
273+
});
269274

270275
// Test 5
271-
await dynamoDBClient.send(new PutItemCommand({
272-
TableName: tableGet,
273-
Item: marshall({
276+
putDynamoDBItem({
277+
stack,
278+
id: 'my-param-test5',
279+
table: ddbTableGet,
280+
item: {
274281
id: 'my-param-json',
275282
value: JSON.stringify({ foo: 'bar' }),
276-
}),
277-
}));
283+
},
284+
});
278285

279286
// Test 6
280-
await dynamoDBClient.send(new PutItemCommand({
281-
TableName: tableGet,
282-
Item: marshall({
287+
putDynamoDBItem({
288+
stack,
289+
id: 'my-param-test6',
290+
table: ddbTableGet,
291+
item: {
283292
id: 'my-param-binary',
284293
value: 'YmF6', // base64 encoded 'baz'
285-
}),
286-
}));
287-
294+
},
295+
});
296+
288297
// Test 7
289-
await dynamoDBClient.send(new PutItemCommand({
290-
TableName: tableGetMultiple,
291-
Item: marshall({
298+
putDynamoDBItem({
299+
stack,
300+
id: 'my-param-test7-a',
301+
table: ddbTableGetMultiple,
302+
item: {
292303
id: 'my-encoded-params',
293304
sk: 'config.json',
294305
value: JSON.stringify({ foo: 'bar' }),
295-
}),
296-
}));
297-
await dynamoDBClient.send(new PutItemCommand({
298-
TableName: tableGetMultiple,
299-
Item: marshall({
306+
},
307+
});
308+
putDynamoDBItem({
309+
stack,
310+
id: 'my-param-test7-b',
311+
table: ddbTableGetMultiple,
312+
item: {
300313
id: 'my-encoded-params',
301314
sk: 'key.binary',
302315
value: 'YmF6', // base64 encoded 'baz'
303-
}),
304-
}));
316+
},
317+
});
305318

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

321+
// Deploy the stack
322+
await deployStack(integTestApp, stack);
323+
308324
// and invoke the Lambda function
309325
invocationLogs = await invokeFunction(functionName, invocationCount, 'SEQUENTIAL');
310326

Diff for: packages/parameters/tests/helpers/parametersUtils.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Stack, RemovalPolicy, CustomResource, Duration } from 'aws-cdk-lib';
2-
import { Provider } from 'aws-cdk-lib/custom-resources';
2+
import { PhysicalResourceId, Provider } from 'aws-cdk-lib/custom-resources';
33
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
44
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
55
import { Runtime } from 'aws-cdk-lib/aws-lambda';
@@ -14,6 +14,11 @@ import {
1414
CfnEnvironment,
1515
CfnHostedConfigurationVersion,
1616
} from 'aws-cdk-lib/aws-appconfig';
17+
import {
18+
AwsCustomResource,
19+
AwsCustomResourcePolicy
20+
} from 'aws-cdk-lib/custom-resources';
21+
import { marshall } from '@aws-sdk/util-dynamodb';
1722

1823
export type CreateDynamoDBTableOptions = {
1924
stack: Stack
@@ -201,10 +206,37 @@ const createSSMSecureString = (options: CreateSSMSecureStringOptions): IStringPa
201206
return param;
202207
};
203208

209+
export type PutDynamoDBItemOptions = {
210+
stack: Stack
211+
id: string
212+
table: Table
213+
item: Record<string, unknown>
214+
};
215+
216+
const putDynamoDBItem = async (options: PutDynamoDBItemOptions): Promise<void> => {
217+
const { stack, id, table, item } = options;
218+
219+
new AwsCustomResource(stack, id, {
220+
onCreate: {
221+
service: 'DynamoDB',
222+
action: 'putItem',
223+
parameters: {
224+
TableName: table.tableName,
225+
Item: marshall(item),
226+
},
227+
physicalResourceId: PhysicalResourceId.of(id),
228+
},
229+
policy: AwsCustomResourcePolicy.fromSdkCalls({
230+
resources: [table.tableArn],
231+
}),
232+
});
233+
};
234+
204235
export {
205236
createDynamoDBTable,
206237
createBaseAppConfigResources,
207238
createAppConfigConfigurationProfile,
208239
createSSMSecureString,
209240
createSecureStringProvider,
241+
putDynamoDBItem,
210242
};

0 commit comments

Comments
 (0)