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

fix(constructs): fix log group name bug for step functions #166

Merged
merged 4 commits into from
Dec 15, 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
55 changes: 55 additions & 0 deletions src/common/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import * as cdk from 'aws-cdk-lib';
/**
* @internal This is an internal core function and should not be called directly by Solutions Constructs clients.
*
* @summary Creates a physical resource name in the style of the CDK (string+hash) - this value incorporates Stack ID,
* so it will remain static in multiple updates of a single stack, but will be different in a separate stack instance
* @param {string[]} parts - the various string components of the name (eg - stackName, solutions construct ID, L2 construct ID)
* @param {number} maxLength - the longest string that can be returned
* @returns {string} - a string with concatenated parts (truncated if necessary) + a hash of the full concatenated parts
*
*/
export function generatePhysicalName(
prefix: string,
parts: string[],
maxLength: number,
): string {
// The result will consist of:
// -The prefix - unaltered
// -The parts concatenated, but reduced in size to meet the maxLength limit for the overall name
// -A hyphen delimiter
// -The GUID portion of the stack arn

const stackIdGuidLength = 36;
const prefixLength = prefix.length;
const maxPartsLength = maxLength - prefixLength - 1 - stackIdGuidLength; // 1 is the hyphen

// Extract the Stack ID Guid
const uniqueStackIdPart = cdk.Fn.select(2, cdk.Fn.split('/', `${cdk.Aws.STACK_ID}`));

let allParts: string = '';

parts.forEach((part) => {
allParts += part;
});

if (allParts.length > maxPartsLength) {
const subStringLength = maxPartsLength / 2;
allParts = allParts.substring(0, subStringLength) + allParts.substring(allParts.length - subStringLength);
}

const finalName = prefix.toLowerCase() + allParts + '-' + uniqueStackIdPart;
return finalName;
}
18 changes: 16 additions & 2 deletions src/patterns/gen-ai/aws-rag-appsync-stepfn-opensearch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* and limitations under the License.
*/
import * as path from 'path';
import { Duration, Aws } from 'aws-cdk-lib';
import { Duration, Aws, Stack } from 'aws-cdk-lib';
import * as appsync from 'aws-cdk-lib/aws-appsync';
import * as cognito from 'aws-cdk-lib/aws-cognito';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
Expand All @@ -28,6 +28,7 @@ import * as stepfn_task from 'aws-cdk-lib/aws-stepfunctions-tasks';
import { NagSuppressions } from 'cdk-nag';
import { Construct } from 'constructs';
import * as s3_bucket_helper from '../../../common/helpers/s3-bucket-helper';
import { generatePhysicalName } from '../../../common/helpers/utils';
import * as vpc_helper from '../../../common/helpers/vpc-helper';

/**
Expand Down Expand Up @@ -770,6 +771,19 @@ export class RagAppsyncStepfnOpensearch extends Construct {
const definition = inputValidationTask.next(validate_input_choice.when(
stepfn.Condition.booleanEquals('$.validation_result.Payload.isValid', false), jobFailed).otherwise(run_files_in_parallel.next(embeddingsTask)));

const maxLogGroupNameLength = 255;
const logGroupPrefix = '/aws/vendedlogs/states/constructs/';
const maxGeneratedNameLength = maxLogGroupNameLength - logGroupPrefix.length;
const nameParts: string[] = [
Stack.of(scope).stackName, // Name of the stack
scope.node.id, // Construct ID
'StateMachineLogRag', // Literal string for log group name portion
];
const logGroupName = generatePhysicalName(logGroupPrefix, nameParts, maxGeneratedNameLength);
const ragLogGroup = new logs.LogGroup(this, 'ingestionStepFunctionLogGroup', {
logGroupName: logGroupName,
});

const ingestion_step_function = new stepfn.StateMachine(
this,
'IngestionStateMachine',
Expand All @@ -778,7 +792,7 @@ export class RagAppsyncStepfnOpensearch extends Construct {
definitionBody: stepfn.DefinitionBody.fromChainable(definition),
timeout: Duration.minutes(30),
logs: {
destination: new logs.LogGroup(this, 'ingestionStepFunctionLogGroup'),
destination: ragLogGroup,
level: stepfn.LogLevel.ALL,
},
tracingEnabled: enable_xray,
Expand Down
17 changes: 14 additions & 3 deletions src/patterns/gen-ai/aws-summarization-appsync-stepfn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* and limitations under the License.
*/
import * as path from 'path';
import { Duration, Aws } from 'aws-cdk-lib';
import { Duration, Aws, Stack } from 'aws-cdk-lib';
import * as appsync from 'aws-cdk-lib/aws-appsync';
import * as cognito from 'aws-cdk-lib/aws-cognito';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
Expand All @@ -30,6 +30,7 @@ import { Construct } from 'constructs';
import * as eventBridge from '../../../common/helpers/eventbridge-helper';
import * as redisHelper from '../../../common/helpers/redis-helper';
import * as s3BucketHelper from '../../../common/helpers/s3-bucket-helper';
import { generatePhysicalName } from '../../../common/helpers/utils';
import * as vpcHelper from '../../../common/helpers/vpc-helper';

export interface SummarizationAppsyncStepfnProps {
Expand Down Expand Up @@ -804,8 +805,18 @@ export class SummarizationAppsyncStepfn extends Construct {
),
);

const summarizationLogGroup = new logs.LogGroup(this, 'summarizationLogGroup', {});

const maxLogGroupNameLength = 255;
const logGroupPrefix = '/aws/vendedlogs/states/constructs/';
const maxGeneratedNameLength = maxLogGroupNameLength - logGroupPrefix.length;
const nameParts: string[] = [
Stack.of(scope).stackName, // Name of the stack
scope.node.id, // Construct ID
'StateMachineLogSummarization', // Literal string for log group name portion
];
const logGroupName = generatePhysicalName(logGroupPrefix, nameParts, maxGeneratedNameLength);
const summarizationLogGroup = new logs.LogGroup(this, 'summarizationLogGroup', {
logGroupName: logGroupName,
});

// step function definition
const definition = inputValidationTask.next(
Expand Down