diff --git a/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts b/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts index ec468cd2b5..62e8bd6c39 100644 --- a/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts +++ b/packages/parameters/tests/e2e/dynamoDBProvider.class.test.ts @@ -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, @@ -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'; @@ -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 @@ -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'); diff --git a/packages/parameters/tests/helpers/parametersUtils.ts b/packages/parameters/tests/helpers/parametersUtils.ts index 3bd785bbed..3e769f9d3e 100644 --- a/packages/parameters/tests/helpers/parametersUtils.ts +++ b/packages/parameters/tests/helpers/parametersUtils.ts @@ -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'; @@ -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 @@ -201,10 +206,37 @@ const createSSMSecureString = (options: CreateSSMSecureStringOptions): IStringPa return param; }; +export type PutDynamoDBItemOptions = { + stack: Stack + id: string + table: Table + item: Record +}; + +const putDynamoDBItem = async (options: PutDynamoDBItemOptions): Promise => { + 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, };