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): allow disable x-ray #30808

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as iam from 'aws-cdk-lib/aws-iam';
import * as cdk from 'aws-cdk-lib';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';

This comment was marked as resolved.

/*
* Stack verification steps:
*
* -- aws stepfunctions describe-state-machine --state-machine-arn <stack-output> has a status of `ACTIVE`
* -- aws iam get-role-policy --role-name <role-name> --policy-name <policy-name> has all actions mapped to respective resources.
*/

This comment was marked as resolved.

const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-stepfunctions-integ');

// const role = new iam.Role(stack, 'Role', {
// assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
// });

This comment was marked as resolved.


const stateMachine = new sfn.StateMachine(stack, 'StateMachine', {
comment: 'a super cool state machine',
tracingEnabled: false,
});

// stateMachine.grantRead(role);
// stateMachine.grant(role, 'states:SendTaskSuccess');

This comment was marked as resolved.


app.synth();

This comment was marked as resolved.

34 changes: 20 additions & 14 deletions packages/aws-cdk-lib/aws-stepfunctions/lib/state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ export class StateMachine extends StateMachineBase {
stateMachineType: props.stateMachineType ?? undefined,
roleArn: this.role.roleArn,
loggingConfiguration: props.logs ? this.buildLoggingConfiguration(props.logs) : undefined,
tracingConfiguration: props.tracingEnabled ? this.buildTracingConfiguration() : undefined,
tracingConfiguration: this.buildTracingConfiguration(props.tracingEnabled),
...definitionBody.bind(this, this.role, props, graph),
definitionSubstitutions: props.definitionSubstitutions,
});
Expand Down Expand Up @@ -532,21 +532,27 @@ export class StateMachine extends StateMachineBase {
};
}

private buildTracingConfiguration(): CfnStateMachine.TracingConfigurationProperty {
this.addToRolePolicy(new iam.PolicyStatement({
// https://docs.aws.amazon.com/xray/latest/devguide/security_iam_id-based-policy-examples.html#xray-permissions-resources
// https://docs.aws.amazon.com/step-functions/latest/dg/xray-iam.html
actions: [
'xray:PutTraceSegments',
'xray:PutTelemetryRecords',
'xray:GetSamplingRules',
'xray:GetSamplingTargets',
],
resources: ['*'],
}));
private buildTracingConfiguration(isTracing?: boolean): CfnStateMachine.TracingConfigurationProperty | undefined {
if (isTracing === undefined) {
return undefined;
}

if (isTracing) {
this.addToRolePolicy(new iam.PolicyStatement({
// https://docs.aws.amazon.com/xray/latest/devguide/security_iam_id-based-policy-examples.html#xray-permissions-resources
// https://docs.aws.amazon.com/step-functions/latest/dg/xray-iam.html
actions: [
'xray:PutTraceSegments',
'xray:PutTelemetryRecords',
'xray:GetSamplingRules',
'xray:GetSamplingTargets',
],
resources: ['*'],
}));
}

return {
enabled: true,
enabled: isTracing,
};
}
}
Expand Down
19 changes: 19 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 @@ -348,6 +348,25 @@ describe('State Machine', () => {
});
});

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

// WHEN
new sfn.StateMachine(stack, 'MyStateMachine', {
definitionBody: sfn.DefinitionBody.fromChainable(sfn.Chain.start(new sfn.Pass(stack, 'Pass'))),
tracingEnabled: false,
});

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

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