'aws_cdk events rule target for cdk pipelines fails

below error pops when I try to target a CDK pipeline using events targets.

jsii.errors.JavaScriptError: 
  Error: Resolution error: Supplied properties not correct for "CfnRuleProps"
    targets: element 0: supplied properties not correct for "TargetProperty"
      arn: required but missing.

code is below

from aws_cdk import (
    core,
    aws_codecommit as codecommit,
    aws_codepipeline as codepipeline,
    aws_events as events,
    aws_events_targets as targets
)
from aws_cdk import pipelines

from aws_cdk.pipelines import CodePipeline, CodePipelineSource, ShellStep

class BootStrappingStack(core.Stack):

    def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs

        repo = codecommit.Repository(
            self, 'Repository',
            repository_name='Repository'
        )

        source_artifact = codepipeline.Artifact()

        cloud_assembly_artifact = codepipeline.Artifact()

        pipeline = CodePipeline(self, 'Pipeline',
            synth=ShellStep("Synth",
            input=CodePipelineSource.code_commit(
                repository=repo,
                branch='master'),
            commands=[
                'pip install -r requirements', 
                'npm install -g aws-cdk', 
                'cdk synth']
            )
            )

        rule = events.Rule(self, 'TriggerPipeline',
            schedule=events.Schedule.expression('rate(1 hour)')
        )

        rule.add_target(targets.CodePipeline(pipeline))

The documentation for aws_cdk.events_targets works with codepipeline construct, however doesn't work as documented for cdk pipelines.

This needs to be addressed in the documentation, once I get to know what's the fix. Please help.



Solution 1:[1]

The problem is that targets.CodePipeline receives a codepipeline.IPipeline as a parameter. But what you are using instead is a pipelines.CodePipeline, which is a different thing. CodePipeline is more abstract construct, built on top of the codepipeline module.

Solution 2:[2]

You can try this:

const pipeline = new CodePipeline(self, 'Pipeline' ....

Then:

rule.addTarget(new targets.CodePipeline(pipeline))

Solution 3:[3]

As mentioned by @Otavio, you need to use the codepipeline.IPipeline.

You can use the pipeline property from CDK CodePipeline construct but in order to use that first you need to construct the pipeline using build_pipeline() method:

       pipeline = CodePipeline(self, 'Pipeline',
            synth=ShellStep("Synth",
            input=CodePipelineSource.code_commit(
                repository=repo,
                branch='master'),
            commands=[
                'pip install -r requirements', 
                'npm install -g aws-cdk', 
                'cdk synth']
            )
            )

        # You need to construct the pipeline before passing it as a target in rule
        pipeline.build_pipeline() 

        rule = events.Rule(self, 'TriggerPipeline',
            schedule=events.Schedule.expression('rate(1 hour)')
        )

        # Using the pipeline property from CDK Codepipeline
        rule.add_target(targets.CodePipeline(pipeline.pipeline))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Otavio Macedo
Solution 2 Jeremy Caney
Solution 3 Cizer Pereira