Search code examples
amazon-web-servicesnpmaws-lambdaaws-step-functions

How to update a Step Function in AWS CLI without Deleting the Logs


I have been working with AWS for the last month and I need to know how can we update the step function without changing the name of the file.
The way that the documentation provided to make the changes in the step function is to change the name of the existing one and add the changes in the file. But that will eliminate the logs that have been created in the AWS CLI.

For Example, if I replace the following code with something else I have to change the whole dynamic of the project in order to make them appear in the AWS CLI enter image description here

Can somebody please provide a solution for this??


Solution

  • The update part can be done through an AWS configuration command. Follow the commands below, it will keep all the changes in the execution logs as wells.

    let aws = require('aws-sdk');
    
    let roleArn = `roleARN goes here`;
    
    let params = {
      name: stepFunctionName,
      roleArn: roleArn,
      definition: JSON.stringify(definitionGoesHere),
    };
    
    let stepFunctions = new aws.StepFunctions();
    
    stepfunctions.createStateMachine(params, function (err, data) {
      if (err) {
        console.log("error occured while creating the step function");
        console.log(err, err.stack);
        if (err.code === "StateMachineAlreadyExists" && err.statusCode === 400) {
          let paramsUpdate = {
            stateMachineArn: "stateMachine ARN for the existing stateMachine",
            definition: JSON.stringify(definition),
            loggingConfiguration: {
              includeExecutionData: true,
            },
            roleArn: roleArn,
          };
    
          stepfunctions.updateStateMachine(
            paramsUpdate,
            function (error, updateData) {
              if (error) {
                console.log("error occured while updating the step function.");
                console.log("Error", error.stack);
              }
              console.log("step function updated successfully");
              console.log("response", updateData);
            }
          );
        }
    
        console.log(
          "step function does not exist and the function creation and update faild in the process."
        );
        console.log("definition", definition for the stateMachine);
      } // an error occurred
      else console.log(data); // successful response
    });