Question: How can I fix AWS CodePipeline when it runs twice?

My AWS CodePipeline runs twice. How can I resolve this issue?Short descriptionThere are two common reasons AWS CodePipeline runs more than once:The PollForSourceChanges parameter is set to true, and that causes a second, polling-triggered launch of the pipeline.There's a duplicate CloudWatch Events rule with the same target as the pipeline, and that causes the pipeline to run twice.To resolve the issue, first look at the pipeline history to confirm what's causing the pipeline to run twice.If there are more than one of the same CloudWatch Events rule-triggered launches of the pipeline, delete or disable any duplicate rules.If there are polling-triggered launches of the pipeline, read the Understand the default behavior of the PollForSourceChanges parameter section. Then, based on your scenario, complete the steps in one of the following sections:If you created your pipeline with AWS CloudFormation, then complete the steps in the Update your AWS CloudFormation template section.If you created your pipeline with the AWS Command Line Interface (AWS CLI), then complete the steps in the Update your pipeline with a JSON file section.If you created your pipeline with the AWS SDK, then complete the steps in the Update your pipeline based on the configuration syntax of your language section.Important: Update your pipeline using the same method that you created it with. Avoid making out-of-band changes to your pipeline, and make sure that you complete the steps in the section that applies to your scenario only. For example, if you created your pipeline with AWS CloudFormation, then follow the steps in the Update your AWS CloudFormation template section only.

ResolutionNote: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent version of the AWS CLI.Confirm what's causing the pipeline to run twice1.    Open the CodePipeline console.2.    In Name, choose the name of the pipeline.3.    Choose View history.4.    In the Trigger column, check to see if there are any duplicate CloudWatch Events rule-triggered or polling-triggered launches of the pipeline.5.    If there are more than one of the same CloudWatch Events rule-triggered launches, delete or disable any duplicate rules.-or-If there are polling-triggered launches of the pipeline, take the following troubleshooting steps.Understand the default behavior of the PollForSourceChanges parameterConsider the following:The default behavior of the PollForSourceChanges parameter is determined by the method used to create the pipeline.In many cases, the value of PollForSourceChanges is set to true by default and must be disabled.If you create your pipeline with the CodePipeline console, then the source detection method is automatically set to Amazon CloudWatch Events (the recommended way of detecting change in your source).If you create your pipeline with AWS CloudFormation, the AWS CLI, or the AWS SDK and don't specify the change detection method, then PollForSourceChanges is set to true by default (depending on the creation method).If you create your pipeline using a method other than the CodePipeline console and then update your pipeline out-of-band using the console, CodePipeline automatically creates an extra CloudWatch Events rule.If you create a CloudWatch Events rule in your AWS CloudFormation template or create a webhook for your GitHub version 1 repository and don't set the PollForSourceChanges parameter, then you end up with two ways to detect changes in the source. This causes your pipeline to run twice.Update your AWS CloudFormation templateIn your AWS CloudFormation template or pipeline configuration file, set the PollForSourceChanges parameter to false.Note: The PollForSourceChanges parameter is set to true by default.For more information on GitHub version 1 Webhooks, see Use webhooks to start a pipeline.Update your pipeline with a JSON file1.    Copy your pipeline structure to a JSON file:$ aws codepipeline get-pipeline --name NAME_OF_YOUR_PIPELINE  > pipeline.json2.    Open the pipeline.json file in a text editor, and then add the PollForSourceChanges parameter to the source actions configuration section. Set the parameter to false.3.    Remove the following metadata fields from the file:"metadata":{}"created""pipelineARN""updated"Important: The metadata lines must be removed from the pipeline.json file so the following update-pipeline command can use it.4.    Save the pipeline.json file, and then run the following update-pipeline command to apply the changes to the file:$ aws codepipeline update-pipeline --cli-input-json file://pipeline.jsonFor more information, see Edit a pipeline (AWS CLI).Update your pipeline based on the configuration syntax of your languageFor instructions on updating your pipeline, see the SDK documentation for your language.For example, if you deployed your pipeline with Python, you can set PollForSourceChanges to false in the configuration section of your pipeline.