aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.46k stars 3.83k forks source link

(stepfunctions): cannot disable x-ray tracing #30796

Closed Tietew closed 3 weeks ago

Tietew commented 1 month ago

Describe the bug

When I have a StateMachine with X-Ray tracing is enabled, Setting tracingEnabled to false does not disable X-Ray tracing.

Expected Behavior

Changing tracingEnabled from true to false disables X-Ray tracing.

Current Behavior

Changing tracingEnabled to false does not modify X-Ray tracing.

Reproduction Steps

First deploy a StateMachine with tracingEnabled: true. The stack creates an StateMacine with X-Ray tracing enabled.

new sfn.StateMachine(stack, 'StateMachine', {
  definitionBody: sfn.DefinitionBody.fromChainable(...),
  tracingEnabled: true,
});

Then, change tracingEnabled: false and deploy again. X-Ray tracing is still enabled.

new sfn.StateMachine(stack, 'StateMachine', {
  definitionBody: sfn.DefinitionBody.fromChainable(...),
  tracingEnabled: false,
});

Possible Solution

When tracingEnabled is explictly false, render "TrancingConfiguration": {"Enabled": false} in template instead of undefined.

Additional Information/Context

Workaround: Following escape hatch can diable X-Ray tracing.

const machine = new sfn.StateMachine(stack, 'StateMachine', {
  definitionBody: sfn.DefinitionBody.fromChainable(...),
  tracingEnabled: false,
});
(machine.node.defaultChild as cdk.CfnResource).addPropertyOverride('TracingConfiguration', { Enabled: false });

CDK CLI Version

2.148.0

Framework Version

No response

Node.js Version

20.15.1

OS

Linux

Language

TypeScript

Language Version

No response

Other information

No response

ashishdhingra commented 1 month ago

Reproducible using code below (reference Creating a Lambda state machine for Step Functions using AWS CDK):

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';

export class Issue30796Stack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const helloFunction = new lambda.Function(this, 'MyLambdaFunction', {
      code: lambda.Code.fromInline(`
            exports.handler = (event, context, callback) => {
                callback(null, "Hello World!");
            };
        `),
      runtime: lambda.Runtime.NODEJS_18_X,
      handler: "index.handler",
      timeout: cdk.Duration.seconds(3)
    });

    const stateMachine = new sfn.StateMachine(this, 'MyStateMachine', {
      definition: new tasks.LambdaInvoke(this, "MyLambdaTask", {
        lambdaFunction: helloFunction
      }).next(new sfn.Succeed(this, "GreetedWorld")),
      tracingEnabled: true // false
    });
  }
}

Running cdk synth on 2nd deployment with tracingEnabled set to false generates the following CF template:

Resources:
  MyLambdaFunctionServiceRole313A4D46:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
        Version: "2012-10-17"
      ManagedPolicyArns:
        - Fn::Join:
            - ""
            - - "arn:"
              - Ref: AWS::Partition
              - :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
    Metadata:
      aws:cdk:path: Issue30796Stack/MyLambdaFunction/ServiceRole/Resource
  MyLambdaFunction67CCA873:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: "

          \            exports.handler = (event, context, callback) => {

          \                callback(null, \"Hello World!\");

          \            };

          \        "
      Handler: index.handler
      Role:
        Fn::GetAtt:
          - MyLambdaFunctionServiceRole313A4D46
          - Arn
      Runtime: nodejs18.x
      Timeout: 3
    DependsOn:
      - MyLambdaFunctionServiceRole313A4D46
    Metadata:
      aws:cdk:path: Issue30796Stack/MyLambdaFunction/Resource
  MyStateMachineRoleD59FFEBC:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service: states.amazonaws.com
        Version: "2012-10-17"
    Metadata:
      aws:cdk:path: Issue30796Stack/MyStateMachine/Role/Resource
  MyStateMachineRoleDefaultPolicyE468EB18:
    Type: AWS::IAM::Policy
    Properties:
      PolicyDocument:
        Statement:
          - Action: lambda:InvokeFunction
            Effect: Allow
            Resource:
              - Fn::GetAtt:
                  - MyLambdaFunction67CCA873
                  - Arn
              - Fn::Join:
                  - ""
                  - - Fn::GetAtt:
                        - MyLambdaFunction67CCA873
                        - Arn
                    - :*
        Version: "2012-10-17"
      PolicyName: MyStateMachineRoleDefaultPolicyE468EB18
      Roles:
        - Ref: MyStateMachineRoleD59FFEBC
    Metadata:
      aws:cdk:path: Issue30796Stack/MyStateMachine/Role/DefaultPolicy/Resource
  MyStateMachine6C968CA5:
    Type: AWS::StepFunctions::StateMachine
    Properties:
      DefinitionString:
        Fn::Join:
          - ""
          - - '{"StartAt":"MyLambdaTask","States":{"MyLambdaTask":{"Next":"GreetedWorld","Retry":[{"ErrorEquals":["Lambda.ClientExecutionTimeoutException","Lambda.ServiceException","Lambda.AWSLambdaException","Lambda.SdkClientException"],"IntervalSeconds":2,"MaxAttempts":6,"BackoffRate":2}],"Type":"Task","Resource":"arn:'
            - Ref: AWS::Partition
            - :states:::lambda:invoke","Parameters":{"FunctionName":"
            - Fn::GetAtt:
                - MyLambdaFunction67CCA873
                - Arn
            - '","Payload.$":"$"}},"GreetedWorld":{"Type":"Succeed"}}}'
      RoleArn:
        Fn::GetAtt:
          - MyStateMachineRoleD59FFEBC
          - Arn
    DependsOn:
      - MyStateMachineRoleDefaultPolicyE468EB18
      - MyStateMachineRoleD59FFEBC
    UpdateReplacePolicy: Delete
    DeletionPolicy: Delete
    Metadata:
      aws:cdk:path: Issue30796Stack/MyStateMachine/Resource
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Analytics: v2:deflate64:H4sIAAAAAAAA/1WO0QqCQBBFv6X3dVLpoXchCApCP0DG3YkmdTecVYnFfw+1wJ7uucPMvZNCcjhCvMNRIm3qqOEKQuFR1wpHKUODbWUQwqm32rOzKrvbH0+KsYWQu4bm8aI317B+z3alSYmn1/17IaVHqQUuS+rZDq6m/wUIRa81kVGFR09X1A+2S/zWT5PKSVzf6bV5w5mzhtfvrDMET9kPaQxJAsnuKcxR11vPLUG+6gf2Bp9OAQEAAA==
...

Notice that it doesn't include below TracingConfiguration::Enabled property (which was included in 1st deployment as shown below):

      TracingConfiguration:
        Enabled: true
github-actions[bot] commented 3 weeks ago

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.