Project Structure

NOTE: typically having a service’s code in this dir will mean that you have to create a dir in the root of the project dir, you will call this what the name of the service is

Your app’s entry point

Let’s have a quick look at bin/cdk-workshop.ts:

#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { CdkWorkshopStack } from '../lib/cdk-workshop-stack';

const app = new cdk.App();
new CdkWorkshopStack(app, 'CdkWorkshopStack');

This code loads and instantiates the CdkWorkshopStack class from the lib/cdk-workshop-stack.ts file. We won’t need to look at this file anymore.

The main stack #

Open up lib/cdk-workshop-stack.ts. This is where the meat of our application is:

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

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

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

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

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

As you can see, our app was created with a sample CDK stack (CdkWorkshopStack).

The stack includes: