Skip to content

Step functions is triggered twice in CodePipeline

0

After deploy stage, I add new stage which will trigger step functions to create ami, update launch template. I noticed that 2 state machines are being executed. One of those state machines will fail because it create image from 1 servers, which gave the error: Another CreateImage operation is in progress on this instance. While the instance is rebooting during this operation, only one CreateImage operation can be in progress. Wait until the instance has rebooted, and then try again. But If I config notify by using sns which trigger lambda, lambda will then run step functions using boto3, then state machine only run once. I do not why CodePipeline stage run state machine twice

3 Answers
3

Hi,

To diagnose and fix your issue, you may want to follow this KC article: https://repost.aws/knowledge-center/codepipeline-running-twice

You may also find this article interesting: https://medium.com/@shweta2202/step-functions-preventing-duplicate-executions-a4f5f2868381

Best,

Didier

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

1

Hello,

Follow these steps to resolve issue:

CodePipeline Configuration:

  • Parallel Execution: Ensure that your CodePipeline stage is not configured to run actions in parallel. If it is, this could be causing the two state machines to be triggered simultaneously.
  • Dependency Configuration: Verify that the actions within the stage are properly configured to depend on each other. If the state machines are not dependent on each other, they could be executed independently.

State Machine Logic:

  • Concurrency Control: Check if the state machine logic includes any mechanisms to prevent concurrent CreateImage operations. If not, you may need to implement a wait state or a conditional branch to ensure that the operation is not attempted while another is in progress.
  • Error Handling: Ensure that the state machine is properly handling errors and retrying failed operations if necessary. This can help prevent the state machine from failing prematurely.

SNS and Lambda Configuration:

  • Message Deduplication: Verify that the SNS topic is configured to use message deduplication. This can help prevent the Lambda function from being triggered multiple times with the same message.
  • Lambda Function Logic: Ensure that the Lambda function is properly handling the SNS message and triggering the state machine only once. Avoid unnecessary retries or parallel executions within the Lambda function.

Related document links:

EXPERT

answered 2 years ago

  • My pipeline execution mode is superseded so I can 100% sure that my pipeline does not run parallel. In state machine at createImage stage, I do not implement mechanisms to prevent concurrent CreateImage operations because I think state machine will run once, but not twice. But if I setup notify in CodePipeline, sns will trigger lambda to start state machine. In that case, state machine only run once. For more information, I manually release change in codePipeline to test the behaviour

  • Analyze the logs: Examine the logs for CodePipeline, state machine, SNS, and Lambda to identify any errors, warnings, or unexpected behavior.

  • I examine the log and do see step functions run twice, I also see the fail error, but cannot find any clue to identify why step functions run twice. If codePipeline runs parallel then it would be the case, but the only person trigger codePipeline is me, so there is no parallel execution there. Again, this only happen if I add step functions as stage behind deploy stage. But if I config sns to trigger lambda to run step functions, then state machine is only run once. I don't know why but I will stick with the latter solution until find out the reason

0

I included the picture of my step functions state machine for reference Step functions state machines

answered 2 years ago

  • Your Step Function is likely being triggered twice due to a configuration issue in CodePipeline or the state machine itself.

    Check:

    1. CodePipeline stage: Ensure it's not set to run actions in parallel.

    2. State machine: Verify there's no concurrency control issue or missing error handling.

    3. SNS and Lambda: Confirm message deduplication and proper Lambda logic.

  • I think the issue is in codePipeLine stage config. I do simple test, by adding stage after deploy with a lambda function. After deploy stage, it run into the stage, and in cloudwatch logs, I do see lambda run twice. If I config notify using sns and lambda then lambda run only once

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.