DEV Community

loading...
Cover image for AWS CDK file system

AWS CDK file system

hariraghupathy profile image Hari Krishnan Updated on ・3 min read

The AWS CDK gives us a boilerplate with the following file system :

|-- bin
    |-- /project-name.d.ts
    |-- /project-name.js
    |-- /project-name.ts
|-- lib
    |-- /project-name-stack.d.ts
    |-- /project-name-stack.js
    |-- /project-name-stack.ts
|-- node_modules/
|-- test
    |-- /project-name.test.d.ts
    |-- /project-name.test.js
    |-- /project-name.test.ts
|-- cdk.json
|-- package.json
|-- package-lock.json
|-- tsconfig.json
Enter fullscreen mode Exit fullscreen mode

lib/project-name-stack.ts

This is where your CDK application's main stack is defined. We will be spending most of our time in this file.

bin/project-name.ts

This is the entry point of the CDK application. This will load the application stack defined in the lib/project-name-stack.ts

cdk.json

It tells the toolkit how to run your app. In our case it will be "npx ts-node bin/project-name.ts"

package.json && package-lock.json

All the dependencies required for your CDK application are defined in this file. When you run npm install in your project directory, all the modules listed here will be installed. Build scripts are also defined here in this file. The package-lock.json keeps track of what already has been installed.

tsconfig.json

This file contains the typescript configuration. The presence of a tsconfig.json file in a directory indicates that the directory is the root of a typescript project. This file specifies the root files and the compiler options required to compile the project in this directory.

Entry point of the application

As already said the entry point of the application is bin/project-name.ts. This code instantiates the project's base stack class from the lib/project-name-stack.ts. This just loads and initializes the application stack and we don't have to look at this file anymore.

Note: ProjectName mentioned in the entire page refers to the name of the aws cdk project you created. Here the name of my project is project-name.

import * as cdk from '@aws-cdk/core';
import { ProjectNameStack } from '../lib/project-name-stack';

const app = new cdk.App();
new ProjectNameStack(app, 'ProjectNameStack');
Enter fullscreen mode Exit fullscreen mode

Main application stack

Open the lib/project-name-stack.ts file, this is the place where the main stack of the application is defined.

import * as sns from '@aws-cdk/aws-sns';
import * as subs from '@aws-cdk/aws-sns-subscriptions';
import * as sqs from '@aws-cdk/aws-sqs';
import * as cdk from '@aws-cdk/core';

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

    const queue = new sqs.Queue(this, 'ProjectNameQueue', {
      visibilityTimeout: cdk.Duration.seconds(300)
    });

    const topic = new sns.Topic(this, 'ProjectNameTopic');

    topic.addSubscription(new subs.SqsSubscription(queue));
  }
}

Enter fullscreen mode Exit fullscreen mode

This file includes SQS and SNS by default when we create our first CDK project with the sample-app template. 

1. SQS queue

SQS is a distributed queuing system where messages are not directly pushed to the receivers, the receivers have to poll or pull the messages. The messages can't be received by multiple receivers at the same time. When a message is received by a receiver, it is processed and deleted from the queue. Others will not have access to this message later. It decouples the sending and receiving components, without requiring each component to be concurrently available.

2. SNS topic

SNS is distributed publish/subscribe system. We create a pub/sub topic for our cdk application. It will act as a common access point where information will be published, and applications or endpoints which have subscribed to this topic will get notified about this information.

3.Coupling SQS and SNS

In most cases SNS and SQS are not coupled. There are advantages when coupling SNS with SQS, consider the scenario where some would require to immediately receive the messages and some would require message delivery only after requesting (polling) . Example: users listening to push notifications can be done through SNS, whereas an application or service or a cron job which would require data only when it is up and running will get the data from the message queue by polling.

When we subscribe an Amazon SQS queue to an SNS topic, you can publish a message to the topic, and the topic sends an Amazon SQS message to the SQS queue.

Discussion (0)

pic
Editor guide