Deploy application

Bootstrap CDK in Your Account

  1. Copy this command.
npx cdk bootstrap aws://ACCOUNT-NUMBER/REGION
  1. Replace ACCOUNT-NUMBER with your Account ID.
  2. Replace REGION with your Region ID
  3. Paste and run it in terminal.

Create CDK Infrastructure 5. After bootstraping, go to CloudFormation. You will see a stack name CDKToolkit had been provisioned.

Create CDK Infrastructure

Build and Deploy the CDK Application

After we have bootstrapped our AWS account and Region, we are ready to build and deploy our CDK application.

  1. At terminal of Cloud9 environment, build the CDK application.
npm run build

Create CDK Infrastructure

  1. If there are no errors in our application, this will succeed. We can now push all the code to the GitHub repository.
git add .
git commit -m "empty pipeline"
git push

Create CDK Infrastructure

Input the GitHub username and access token if necessary.

  1. Deploy the CDK application in AWS.

npx cdk deploy

Enter Y if was requested.

Create CDK Infrastructure

  1. After CDK application was deployed in AWS successfully, a CloudFormation name CdkPipelineStack will be created.
  2. Go to Stack of CloudFormation to check the result.

Create CDK Infrastructure

  1. This CloudFormation stack will create an Empty CodePipeline name MyServicePipeline.
  2. Go to CodePipeline to see your pipeline.

Create CDK Infrastructure

  1. Click to pipeline to see stages.

Create CDK Infrastructure

Add a Deploy Stage for Elastic Beanstalk Environment

So far, we have provisioned an empty pipeline, and the pipeline isn’t deploying our web application yet. Now, we will create a stage to deploy application to Elastic Beanstalk.

  1. Create a new file lib/eb-stage.ts.
  2. Put the following code in it:
import {  Stage } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { EBEnvProps, EBApplnStack } from './eb-appln-stack';

/**
* Deployable unit of web service app
*/
export class CdkEBStage extends Stage {
    
constructor(scope: Construct, id: string, props?: EBEnvProps) {
    super(scope, id, props);

    const service = new EBApplnStack(this, 'WebService', {
        minSize : props?.minSize, 
        maxSize : props?.maxSize,
        instanceTypes : props?.instanceTypes,
        envName : props?.envName
    } );

}
}

Create CDK Infrastructure

  1. Add a new import line at the top of lib/cdk-pipeline-stack.ts.
import { CdkEBStage } from './eb-stage';
  1. Add the following code after the mentioned comment.
// deploy beanstalk app
// For environment with all default values:
// const deploy = new CdkEBStage(this, 'Pre-Prod');

// For environment with custom AutoScaling group configuration
const deploy = new CdkEBStage(this, 'Pre-Prod', { 
    minSize : "1",
    maxSize : "2"
});
const deployStage = pipeline.addStage(deploy); 

Create CDK Infrastructure

  1. Run npm run build and push source code to GitHub repository.
npm run build
git add .
git commit -m 'Add Pre-Prod stage'
git push

Create CDK Infrastructure