@@ -118,6 +118,13 @@ export interface StateMachineProps {
118
118
* @default No logging
119
119
*/
120
120
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 ;
121
128
}
122
129
123
130
/**
@@ -272,37 +279,13 @@ export class StateMachine extends StateMachineBase {
272
279
273
280
this . stateMachineType = props . stateMachineType ? props . stateMachineType : StateMachineType . STANDARD ;
274
281
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
-
300
282
const resource = new CfnStateMachine ( this , 'Resource' , {
301
283
stateMachineName : this . physicalName ,
302
284
stateMachineType : props . stateMachineType ? props . stateMachineType : undefined ,
303
285
roleArn : this . role . roleArn ,
304
286
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 ,
306
289
} ) ;
307
290
308
291
resource . node . addDependency ( this . role ) ;
@@ -324,7 +307,7 @@ export class StateMachine extends StateMachineBase {
324
307
* Add the given statement to the role's policy
325
308
*/
326
309
public addToRolePolicy ( statement : iam . PolicyStatement ) {
327
- this . role . addToPolicy ( statement ) ;
310
+ this . role . addToPrincipalPolicy ( statement ) ;
328
311
}
329
312
330
313
/**
@@ -404,6 +387,44 @@ export class StateMachine extends StateMachineBase {
404
387
public metricTime ( props ?: cloudwatch . MetricOptions ) : cloudwatch . Metric {
405
388
return this . metric ( 'ExecutionTime' , props ) ;
406
389
}
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
+ }
407
428
}
408
429
409
430
/**
0 commit comments