Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/okruti-raghuraj/aws-1/llms.txt

Use this file to discover all available pages before exploring further.

AwsCdkPipelineStack is the top-level CDK stack for this project. It provisions a fully managed, self-mutating AWS CodePipeline that watches a GitHub repository and automatically rebuilds and redeploys your CDK application whenever changes are pushed to the tracked branch.

Overview

AwsCdkPipelineStack extends cdk.Stack and serves as the entry point for the entire CI/CD infrastructure. It uses the higher-level CodePipeline construct from aws-cdk-lib/pipelines, which wraps AWS CodePipeline and CodeBuild into a single, opinionated abstraction that understands CDK app structure.
aws-cdk-pipeline/lib/aws-cdk-pipeline-stack.ts
import * as cdk from 'aws-cdk-lib';
//import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CodePipeline ,  CodePipelineSource , ShellStep } from 'aws-cdk-lib/pipelines';
// import * as sqs from 'aws-cdk-lib/aws-sqs';

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

    // The code that defines your stack goes here

    const pipeline = new CodePipeline(this , 'Pipeline' ,{
      pipelineName: 'TestPipeline',
      crossAccountKeys: false,
      synth: new ShellStep('synth' , {
        input: CodePipelineSource.gitHub('RaghurajSingh/SMS','aws-pipeline'),
        commands:['npm ci' ,
                  'npm run build',
                  'npx cdk synth']
      }),
    });
    // example resource
    // const queue = new sqs.Queue(this, 'AwsCdkPipelineQueue', {
    //   visibilityTimeout: cdk.Duration.seconds(300)
    // });
  }
}

Constructor Parameters

scope
Construct
required
The parent construct for this stack. In practice this is always the cdk.App instance created in bin/aws-cdk-pipeline.ts.
id
string
required
The logical ID of the stack within the CDK app. This project passes 'AwsCdkPipelineStack', which also becomes the CloudFormation stack name used during cdk deploy.
props
cdk.StackProps
Optional stack properties. The env field pins the stack to a specific AWS account and region. This project targets account 602143770054 in ap-south-1.

The CodePipeline Construct

The CodePipeline construct (from aws-cdk-lib/pipelines) is the heart of this stack. It creates an AWS CodePipeline with a built-in self-mutation stage — meaning the pipeline will update its own definition before deploying application changes. The three configuration options used here are:
OptionValuePurpose
pipelineName'TestPipeline'Human-readable name shown in the AWS Console under CodePipeline.
crossAccountKeysfalseDisables cross-account KMS key creation, reducing cost and complexity for single-account deployments.
synthShellStep(...)Defines the CodeBuild action that synthesizes the CDK app into a CloudFormation template.

The ShellStep (Synth Step)

The synth step is the CodeBuild phase that converts your TypeScript CDK app into CloudFormation templates. It is configured with two key properties:
input
CodePipelineSource
CodePipelineSource.gitHub('RaghurajSingh/SMS', 'aws-pipeline') — connects to the aws-pipeline branch of the RaghurajSingh/SMS GitHub repository. CodePipeline polls this source and triggers on every push. A GitHub OAuth connection (or a GitHub App connection) must be established in the target AWS account for this to work.
commands
string[]
An ordered array of shell commands executed inside the CodeBuild environment:
npm ci          # Clean install of exact dependency versions from package-lock.json
npm run build   # Compiles TypeScript to JavaScript via tsc
npx cdk synth   # Synthesizes all CDK stacks into CloudFormation templates in cdk.out/

App Entry Point

The CDK app is bootstrapped in bin/aws-cdk-pipeline.ts, which instantiates the stack with a hard-pinned env:
aws-cdk-pipeline/bin/aws-cdk-pipeline.ts
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
//import * as cdk from '@aws-cdk/core';
import { AwsCdkPipelineStack } from '../lib/aws-cdk-pipeline-stack';

const app = new cdk.App();
new AwsCdkPipelineStack(app, 'AwsCdkPipelineStack', {
  /* If you don't specify 'env', this stack will be environment-agnostic.
   * Account/Region-dependent features and context lookups will not work,
   * but a single synthesized template can be deployed anywhere. */

  /* Uncomment the next line to specialize this stack for the AWS Account
   * and Region that are implied by the current CLI configuration. */
  // env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },

  /* Uncomment the next line if you know exactly what Account and Region you
   * want to deploy the stack to. */
  env: { account: '602143770054', region: 'ap-south-1' },

  /* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});
app.synth();
Pinning env to a specific account and region (602143770054 / ap-south-1) is required when using CDK Pipelines — environment-agnostic stacks cannot use constructs that perform account/region lookups at synth time. The app.synth() call at the end triggers CloudFormation template generation when the cdk synth command runs inside the pipeline’s CodeBuild action.
The stack ID passed to the constructor — 'AwsCdkPipelineStack' — becomes the CloudFormation stack name. You must use this exact name when running the initial bootstrap deployment:
cdk deploy AwsCdkPipelineStack
After the first manual deploy, the pipeline takes over and self-mutates on every subsequent commit. You should only need to run cdk deploy manually once (or after breaking changes to the pipeline itself).

Build docs developers (and LLMs) love