AWS CDK — Top 5 suggestions from my toolbox

Arvind Telharkar
Level Up Coding
Published in
4 min readJan 15, 2023

--

AWS Cloud Development Kit (CDK) is a powerful tool for creating and managing cloud infrastructure. It allows developers to use familiar programming languages, such as TypeScript, to define their infrastructure as code and deploy it to the AWS cloud. Here are 5 practical suggestions from my toolbox while using AWS CDK to optimize infrastructure and streamline the development process —

Photo by Alexander Schimmeck on Unsplash

1. Stacks for modular infrastructure

Use Stacks for modular and reusable infrastructure. One of the best practices in infrastructure as code is to create modular and reusable code. AWS CDK allows you to achieve this by using the cdk.Stack class. You can create multiple stacks, each one responsible for a specific set of resources.

For example, if you have a microservices architecture you could create a stack for each service, that way you can manage the service’s infrastructure independently.

import * as cdk from 'aws-cdk-lib';

const app = new cdk.App();
const stack1 = new cdk.Stack(app, 'Service1Stack', { env: { region: 'us-east-1' } });
const stack2 = new cdk.Stack(app, 'Service2Stack', { env: { region: 'us-east-1' } });

const service1Lambda = new lambda.Function(stack1, 'Service1Lambda', {
code: new lambda.AssetCode('path/to/service1/code'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_12_X,
});

const service2Lambda = new lambda.Function(stack2, 'Service2Lambda', {
code: new lambda.AssetCode('path/to/service2/code'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_12_X,
});

You can easily manage the infrastructure of each service independently, update them separately, and also use the same stack in different environments like staging and production.

2. Separate configuration with environment variables

Use environment variables to separate configuration between environments. Using environment variables can be a best practice when it comes to separating configuration between different environments, such as staging and production. CDK allows you to easily create environment-specific configuration by using the cdk.Environment class.

import * as cdk from 'aws-cdk-lib';

const stagingEnv = { account: '123456789', region: 'us-west-2' };
const prodEnv = { account: '987654321', region: 'us-east-1' };

const stack = new cdk.Stack(app, 'MyStack', { env: stagingEnv });
const myLambda = new lambda.Function(stack, 'MyLambda', {
code: new lambda.AssetCode('path/to/staging/code'),
environment: {
STAGE: 'staging'
}
});

const prodStack = new cdk.Stack(app, 'MyProdStack', { env: prodEnv });
const myProdLambda = new lambda.Function(prodStack, 'MyProdLambda', {
code: new lambda.AssetCode('path/to/prod/code'),
environment: {
STAGE: 'production'
}
});ty

You can use these environment variables in your Lambda functions to change the behavior of your functions depending on the environment they are running in. For example, you can use the environment variable to connect to different databases depending on the environment.

You can also use the environment variable to configure other parts of your infrastructure, such as SNS topics or SQS queues, to ensure that the correct resources are being used in each environment.

3. Stack outputs

Use Stack Outputs to share data between stacks. Stack Outputs allow you to expose data from one stack to another, making it easy to share resources and configuration between different parts of your infrastructure. To use Stack Outputs, you can use the cdk.CfnOutput class in your TypeScript code.

Example:

import * as cdk from 'aws-cdk-lib';

const stack = new cdk.Stack(app, 'MyStack', { env: { region: 'us-east-1' } });
const myOutput = new cdk.CfnOutput(stack, 'MyOutput', {
value: 'Hello, World!',
});

4. cdk diff

The cdk diff command is a powerful tool when working with the AWS CDK. It allows you to see the differences between the current state of your stack and the proposed changes before deploying. This can be useful to understand what changes will be made, and to catch any potential errors before deploying.

Here are a few examples(not an exhaustive list) of how to use the cdk diff command:

  1. To see the difference between the current state of the stack and the proposed changes:
cdk diff

2. To see the difference between the current state of the stack and a specific version of the stack:

cdk diff --context version=<version>

3. To see the difference between the current state of the stack and the last deployed version:

cdk diff --context deployed

By using cdk diff command, you can quickly identify and understand the changes that will be made to your stack before deploying, which can help you to catch errors before they happen, and ensure that your infrastructure remains stable and predictable.

5. Prefer L2 constructs over L1

By using L2 constructs, you can write more readable and maintainable code, that abstracts away the low-level details of CloudFormation resources. This makes it easier to understand and reason about your infrastructure, and also reduces the risk of errors when working with complex resources.

It’s also important to note that L2 constructs are built on top of L1 constructs, so if you need more control or access to low-level functionality, you can always drop down to the L1 constructs.

Example:

SNS topic with L1 construct

import * as cdk from 'aws-cdk-lib';

const stack = new cdk.Stack(app, 'MyStack', { env: { region: 'us-east-1' } });
const snsTopic = new cdk.CfnTopic(stack, 'MyTopic', {
displayName: 'My SNS Topic',
});

SNS topic with L2 construct

import * as sns from '@aws-cdk/aws-sns';

const stack = new cdk.Stack(app, 'MyStack', { env: { region: 'us-east-1' } });
const snsTopic = new sns.Topic(stack, 'MyTopic', {
displayName: 'My SNS Topic',
});

Instead of using the low-level cdk.CfnTopic class, you can use the higher-level sns.Topic class from the @aws-cdk/aws-sns package, which provides a more intuitive interface for working with SNS topics.

There are lots more. If you want to connect with me, feel free to reach out on LinkedIn!

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

--

--