How to Deploy your Application to AWS using CDK Pipelines

aws
Reading Time: 4 minutes

Hello Readers! In this blog we will see how easily we can deploy our applications to AWS using CDK Pipelines. With this pipeline we will be able to test, build and deploy our application. If you are not familiar with CDK do check my previous blog: https://blog.knoldus.com/how-to-create-a-custom-vpc-using-aws-cdk/

Prerequisites

Before getting started we have some steps we need to follow these are:

  1. You must have installed AWS CLI.
  2. You must have configured credentials with AWS.
  3. Install AWS CDK.

For installing it use the following command:

$ npm install -g aws-cdk
cdk version
  1. Make one repository in your github account and clone it on your local.

Let’s get started!

Step1: Firstly I will create a folder for the project and inside this I will initialize my project.

$ cd aws-cdk-pipeline 
$ cdk init app --language typescript
aws cdk

Step2: Move to your /bin/aws-cdk-pipeline.ts and modify your account id and region inside the environment.

import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { AwsCdkPipelineStack } from '../lib/aws-cdk-pipeline-stack';
const app = new cdk.App();
new AwsCdkPipelineStack(app, 'AwsCdkPipelineStack', {
      env: { account: '044698251221', region: 'ap-south-1' },
});

Now inside /lib/aws-cdk-pipeline-stack.ts define your pipeline.You can change here the pipeline name as per your wish. And don’t forget to change the input inside the function and give your repository name.

import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as cdk from 'aws-cdk-lib';
import { CodePipeline, CodePipelineSource, ShellStep } from 'aws-cdk-lib/pipelines';
import { ManualApprovalStep } from 'aws-cdk-lib/pipelines';

export class AwsCdkPipelineStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);
    new CodePipeline(this, 'Pipeline', {
      pipelineName: 'CDKTestPipeline',       // Creating a new code pipeline which is a construct
      synth: new ShellStep('Synth', {        // Add a new synthesis 'shellstep' which will be pointed at our gihub repository 
        input: CodePipelineSource.gitHub('NaincyKumariKnoldus/aws-cdk-code-repo', 'main'), // replace the GitHub repository name with 'user-name/repository-name'
        
        // The build steps for the pipeline are defined by these commands
        
        commands: ['npm ci',
                   'npm run build',
                   'npx cdk synth']
      }),
    })
  }
}

Step3: Push your code to the github.

github

Step4: Now we will store our Github Personal Access Token inside the Secrets Manager in AWS.

For this Sign in to your AWS console and move to Secrets Manager. Click on store new secret, select other types of secrets and then paste your github token under plain text. Click on Next and give a secret name as per your wish.

AWS secrets

Step5: Before deploying bootstrap your project. Run the command for bootstrapping.

$ cdk bootstrap
cdk bootstrap

After the bootstrapping process is successful you will be able to see the CloudFormation stack created in your AWS account.

cdk

Step6: Now it’s time to deploy our project. Run the following command for deploying it. 

$ cdk deploy

It will ask yes to deploy these changes. So, Enter yes.

aws cdk

After some time this process will be completed.

cdk deploy

If the deployment is successful you can see the completed CloudFormation stack and the pipeline created. It will contain three stages inside the pipeline: Source, Build, Update Pipeline.

These are the stages that you will find inside CDK pipeline:

CDK stages

So, now your pipeline is totally ready. You can commit the changes inside your repository and can see the changes inside the pipeline also. For example if you add any stages inside your code then you will observe one more stage added inside your pipeline also.

This is a very basic implementation that we have performed in this blog of AWS Codepipeline with GitHub to your CDK project.

Congrats! 👏 We are successfully done now!

Conclusion

Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. 

HAPPY LEARNING!

knoldus

Written by 

Naincy Kumari is a DevOps Consultant at Knoldus Inc. She is always ready to learn new technologies and tools. She loves painting and dancing.

Leave a Reply