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

wip Function Factory #9

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
node_modules

# testing
/coverage
Expand All @@ -22,4 +22,3 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

7 changes: 4 additions & 3 deletions __mocks__/aws-sdk.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/**
* Mocking the AWS SDK version 2
* @see https://dev.to/elthrasher/mocking-aws-with-jest-and-typescript-199i
* Uses `requireActual` because SST uses `SharedIniFileCredentials`
* Uses `importActual` because SST uses `SharedIniFileCredentials`
*
* For mocking AWS SDK version 3, use https://m-radzikowski.github.io/aws-sdk-client-mock/
*/

import DynamoDB from './aws-sdk/clients/dynamodb';
import S3 from './aws-sdk/clients/s3';
import { vi } from 'vitest';

const SharedIniFileCredentials =
jest.requireActual('aws-sdk').SharedIniFileCredentials;
const SharedIniFileCredentials = ((await vi.importActual('aws-sdk')) as any)
.SharedIniFileCredentials;

export { DynamoDB, S3, SharedIniFileCredentials };
3 changes: 2 additions & 1 deletion __mocks__/aws-sdk/clients/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* Only used methods are mocked.
* @see https://dev.to/elthrasher/mocking-aws-with-jest-and-typescript-199i
*/
import { vi } from 'vitest';

import { awsSdkV2PromiseResponse } from '../../awsSdkV2PromiseResponse';

export const batchWriteFn = jest
export const batchWriteFn = vi
.fn()
.mockImplementation(() => ({ promise: awsSdkV2PromiseResponse }));

Expand Down
3 changes: 2 additions & 1 deletion __mocks__/aws-sdk/clients/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* Only used methods are mocked.
* @see https://dev.to/elthrasher/mocking-aws-with-jest-and-typescript-199i
*/
import { vi } from 'vitest';

import { awsSdkV2PromiseResponse } from '../../awsSdkV2PromiseResponse';

export const getObjectFn = jest.fn().mockImplementation(() => ({
export const getObjectFn = vi.fn().mockImplementation(() => ({
createReadStream: awsSdkV2PromiseResponse,
}));

Expand Down
3 changes: 2 additions & 1 deletion __mocks__/awsSdkV2PromiseResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* For use in mocking AWS SDK v2.
* @see https://dev.to/elthrasher/mocking-aws-with-jest-and-typescript-199i
*/
import { vi } from 'vitest';

export const awsSdkV2PromiseResponse = jest
export const awsSdkV2PromiseResponse = vi
.fn()
.mockReturnValue(Promise.resolve(true));
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"matt-community-benchmarks-BenchmarkMachineStack": {
"BenchmarkStateMachineBenchmarkUrlC4F2BF06": "https://a014r3k9o7.execute-api.us-east-1.amazonaws.com"
}
}
6 changes: 4 additions & 2 deletions constructs/BenchmarkStateMachine.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { App, Stack } from '@serverless-stack/resources';
import { Template } from 'aws-cdk-lib/assertions';
import { expect, test } from 'vitest';

import lambdaTests from '../fixtures/lambda-tests';
import { BenchmarkStateMachine } from './BenchmarkStateMachine';

Expand All @@ -10,7 +12,7 @@ test('BenchmarkStateMachine', () => {
lambdaTests,
});
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::Lambda::Function', 1);
template.resourceCountIs('AWS::StepFunctions::StateMachine', 4);
template.resourceCountIs('AWS::Lambda::Function', 3);
template.resourceCountIs('AWS::StepFunctions::StateMachine', 10);
expect(template.toJSON()).toMatchSnapshot();
});
50 changes: 39 additions & 11 deletions constructs/BenchmarkStateMachine.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Function, Table, TableFieldType } from '@serverless-stack/resources';
import { Duration, RemovalPolicy } from 'aws-cdk-lib';
import { Api, Function, Table } from '@serverless-stack/resources';
import { CfnOutput, Duration, RemovalPolicy } from 'aws-cdk-lib';
import { BillingMode } from 'aws-cdk-lib/aws-dynamodb';
import { PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Tracing } from 'aws-cdk-lib/aws-lambda';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import {
IntegrationPattern,
JsonPath,
Expand All @@ -26,12 +26,14 @@ export class BenchmarkStateMachine extends Construct {
const stateMachineName = 'benchmark-machine';

const table = new Table(scope, 'BenchmarksTable', {
dynamodbTable: {
billingMode: BillingMode.PAY_PER_REQUEST,
removalPolicy: RemovalPolicy.DESTROY,
tableName: 'Benchmarks',
cdk: {
table: {
billingMode: BillingMode.PAY_PER_REQUEST,
removalPolicy: RemovalPolicy.DESTROY,
tableName: 'Benchmarks',
},
},
fields: { pk: TableFieldType.STRING, sk: TableFieldType.STRING },
fields: { pk: 'string', sk: 'string' },
primaryIndex: { partitionKey: 'pk', sortKey: 'sk' },
});

Expand All @@ -45,15 +47,22 @@ export class BenchmarkStateMachine extends Construct {
resources: ['*'],
}),
],
logRetention: RetentionDays.ONE_WEEK,
srcPath: 'src/benchmark',
tracing: Tracing.ACTIVE,
});

// // Get data from the table
// const getBenchmarks = new Function(scope, 'GetBenchmarks', {
// functionName: 'get-benchmarks',
// handler: 'get-benchmarks.handler',
// srcPath: 'src/benchmark',
// });

// Step Functions parallel step to fan out to nested Sfn executions.
const parallel = new Parallel(scope, 'Parallel Execution 0');
let p = parallel;

const MAX_PARALLEL = 10;
const MAX_PARALLEL = 8;

// Loop over functions to test. We will create a new nested state machine for each function.
props.lambdaTests.forEach((lambdaTest, index) => {
Expand All @@ -73,7 +82,7 @@ export class BenchmarkStateMachine extends Construct {
}
);

// sfExecution.addRetry({ maxAttempts: 2 });
sfExecution.addRetry({ maxAttempts: 2 });

if (index && index % MAX_PARALLEL === 0) {
const np = new Parallel(
Expand All @@ -92,5 +101,24 @@ export class BenchmarkStateMachine extends Construct {
stateMachineName,
timeout: Duration.minutes(10),
});

const api = new Api(this, 'BenchmarksApi', {
cors: true,
defaults: {
function: {
environment: { TABLE_NAME: table.tableName },
logRetention: RetentionDays.ONE_WEEK,
runtime: 'nodejs16.x',
},
},
routes: {
'GET /benchmarks': 'src/benchmark/get-benchmarks.handler',
},
});
api.attachPermissions([table]);

new CfnOutput(this, 'BenchmarkUrl', {
value: api.cdk.httpApi.apiEndpoint,
});
}
}
Loading