Skip to content

Commit e0f251b

Browse files
author
Dinesh Sajwan
committed
feat(baseclass): assded base class for tracking
1 parent a45fc08 commit e0f251b

File tree

3 files changed

+147
-38
lines changed

3 files changed

+147
-38
lines changed

src/common/base-class/base-class.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
5+
* with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
10+
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
11+
* and limitations under the License.
12+
*/
13+
import { Construct } from 'constructs';
14+
import { version } from '../helpers/utils';
15+
import * as lambda from 'aws-cdk-lib/aws-lambda';
16+
import { Stack } from 'aws-cdk-lib';
17+
18+
export interface BaseClassProps {
19+
/**
20+
* Value will be appended to resources name.
21+
*
22+
* @default - _dev
23+
*/
24+
readonly stage?: string;
25+
26+
27+
/**
28+
* Optional.CDK constructs provided collects anonymous operational
29+
* metrics to help AWS improve the quality and features of the
30+
* constructs. Data collection is subject to the AWS Privacy Policy
31+
* (https://aws.amazon.com/privacy/). To opt out of this feature,
32+
* simply disable it by setting the construct property
33+
* "enableOperationalMetric" to false for each construct used.
34+
*
35+
* @default - true
36+
*/
37+
readonly enableOperationalMetric?: boolean;
38+
39+
lambdaFunctions: lambda.DockerImageFunction[]
40+
constructorName: string
41+
constructId: string
42+
}
43+
44+
export class BaseClass extends Construct {
45+
46+
readonly CONSTRUCT_TRACKING_CODE = 'uksb-1tupboc45';
47+
48+
//static readonly solutionId = `genai_cdk_${version}/${this.constructor.name}/${id}`;
49+
50+
stage!: string;
51+
52+
constructor(scope: Construct, id: string) {
53+
super(scope, id)
54+
55+
}
56+
57+
public updateEnvSuffix(props: BaseClassProps){
58+
let stage = '-dev';
59+
if (props?.stage) {
60+
stage = props.stage;
61+
}
62+
this.stage = stage;
63+
}
64+
65+
public updateConstructTrackingCode(props: BaseClassProps,scope: Construct){
66+
const enableOperationalMetric =
67+
props.enableOperationalMetric !== undefined &&
68+
props.enableOperationalMetric !== null ? props.enableOperationalMetric : true;
69+
70+
if (enableOperationalMetric) {
71+
72+
// iterate props.lambdaFunction array
73+
const solutionId = `genai_cdk_${version}/${props.constructorName}/${props.constructId}`;
74+
for(let lambdaFunction of props.lambdaFunctions)
75+
{
76+
lambdaFunction.addEnvironment(
77+
'AWS_SDK_UA_APP_ID', solutionId,
78+
);
79+
}
80+
// ADD unique key in CFN stack
81+
Stack.of(scope).templateOptions.description =solutionId
82+
83+
};
84+
}
85+
86+
}

src/common/base-class/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
5+
* with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
10+
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
11+
* and limitations under the License.
12+
*/
13+
export * from './base-class';

src/patterns/gen-ai/aws-summarization-appsync-stepfn/index.ts

+48-38
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ import * as eventBridge from '../../../common/helpers/eventbridge-helper';
3131
import { buildDockerLambdaFunction } from '../../../common/helpers/lambda-builder-helper';
3232
import * as redisHelper from '../../../common/helpers/redis-helper';
3333
import * as s3BucketHelper from '../../../common/helpers/s3-bucket-helper';
34-
import { generatePhysicalName, version, lambdaMemorySizeLimiter } from '../../../common/helpers/utils';
34+
import { generatePhysicalName, lambdaMemorySizeLimiter } from '../../../common/helpers/utils';
3535
import * as vpcHelper from '../../../common/helpers/vpc-helper';
3636
import { DockerLambdaCustomProps } from '../../../common/props/DockerLambdaCustomProps';
37+
import { BaseClass,BaseClassProps } from '../../../common/base-class/base-class';
3738

3839
export interface SummarizationAppsyncStepfnProps {
3940
/**
@@ -205,7 +206,7 @@ export interface SummarizationAppsyncStepfnProps {
205206

206207
}
207208

208-
export class SummarizationAppsyncStepfn extends Construct {
209+
export class SummarizationAppsyncStepfn extends BaseClass {
209210
/**
210211
* Returns an instance of events.IEventBus created by the construct
211212
*/
@@ -264,10 +265,7 @@ export class SummarizationAppsyncStepfn extends Construct {
264265
constructor(scope: Construct, id: string, props: SummarizationAppsyncStepfnProps) {
265266
super(scope, id);
266267

267-
let stage = '-dev';
268-
if (props?.stage) {
269-
stage = props.stage;
270-
}
268+
271269

272270
// observability
273271
let lambda_tracing = lambda.Tracing.ACTIVE;
@@ -319,7 +317,7 @@ export class SummarizationAppsyncStepfn extends Construct {
319317

320318
// bucket for storing server access logging
321319
const serverAccessLogBucket = new s3.Bucket(this,
322-
'serverAccessLogBucket'+stage,
320+
'serverAccessLogBucket'+this.stage,
323321
{
324322
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
325323
encryption: s3.BucketEncryption.S3_MANAGED,
@@ -341,10 +339,10 @@ export class SummarizationAppsyncStepfn extends Construct {
341339
this.inputAssetBucket = props.existingInputAssetsBucketObj;
342340
} else if (props?.bucketInputsAssetsProps) {
343341
this.inputAssetBucket = new s3.Bucket(this,
344-
'inputAssetsSummaryBucket'+stage, props.bucketInputsAssetsProps);
342+
'inputAssetsSummaryBucket'+this.stage, props.bucketInputsAssetsProps);
345343
} else {
346-
const bucketName= 'input-assets-summary-bucket'+stage+'-'+Aws.ACCOUNT_ID;
347-
this.inputAssetBucket = new s3.Bucket(this, 'inputAssetsSummaryBucket'+stage,
344+
const bucketName= 'input-assets-summary-bucket'+this.stage+'-'+Aws.ACCOUNT_ID;
345+
this.inputAssetBucket = new s3.Bucket(this, 'inputAssetsSummaryBucket'+this.stage,
348346
{
349347
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
350348
encryption: s3.BucketEncryption.S3_MANAGED,
@@ -368,11 +366,11 @@ export class SummarizationAppsyncStepfn extends Construct {
368366
this.processedAssetBucket = props.existingProcessedAssetsBucketObj;
369367
} else if (props?.bucketProcessedAssetsProps) {
370368
this.processedAssetBucket = new s3.Bucket(this,
371-
'processedAssetsSummaryBucket'+stage, props.bucketProcessedAssetsProps);
369+
'processedAssetsSummaryBucket'+this.stage, props.bucketProcessedAssetsProps);
372370
} else {
373-
const bucketName= 'processed-assets-summary-bucket'+stage+'-'+Aws.ACCOUNT_ID;
371+
const bucketName= 'processed-assets-summary-bucket'+this.stage+'-'+Aws.ACCOUNT_ID;
374372

375-
this.processedAssetBucket = new s3.Bucket(this, 'processedAssetsSummaryBucket'+stage,
373+
this.processedAssetBucket = new s3.Bucket(this, 'processedAssetsSummaryBucket'+this.stage,
376374
{
377375
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
378376
encryption: s3.BucketEncryption.S3_MANAGED,
@@ -437,9 +435,9 @@ export class SummarizationAppsyncStepfn extends Construct {
437435
const apiName = props.summaryApiName || 'summaryApi';
438436

439437
// graphql api for summary. client invoke this api with given schema and cognito user pool auth.
440-
const summarizationGraphqlApi = new appsync.GraphqlApi(this, 'summarizationGraphqlApi'+stage,
438+
const summarizationGraphqlApi = new appsync.GraphqlApi(this, 'summarizationGraphqlApi'+this.stage,
441439
{
442-
name: apiName+stage,
440+
name: apiName+this.stage,
443441
logConfig: api_log_config,
444442
definition: appsync.Definition.fromFile(
445443
path.join(__dirname, '../../../../resources/gen-ai/aws-summarization-appsync-stepfn/schema.graphql'),
@@ -528,7 +526,7 @@ export class SummarizationAppsyncStepfn extends Construct {
528526

529527
const construct_input_validation_lambda_props = {
530528
code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, '../../../../lambda/aws-summarization-appsync-stepfn/input_validator')),
531-
functionName: 'summary_input_validator'+stage,
529+
functionName: 'summary_input_validator'+this.stage,
532530
description: 'Lambda function to validate input for summary api',
533531
vpc: this.vpc,
534532
tracing: lambda_tracing,
@@ -544,7 +542,7 @@ export class SummarizationAppsyncStepfn extends Construct {
544542

545543
// Lambda function used to validate inputs in the step function
546544
const inputValidatorLambda = buildDockerLambdaFunction(this,
547-
'inputValidatorLambda' + stage,
545+
'inputValidatorLambda' + this.stage,
548546
construct_input_validation_lambda_props,
549547
props.customInputValidationDockerLambdaProps,
550548
);
@@ -618,7 +616,7 @@ export class SummarizationAppsyncStepfn extends Construct {
618616

619617
const construct_document_reader_lambda_props = {
620618
code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, '../../../../lambda/aws-summarization-appsync-stepfn/document_reader')),
621-
functionName: 'summary_document_reader'+stage,
619+
functionName: 'summary_document_reader'+this.stage,
622620
description: 'Lambda function to read the input transformed document',
623621
vpc: this.vpc,
624622
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
@@ -640,7 +638,7 @@ export class SummarizationAppsyncStepfn extends Construct {
640638

641639
// Lambda function used to read documents in the step function
642640
const documentReaderLambda = buildDockerLambdaFunction(this,
643-
'documentReaderLambda' + stage,
641+
'documentReaderLambda' + this.stage,
644642
construct_document_reader_lambda_props,
645643
props.customDocumentReaderDockerLambdaProps,
646644
);
@@ -719,7 +717,7 @@ export class SummarizationAppsyncStepfn extends Construct {
719717
);
720718

721719
const construct_generate_summary_lambda_props = {
722-
functionName: 'summary_generator'+stage,
720+
functionName: 'summary_generator'+this.stage,
723721
description: 'Lambda function to generate the summary',
724722
code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, '../../../../lambda/aws-summarization-appsync-stepfn/summary_generator')),
725723
vpc: this.vpc,
@@ -740,7 +738,7 @@ export class SummarizationAppsyncStepfn extends Construct {
740738

741739
// Lambda function used to generate the summary in the step function
742740
const generateSummarylambda = buildDockerLambdaFunction(this,
743-
'generateSummarylambda' + stage,
741+
'generateSummarylambda' + this.stage,
744742
construct_generate_summary_lambda_props,
745743
props.customSummaryGeneratorDockerLambdaProps,
746744
);
@@ -750,33 +748,44 @@ export class SummarizationAppsyncStepfn extends Construct {
750748
this.inputAssetBucket?.grantRead(documentReaderLambda);
751749
this.processedAssetBucket?.grantReadWrite(documentReaderLambda);
752750

751+
const baseProps: BaseClassProps={
752+
stage: props.stage,
753+
enableOperationalMetric: props.enableOperationalMetric,
754+
lambdaFunctions: [documentReaderLambda, generateSummarylambda, inputValidatorLambda],
755+
constructorName: this.constructor.name,
756+
constructId: id
757+
}
758+
this.updateConstructTrackingCode( baseProps,scope)
753759

754-
const enableOperationalMetric =
755-
props.enableOperationalMetric !== undefined && props.enableOperationalMetric !== null ? props.enableOperationalMetric : true;
760+
// const enableOperationalMetric =
761+
// props.enableOperationalMetric !== undefined && props.enableOperationalMetric !== null ? props.enableOperationalMetric : true;
756762

757-
if (enableOperationalMetric) {
758-
const solutionId = `genai_cdk_${version}/${this.constructor.name}/${id}`;
759-
documentReaderLambda.addEnvironment(
760-
'AWS_SDK_UA_APP_ID', solutionId,
761-
);
762-
generateSummarylambda.addEnvironment(
763-
'AWS_SDK_UA_APP_ID', solutionId,
764-
);
765-
inputValidatorLambda.addEnvironment(
766-
'AWS_SDK_UA_APP_ID', solutionId,
767-
);
768-
};
763+
// if (enableOperationalMetric) {
764+
// const solutionId = `genai_cdk_${version}/${this.constructor.name}/${id}`;
765+
// documentReaderLambda.addEnvironment(
766+
// 'AWS_SDK_UA_APP_ID', solutionId,
767+
// );
768+
// generateSummarylambda.addEnvironment(
769+
// 'AWS_SDK_UA_APP_ID', solutionId,
770+
// );
771+
// inputValidatorLambda.addEnvironment(
772+
// 'AWS_SDK_UA_APP_ID', solutionId,
773+
// );
774+
// // ADD unique key in CFN stack
775+
// Stack.of(scope).templateOptions.description =solutionId
776+
777+
// };
769778

770779

771780
// create datasource at appsync
772781
const SummaryStatusDataSource = new appsync.NoneDataSource
773-
(this, 'noneDataSource'+stage, {
782+
(this, 'noneDataSource'+this.stage, {
774783
api: summarizationGraphqlApi,
775784
name: 'SummaryStatusDataSource',
776785
});
777786

778787
SummaryStatusDataSource.createResolver
779-
('summaryResponseresolver'+stage, {
788+
('summaryResponseresolver'+this.stage, {
780789
typeName: 'Mutation',
781790
fieldName: 'updateSummaryJobStatus',
782791
requestMappingTemplate: appsync.MappingTemplate.fromString(
@@ -805,7 +814,7 @@ export class SummarizationAppsyncStepfn extends Construct {
805814
});
806815

807816
const dlq: sqs.Queue = new sqs.Queue(this, 'dlq', {
808-
queueName: 'summarydlq'+stage,
817+
queueName: 'summarydlq'+this.stage,
809818
retentionPeriod: Duration.days(7),
810819
enforceSSL: true,
811820
});
@@ -939,3 +948,4 @@ export class SummarizationAppsyncStepfn extends Construct {
939948

940949
}
941950
}
951+

0 commit comments

Comments
 (0)