Skip to main content

CDK Arch

This library aims at providing a way to autogenerate architectural diagrams for CDK projects.

This library is written using the wonderful projen framework.

Installation

The library is available on npmjs.com and can be installed using:

npm i cdk-arch

And on pypi:

pip install cdk-arch

Usage Examples

As an example, let's consider the following CDK app:

import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';

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

    const bucket = new s3.Bucket(this, 'Bucket');
    const lfunction = new lambda.Function(this, 'Lambda', {
        ...
    });
  }
}

const app = new cdk.App();
new ExampleStack(app, 'ExampleStack', {
env: {
    account: process.env.CDK_DEFAULT_ACCOUNT,
    region: 'eu-west-1',
},
});

and imagine that in the diagram we want to show the bucket, the lambda function and an arrow connecting the two. This can be accomplished by changing the code as follows:

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

    const bucket = new s3.Bucket(this, 'Bucket');
    const lfunction = new lambda.Function(this, 'Lambda', {
      runtime: lambda.Runtime.NODEJS_20_X,
      handler: 'index.handler',
      code: lambda.Code.fromInline(`
        exports.handler = async function(event) {
          console.log('event', event);
          return {
            statusCode: 200,
            body: JSON.stringify({ message: 'Hello from Lambda!' }),
          };
        };
      `),
      environment: {
        BUCKET_NAME: bucket.bucketName,
      },
    });

    // We add some metadata to the resources to be able to position them in the diagram
    bucket.node.addMetadata('CDKArch Element', { x: 0, y: 0 });
    lfunction.node.addMetadata('CDKArch Element', { x: 300, y: 0 });
    // We add a connection between the bucket and the lambda function
    bucket.node.addMetadata('CDKArch Connection', { startId: bucket.node.id, endId: lfunction.node.id });

  }
}

const app = new cdk.App();
new ExampleStack(app, 'ExampleStack', {
    env: {
    account: process.env.CDK_DEFAULT_ACCOUNT,
    region: 'eu-west-1',
    },
});

const sketchbuilder = new SketchBuilder();
Aspects.of(app).add(sketchbuilder);

app.synth();
sketchbuilder.exportToFile(filepath);

where we added some metadata to the resources to instruct the SketchBuilder to position two icons in the diagram and a connection between the two, and we added an aspect to the app to instruct the SketchBuilder to generate it when the app is synthetized.

This is the resulting diagram:

Example Diagram

Customizing the diagram

Positioning the resources

The position of the resources in the diagram can be customized using the 'x' and 'y' fields in the metadata of the node:

bucket.node.addMetadata('CDKArch Element', { x: 100, y: 200 });

Name of the resouces

By default, the name of the resources is the id of the node. This can be customized by adding a 'text' field to the metadata of the node:

bucket.node.addMetadata('CDKArch Element', { x: 100, y: 200, text: 'Overriding the bucket name' });

Contributors

Matteo Giani matteo.giani.87@gmail.com

Metadata

Release files for cdk-arch 0.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cdk-arch 0.0.0
File Size Uploaded
cdk-arch-0.0.0.tar.gz 165.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cdk-arch 0.0.0
File Interpreter ABI Platform
cdk_arch-0.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 328.9 kB

Release files / cdk-arch-0.0.0.tar.gz

Download URL cdk-arch-0.0.0.tar.gz
Size 165.3 kB
Tags Source
SHA-256 checksum
How to use checksums
3743b5763d3953861e6cbec3ca2c553dc0d9a02cf13aafdcf1039df05984c361
BLAKE2b-256 checksum
How to use checksums
df0383886b1e975d8e4be4cfe76ef27974b4376f64174206c58dcaa1b519f7be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / cdk_arch-0.0.0-py3-none-any.whl

Download URL cdk_arch-0.0.0-py3-none-any.whl
Size 163.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
59d69f84a14524e4fb0c2d6bd27573d397c35d2b9d1ef051b504fa10e977c725
BLAKE2b-256 checksum
How to use checksums
ddfd44680270a25ca1c3e88697c7add9259e8a98203b5ed58239b614a5fb6648
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page