Home > OS >  Adding manual approval stage in CDK CodePipelines
Adding manual approval stage in CDK CodePipelines

Time:12-02

I have been working with AWS CDK and I think its a great way to work with AWS. Recently I have got a problem which I am unable to resolve. Went over documentations and resources but none had explained how to do it in CDK. So I have two code pipelines and each pipeline either deploys to staging or production. Now I want a manual approval stage before the code gets deployed to production. I'll show my simple code below for reference:

import * as cdk from '@aws-cdk/core';
import { AppsPluginsCdkStack } from './apps-plugins-services/stack';
import {
  CodePipeline,
  ShellStep,
  CodePipelineSource
} from '@aws-cdk/pipelines';

class ApplicationStage extends cdk.Stage {
  constructor(scope: cdk.Construct, id: string) {
    super(scope, id);
    new CdkStack(this, 'cdkStack');
  }
}

class ProductionPipelineStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props: cdk.StackProps) {
    super(scope, id, props);

    //Create the CDK Production Pipeline
    const prodPipeline = new CodePipeline(this, 'ProductionPipeline', {
      pipelineName: 'ProdPipeline',
      synth: new ShellStep('ProdSynth', {
        // Use a connection created using the AWS console to authenticate to GitHub
        input: CodePipelineSource.connection(
          'fahigm/cdk-repo',
          'develop',
          {
            connectionArn:
              'AWS-CONNECTION-ARN' // Created using the AWS console
          }
        ),
        commands: ['npm ci', 'npm run build', 'npx cdk synth']
      })
    });

    prodPipeline.addStage(new ApplicationStage(this, 'Production'));
  }
}

class StagingPipelineStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props: cdk.StackProps) {
    super(scope, id, props);
    //Create the CDK Staging Pipeline
    const stagePipeline = new CodePipeline(this, 'StagingPipeline', {
      pipelineName: 'StagePipeline',
      synth: new ShellStep('StageSynth', {
        // Use a connection created using the AWS console to authenticate to GitHub
        input: CodePipelineSource.connection(
          'fahigm/cdk-repo',
          'master',
          {
            connectionArn:
              'AWS-CONNECTION-ARN' // Created using the AWS console
          }
        ),
        commands: ['npm ci', 'npm run build', 'npx cdk synth']
      })
    });

    stagePipeline.addStage(new ApplicationStage(this, 'Staging'));
  }
}
//
const app = new cdk.App();

new ProductionPipelineStack(app, 'ProductionCDKPipeline', {
  env: { account: 'ACCOUNT', region: 'REGION' }
});

new StagingPipelineStack(app, 'StagingCDKPipeline', {
  env: { account: 'ACCOUNT', region: 'REGION' }
});

app.synth();

Now I don't know where to go from here. The documentation only talks about how to do it from console but I want to add it in the code. Would really appreciate any help!

CodePudding user response:

The CDK documentation doesn't actually talk about how to do it from console, it talks about how to do it with CDK, and provides examples. Here's an example straight from the docs:

The following example shows both an automated approval in the form of a ShellStep, and a manual approval in the form of a ManualApprovalStep added to the pipeline. Both must pass in order to promote from the PreProd to the Prod environment:

declare const pipeline: pipelines.CodePipeline;
const preprod = new MyApplicationStage(this, 'PreProd');
const prod = new MyApplicationStage(this, 'Prod');

pipeline.addStage(preprod, {
  post: [
    new pipelines.ShellStep('Validate Endpoint', {
      commands: ['curl -Ssf https://my.webservice.com/'],
    }),
  ],
});
pipeline.addStage(prod, {
  pre: [
    new pipelines.ManualApprovalStep('PromoteToProd'),
  ],
});

Here's the documentation about the manual approval step specifically: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_pipelines.ManualApprovalStep.html

  • Related