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(stepfunctions): disabling logging still requires LogGroup #30816

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 24 additions & 18 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface LogOptions {
/**
* The log group where the execution history events will be logged.
*/
readonly destination: logs.ILogGroup;
readonly destination?: logs.ILogGroup;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this not be a breaking change for existing usage? example:

const logOptions: LogOptions = {
  destination: <LogGroup>
}

const stateMachine: StateMachine = new StateMachine(parent, 'ID', {
  logs: logOptions,
  ...
}
... 

stateMachine.logs.destination.logGroupName // Error
stateMachine.logs.destination!.logGroupName // Change needed

Some user(s) might be utilising ILogGroup's public properties from logOptions which will now cause issues because now such users will need to make non-null assertions before using such properties (e.g. with !)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the jsii-diff documentation:

You are allowed to make inputs optional

So i think this change is not a breaking change.


/**
* Determines whether execution data is included in your log.
Expand Down Expand Up @@ -507,26 +507,32 @@ export class StateMachine extends StateMachineBase {
}

private buildLoggingConfiguration(logOptions: LogOptions): CfnStateMachine.LoggingConfigurationProperty {
// https://docs.aws.amazon.com/step-functions/latest/dg/cw-logs.html#cloudwatch-iam-policy
this.addToRolePolicy(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'logs:CreateLogDelivery',
'logs:GetLogDelivery',
'logs:UpdateLogDelivery',
'logs:DeleteLogDelivery',
'logs:ListLogDeliveries',
'logs:PutResourcePolicy',
'logs:DescribeResourcePolicies',
'logs:DescribeLogGroups',
],
resources: ['*'],
}));
if (logOptions.level !== LogLevel.OFF && !logOptions.destination) {
throw new Error('Logs destination is required when level is not OFF.');
}
Comment on lines +569 to +571
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall this validation be done earlier (at line 427) with other validations instead?

if (!logs && !logs.level != LogLevel.OFF && !logs.destination) { ...

reasons for ask:

  • with current change, validations will be split across various places and as this file is big (and keep getting bigger), hence, it will become difficult to have complete picture of validation
  • ideally, caller shall not even call buildLoggingConfiguration if prerequisites are not met

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a separate validation method validateLogOptions()


if (logOptions.destination) {
// https://docs.aws.amazon.com/step-functions/latest/dg/cw-logs.html#cloudwatch-iam-policy
this.addToRolePolicy(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'logs:CreateLogDelivery',
'logs:GetLogDelivery',
'logs:UpdateLogDelivery',
'logs:DeleteLogDelivery',
'logs:ListLogDeliveries',
'logs:PutResourcePolicy',
'logs:DescribeResourcePolicies',
'logs:DescribeLogGroups',
],
resources: ['*'],
}));
}

return {
destinations: [{
destinations: logOptions.destination ? [{
cloudWatchLogsLogGroup: { logGroupArn: logOptions.destination.logGroupArn },
}],
}] : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we do this like as follows - will it reduce need to check logOptions.destination twice and simplify a bit?

let destinations = undefined;
if (logOptions.destination) {
  // Policy addition
  destinations = [{
    cloudWatchLogsLogGroup: { logGroupArn: logOptions.destination.logGroupArn },
  }];
}

return {
  destinations,
  ...,
  level: logOptions.level || LogLevel.ERROR
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

includeExecutionData: logOptions.includeExecutionData,
level: logOptions.level || 'ERROR',
};
Expand Down
31 changes: 31 additions & 0 deletions packages/aws-cdk-lib/aws-stepfunctions/test/state-machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,37 @@ describe('State Machine', () => {
});
});

test('log configuration with level OFF', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new sfn.StateMachine(stack, 'MyStateMachine', {
definitionBody: sfn.DefinitionBody.fromChainable(sfn.Chain.start(new sfn.Pass(stack, 'Pass'))),
logs: { level: sfn.LogLevel.OFF },
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::StepFunctions::StateMachine', {
DefinitionString: '{"StartAt":"Pass","States":{"Pass":{"Type":"Pass","End":true}}}',
LoggingConfiguration: {
Level: 'OFF',
},
});
});

test('log configuration throws when no destination specified', () => {
// GIVEN
const stack = new cdk.Stack();

expect(() => {
new sfn.StateMachine(stack, 'MyStateMachine', {
definitionBody: sfn.DefinitionBody.fromChainable(sfn.Chain.start(new sfn.Pass(stack, 'Pass'))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we not directly pass new sfn.Pass(stack, 'Pass') to DefintionBody.fromChainable as Pass implements IChainable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's just a copy & paste.
README also uses sfn.Chain.start() instead of bare sfn.Pass.

logs: { level: sfn.LogLevel.ERROR },
});
}).toThrow('Logs destination is required when level is not OFF.');
});

test('tracing configuration', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down
Loading