Create pipelines with an S3 source enabled for events (Amazon CloudFormation template)
This procedure is for a pipeline where the source bucket has events enabled.
Use these steps to create a pipeline with an Amazon S3 source for event-based change detection.
To build an event-driven pipeline with Amazon S3, you edit the
PollForSourceChanges
parameter of your pipeline and then add the
following resources to your template:
-
EventBridge rule and IAM role to allow this event to start your pipeline.
If you use Amazon CloudFormation to create and manage your pipelines, your template includes content like the following.
Note
The Configuration
property in the source stage called
PollForSourceChanges
. If your template doesn't include that
property, then PollForSourceChanges
is set to true
by
default.
To create an EventBridge rule with Amazon S3 as the event source and CodePipeline as the target and apply the permissions policy
-
In the template, under
Resources
, use theAWS::IAM::Role
Amazon CloudFormation resource to configure the IAM role that allows your event to start your pipeline. This entry creates a role that uses two policies:-
The first policy allows the role to be assumed.
-
The second policy provides permissions to start the pipeline.
Why am I making this change? Adding
AWS::IAM::Role
resource enables Amazon CloudFormation to create permissions for EventBridge. This resource is added to your Amazon CloudFormation stack. -
-
Use the
AWS::Events::Rule
Amazon CloudFormation resource to add an EventBridge rule. This event pattern creates an event that monitors creation or deletion of objects in your Amazon S3 source bucket. In addition, include a target of your pipeline. When an object is created, this rule invokesStartPipelineExecution
on your target pipeline.Why am I making this change? Adding the
AWS::Events::Rule
resource enables Amazon CloudFormation to create the event. This resource is added to your Amazon CloudFormation stack. -
Save your updated template to your local computer, and open the Amazon CloudFormation console.
-
Choose your stack, and then choose Create Change Set for Current Stack.
-
Upload your updated template, and then view the changes listed in Amazon CloudFormation. These are the changes that will be made to the stack. You should see your new resources in the list.
-
Choose Execute.
To edit your pipeline's PollForSourceChanges parameter
Important
When you create a pipeline with this method, the PollForSourceChanges
parameter defaults to true if it is not explicitly set to false. When you add
event-based change detection, you must add the parameter to your output and set it to
false to disable polling. Otherwise, your pipeline starts twice for a single source
change. For details, see Valid settings for the
PollForSourceChanges parameter.
-
In the template, change
PollForSourceChanges
tofalse
. If you did not includePollForSourceChanges
in your pipeline definition, add it and set it tofalse
.Why am I making this change? Changing
PollForSourceChanges
tofalse
turns off periodic checks so you can use event-based change detection only.
When you use Amazon CloudFormation to create these resources, your pipeline is triggered when files in your repository are created or updated.
Note
Do not stop here. Although your pipeline is created, you must create a second Amazon CloudFormation template for your Amazon S3 pipeline. If you do not create the second template, your pipeline does not have any change detection functionality.