title: How do I get the source artifact from CodePipeline on CDK?
body: <p>I'm using the official AWS documentation to create a pipeline using CDK: <a href="https://docs.aws.amazon.com/cdk/latest/guide/cdk_pipeline.html#cdk_pipeline_define" rel="nofollow noreferrer">https://docs.aws.amazon.com/cdk/latest/guide/cdk_pipeline.html#cdk_pipeline_define</a> (with a slight variation to the docs, where I used a CodeStar connection, as the code comments recommend)</p>
  <p>This automatically creates a self-mutating pipeline, with three stages -- Source, Synth, and UpdatePipeline. That's great.</p>
  <p>I would like to add a new stage with a CodeBuild action. I'd like the CodeBuild action to be based on the buildspec.yml file in the source directory.</p>
  <p>On the console, I can easily do this by clicking &quot;Add new stage&quot;, &quot;Add action&quot;, and selecting the input artifact from the dropdown menu.</p>
  <p>However, on CDK, with this recommended setup there's no easy way to get access to the input artifacts.</p>
  <p>I managed to do it by forcing <code>buildPipeline()</code> and doing this:</p>
  <pre class="lang-js prettyprint-override"><code>import * as cdk from &quot;@aws-cdk/core&quot;;
  import {
    CodePipeline,
    ShellStep,
    CodePipelineSource,
  } from &quot;@aws-cdk/pipelines&quot;;
  import * as codebuild from &quot;@aws-cdk/aws-codebuild&quot;;
  import * as codepipelineActions from &quot;@aws-cdk/aws-codepipeline-actions&quot;;
  
  
  export class PipelineStack extends cdk.Stack {
    public readonly source: cdk.CfnOutput
  
    constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
      super(scope, id, props);
  
      const source = CodePipelineSource.connection(&quot;someuser/somerepo&quot;, &quot;master&quot;, {
        connectionArn: &quot;arn:aws:codestar-connections:us-east-1:REDACTED:connection/REDACTED&quot;
      });
  
      const synthShellStep = new ShellStep(&quot;Synth&quot;, {
        input: source,
        commands: [
            &quot;cd infrastructure&quot;,
            &quot;npm run ci&quot;,
            &quot;npm run build&quot;,
            &quot;npx cdk synth&quot;
        ],
        &quot;primaryOutputDirectory&quot;: &quot;infrastructure/cdk.out&quot;
      });
  
      const pipeline = new CodePipeline(this, &quot;Pipeline&quot;, {
        pipelineName: &quot;FancyPipeline&quot;,
        synth: synthShellStep
      });
  
      // Need to build the pipeline to access the
      // source artifact
      pipeline.buildPipeline();
  
      const sourceStage = pipeline.pipeline.stage(&quot;Source&quot;);
      if (sourceStage) {
        const sourceOutputs = sourceStage.actions[0].actionProperties.outputs;
        if (sourceOutputs &amp;&amp; sourceOutputs.length &gt; 0) {
          const sourceArtifact = sourceOutputs[0];
  
  
          const codeBuildProject = new codebuild.PipelineProject(this, 'DockerBuildProject', {
            environment: {
              privileged: true
            }
          });
  
          const buildAction = new codepipelineActions.CodeBuildAction({
            actionName: 'DockerBuild',
            project: codeBuildProject,
            input: sourceArtifact,
            environmentVariables: {
              AWS_DEFAULT_REGION: {
                value: this.region
              },
              AWS_ACCOUNT_ID: {
                value: this.account
              },
              IMAGE_REPO_NAME: {
                value: &quot;somereponame&quot;
              },
              IMAGE_TAG: {
                value: &quot;latest&quot;
              }
            }
          });
  
          pipeline.pipeline.addStage({
            stageName: &quot;DockerBuildStage&quot;,
            actions: [buildAction],
          });
        }
      }
    }
  }
  </code></pre>
  <p>But this feels overall pretty awkward, and I can't call <code>addStage()</code> on the <code>CodePipeline</code> construct anymore. Surely there's a better way to do what I'm trying to do?</p>
  <p>Any help/advice would be appreciated. Thanks.</p>