Connect with us

AWS

Introduction to AWS CDK (Using TypeScript)

This tutorial assumes you have installed and configured AWS CLI and you can execute basic aws-cli commands.

You can refer the below links to read more about the AWS CLI:

  1. Installation: Installing, updating, and uninstalling the AWS CLI – AWS Command Line Interface (amazon.com)
  2. Configure CLI: Configuration basics – AWS Command Line Interface (amazon.com)

1. Verify NodeJS is installed

% node --version
v16.3.0

% npm --version
7.15.1

2. Install AWS CDK NPM Module

% sudo npm install -g aws-cdk

added 180 packages, and audited 181 packages in 4s

found 0 vulnerabilities

% cdk --version
1.108.1 (build ae24d8a)

3. Init the repository

work % mkdir cdk-typescript-example
work % cd cdk-typescript-example
cdk-typescript-example %

4. Init CDK

cdk init sample-app --language typescript
cdk-typescript-example % cdk init sample-app --language typescript
Applying project template sample-app for typescript
# Welcome to your CDK TypeScript project!

You should explore the contents of this project. It demonstrates a CDK app with an instance of a stack (`CdkTypescriptExampleStack`)
which contains an Amazon SQS queue that is subscribed to an Amazon SNS topic.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

## Useful commands

 * `npm run build`   compile typescript to js
 * `npm run watch`   watch for changes and compile
 * `npm run test`    perform the jest unit tests
 * `cdk deploy`      deploy this stack to your default AWS account/region
 * `cdk diff`        compare deployed stack with current state
 * `cdk synth`       emits the synthesized CloudFormation template

Initializing a new git repository...
Executing npm install...
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
 All done!
cdk-typescript-example % 


cdk-typescript-example % ls
README.md		jest.config.js		package-lock.json	tsconfig.json
bin			lib			package.json
cdk.json		node_modules		test
cdk-typescript-example % 

5. Generate CloudFormation Template (using cdk Synth)

cdk synth
cdk-typescript-example % cdk synth
Resources:
  CdkTypescriptExampleQueue5450531E:
    Type: AWS::SQS::Queue
    Properties:
      VisibilityTimeout: 300
    UpdateReplacePolicy: Delete
    DeletionPolicy: Delete
    Metadata:
      aws:cdk:path: CdkTypescriptExampleStack/CdkTypescriptExampleQueue/Resource

.
.
.
.
.
      - Fn::Or:
          - Fn::Equals:
              - Ref: AWS::Region
              - us-west-1
          - Fn::Equals:
              - Ref: AWS::Region
              - us-west-2

6. Bootstrap the environment

cdk bootstrap
% cdk bootstrap
   Bootstrapping environment aws://526470662813/us-east-1...
CDKToolkit: creating CloudFormation changeset...
[██████████████████████████████████████████████████████████] (3/3)



   Environment aws://526470662813/us-east-1 bootstrapped.
cdk-typescript-example % 

Make sure you have configured the AWS CLI, valid AWS token and the user has permissions to CloudFormation and create S3 bucket. Otherwise you will get access denied exception.

7. Deploy

cdk deploy

Type ‘Y’ to deploy the stack.

8. Verify the resources are deployed

aws sns list-topics --region us-east-1

aws sqs list-queues --region us-east-1 

9. Cleanup

cdk destroy
Continue Reading

Trending

Copyright © 2021 Rajan Panneer Selvam. Some of the content is derived from publically available information. For some of the resources I have obtained commercial licenses and you cannot use them in your projects. Before reusing any of the site content, please double-check for copyright issues. I am not responsible if you are infringing copyrights.