Skip to content

Commit ad011c0

Browse files
authored
feat(stepfunctions): support X-Ray tracing (#10371) (#10374)
closes #10371 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9d6817f commit ad011c0

File tree

3 files changed

+103
-28
lines changed

3 files changed

+103
-28
lines changed

packages/@aws-cdk/aws-stepfunctions/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,22 @@ new stepfunctions.StateMachine(stack, 'MyStateMachine', {
568568
});
569569
```
570570

571+
## X-Ray tracing
572+
573+
Enable X-Ray tracing for StateMachine:
574+
575+
```ts
576+
const logGroup = new logs.LogGroup(stack, 'MyLogGroup');
577+
578+
new stepfunctions.StateMachine(stack, 'MyStateMachine', {
579+
definition: stepfunctions.Chain.start(new stepfunctions.Pass(stack, 'Pass')),
580+
tracingEnabled: true
581+
});
582+
```
583+
584+
See [the AWS documentation](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-xray-tracing.html)
585+
to learn more about AWS Step Functions's X-Ray support.
586+
571587
## State Machine Permission Grants
572588

573589
IAM roles, users, or groups which need to be able to work with a State Machine should be granted IAM permissions.

packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts

+48-27
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ export interface StateMachineProps {
118118
* @default No logging
119119
*/
120120
readonly logs?: LogOptions;
121+
122+
/**
123+
* Specifies whether Amazon X-Ray tracing is enabled for this state machine.
124+
*
125+
* @default false
126+
*/
127+
readonly tracingEnabled?: boolean;
121128
}
122129

123130
/**
@@ -272,37 +279,13 @@ export class StateMachine extends StateMachineBase {
272279

273280
this.stateMachineType = props.stateMachineType ? props.stateMachineType : StateMachineType.STANDARD;
274281

275-
let loggingConfiguration: CfnStateMachine.LoggingConfigurationProperty | undefined;
276-
if (props.logs) {
277-
const conf = props.logs;
278-
loggingConfiguration = {
279-
destinations: [{ cloudWatchLogsLogGroup: { logGroupArn: conf.destination.logGroupArn } }],
280-
includeExecutionData: conf.includeExecutionData,
281-
level: conf.level || 'ERROR',
282-
};
283-
// https://docs.aws.amazon.com/step-functions/latest/dg/cw-logs.html#cloudwatch-iam-policy
284-
this.addToRolePolicy(new iam.PolicyStatement({
285-
effect: iam.Effect.ALLOW,
286-
actions: [
287-
'logs:CreateLogDelivery',
288-
'logs:GetLogDelivery',
289-
'logs:UpdateLogDelivery',
290-
'logs:DeleteLogDelivery',
291-
'logs:ListLogDeliveries',
292-
'logs:PutResourcePolicy',
293-
'logs:DescribeResourcePolicies',
294-
'logs:DescribeLogGroups',
295-
],
296-
resources: ['*'],
297-
}));
298-
}
299-
300282
const resource = new CfnStateMachine(this, 'Resource', {
301283
stateMachineName: this.physicalName,
302284
stateMachineType: props.stateMachineType ? props.stateMachineType : undefined,
303285
roleArn: this.role.roleArn,
304286
definitionString: Stack.of(this).toJsonString(graph.toGraphJson()),
305-
loggingConfiguration,
287+
loggingConfiguration: props.logs ? this.buildLoggingConfiguration(props.logs) : undefined,
288+
tracingConfiguration: props.tracingEnabled ? this.buildTracingConfiguration() : undefined,
306289
});
307290

308291
resource.node.addDependency(this.role);
@@ -324,7 +307,7 @@ export class StateMachine extends StateMachineBase {
324307
* Add the given statement to the role's policy
325308
*/
326309
public addToRolePolicy(statement: iam.PolicyStatement) {
327-
this.role.addToPolicy(statement);
310+
this.role.addToPrincipalPolicy(statement);
328311
}
329312

330313
/**
@@ -404,6 +387,44 @@ export class StateMachine extends StateMachineBase {
404387
public metricTime(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
405388
return this.metric('ExecutionTime', props);
406389
}
390+
391+
private buildLoggingConfiguration(logOptions: LogOptions): CfnStateMachine.LoggingConfigurationProperty {
392+
// https://docs.aws.amazon.com/step-functions/latest/dg/cw-logs.html#cloudwatch-iam-policy
393+
this.addToRolePolicy(new iam.PolicyStatement({
394+
effect: iam.Effect.ALLOW,
395+
actions: [
396+
'logs:CreateLogDelivery',
397+
'logs:GetLogDelivery',
398+
'logs:UpdateLogDelivery',
399+
'logs:DeleteLogDelivery',
400+
'logs:ListLogDeliveries',
401+
'logs:PutResourcePolicy',
402+
'logs:DescribeResourcePolicies',
403+
'logs:DescribeLogGroups',
404+
],
405+
resources: ['*'],
406+
}));
407+
408+
return {
409+
destinations: [{
410+
cloudWatchLogsLogGroup: { logGroupArn: logOptions.destination.logGroupArn },
411+
}],
412+
includeExecutionData: logOptions.includeExecutionData,
413+
level: logOptions.level || 'ERROR',
414+
};
415+
}
416+
417+
private buildTracingConfiguration(): CfnStateMachine.TracingConfigurationProperty {
418+
this.addToRolePolicy(new iam.PolicyStatement({
419+
// https://docs.aws.amazon.com/xray/latest/devguide/security_iam_id-based-policy-examples.html#xray-permissions-resources
420+
actions: ['xray:PutTraceSegments', 'xray:PutTelemetryRecords'],
421+
resources: ['*'],
422+
}));
423+
424+
return {
425+
enabled: true,
426+
};
427+
}
407428
}
408429

409430
/**

packages/@aws-cdk/aws-stepfunctions/test/state-machine.test.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,42 @@ describe('State Machine', () => {
120120
});
121121
});
122122

123-
});
123+
test('tracing configuration', () => {
124+
// GIVEN
125+
const stack = new cdk.Stack();
126+
127+
// WHEN
128+
new stepfunctions.StateMachine(stack, 'MyStateMachine', {
129+
definition: stepfunctions.Chain.start(new stepfunctions.Pass(stack, 'Pass')),
130+
tracingEnabled: true,
131+
});
132+
133+
// THEN
134+
expect(stack).toHaveResource('AWS::StepFunctions::StateMachine', {
135+
DefinitionString: '{"StartAt":"Pass","States":{"Pass":{"Type":"Pass","End":true}}}',
136+
TracingConfiguration: {
137+
Enabled: true,
138+
},
139+
});
140+
141+
expect(stack).toHaveResource('AWS::IAM::Policy', {
142+
PolicyDocument: {
143+
Statement: [{
144+
Action: [
145+
'xray:PutTraceSegments',
146+
'xray:PutTelemetryRecords',
147+
],
148+
Effect: 'Allow',
149+
Resource: '*',
150+
}],
151+
Version: '2012-10-17',
152+
},
153+
PolicyName: 'MyStateMachineRoleDefaultPolicyE468EB18',
154+
Roles: [
155+
{
156+
Ref: 'MyStateMachineRoleD59FFEBC',
157+
},
158+
],
159+
});
160+
});
161+
});

0 commit comments

Comments
 (0)