Speed up AWS CDK deploys up to 80%
Tags: aws, typescript, node, devops
Published: 2023-07-16 14:21:24

## TL;DR

The ts-node package needed the speed boost

---

Support me by reading [this post on Medium](https://aws.plainenglish.io/speed-up-aws-cdk-deploys-up-to-80-c47afad1c18c).

---

## Introduction

The Amazon Web Services Cloud Development Kit is an infrastructure-as-code solution. CDK allows us to write code (in JavaScript, TypeScript, Python, for example) to programmatically deploy resources in our AWS account.

CDK is written in TypeScript and uses the ts-node package to deploy. When we run the cdk command in the terminal, the npm package uses the ts-node package to transpile the CDK code before running it.

## The issue

I was finding it was taking “forever” when running a `cdk deploy` or `cdk synth` command. When I was troubleshooting or developing code, I wanted faster deploys. I was using the `cdk watch` command to hotswap changes during development. This was a “little” faster but still felt super slow. I wanted to find out why these CDK commands were taking so long.

## This was the solution

I will share the solution upfront. The rest of the post will share my findings.

I installed a new ts-node transpiler using npm.

```sh
npm i -D @swc/core @swc/helpers regenerator-runtime
```

I updated my `tsconfig.json` file to tell ts-node to use the new SWC transpiler. I added the `ts-node` property and the `swc` property under it.

```json
{
  "ts-node": {
    "swc": true
  }
}
```

I noticed a significant improvement when running `cdk` and `npx ts-node` commands in the terminal.

Keep in mind this note from the [SWC website](https://swc.rs/docs/migrating-from-tsc):

> SWC only transpiles the code and doesn’t perform type checking. Therefore, it’s recommended that you continue to use tsc for detecting any type errors.

## How I found the solution

I realized that ts-node could be the issue. I write command line scripts with TypeScript. I use the `npx ts-node myfile.ts` command to execute them. They took a while to execute but did not think much about it. When I was getting frustrated by the CDK command start times, I started to realize the TypeScript script execution time was slow too.

I decided to investigate if they were speed issues with the ts-node package.

I found a GitHub issue where others expressed their concerns with the slowness of ts-node.

[https://github.com/TypeStrong/ts-node/issues/1070](https://github.com/TypeStrong/ts-node/issues/1070)

The discussion taught me about the SWC transpiler that can speed up the time ts-node uses.

[https://typestrong.org/ts-node/docs/swc/](https://typestrong.org/ts-node/docs/swc/)

I installed the SWC packages and updated my `tsconfig.json` file. I noticed a significant improvement when using `cdk deploy`, `cdk synth`, `cdk watch`, and `npx ts-node` commands in the terminal. Development time was much faster now.

I updated my CI/CD pipeline that deploys multiple CDK apps and runs various TypeScript command line scripts. My CI/CD pipeline execution time dropped from 345 seconds to 74 seconds when I added the ts-node SWC setting. Wow! That’s over 75%.

I needed to share this finding with the CDK community. I submitted a GitHub pull request to add the SWC settings to the TypeScript app template.

[https://github.com/aws/aws-cdk/pull/25089](https://github.com/aws/aws-cdk/pull/25089)

If the pull request is approved and merged, new CDK apps created by the cdk init command will get this speed boost.

## Empirical testing

Let’s do some testing to see the effects of the SWC transpiler.

Let’s create a folder for our CDK apps. We will use the ~/cdkapps folder. Note: My setup [uses Docker](https://medium.com/better-programming/stop-installing-node-js-and-global-npm-packages-use-docker-instead-42597990db13) rather than installing Node.js directly onto my machine.

```sh
cd ~
mkdir cdkapps
```

## The first test

We will create a new CDK app.

```sh
cd ~/cdkapps
mkdir test1-cdk
cd test1-cdk
npx cdk init app --language typescript
```

Let’s test the time it takes to synthesize an empty app.

```sh
time npm run cdk synth
```

The command sent this output:

```sh
real 0m38.681s
user 0m46.195s
sys 0m1.769s
```

It took 46 seconds to synthesize a CDK app.

Now let’s test with the SWC transpiler. (The command assumes the steps to add the SWC transpiler were already done.)

```sh
time npm run cdk synth
```

The command sent this output:

```sh
real 0m9.562s
user 0m9.105s
sys 0m0.703s
```

It took 9 seconds to synthesize a CDK app when using SWC. That’s an 80% improvement!

## The second test

Now let’s create a new CDK app with one TypeScript Lambda function.

We will create a new CDK app.

```sh
cd ~/cdkapps
mkdir test2-cdk
cd test2-cdk
npx cdk init app --language typescript
```

Let’s add the packages to help with adding a Lambda function.

```sh
npm i -D @types/aws-lambda
```

Let’s create the Lambda function.

```sh
touch function.ts
```

Let’s create the function code.

```ts
/* ~/cdkapps/test2-cdk/function.ts */
import { Handler } from 'aws-lambda';

export const handler: Handler<Event> = async function(event, _context) {
  return;
}
```

Let’s create the function resource.

```ts
/* ~/cdkapps/test2-cdk/lib/test2-cdk-stack.ts */
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as path from 'path';

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

    new cdk.aws_lambda_nodejs.NodejsFunction(this, 'function', {
      functionName: 'function',
      handler: 'handler',
      runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
      entry: path.resolve('__dirname', '..', 'function.ts'),
    });
  }
}
```

Let’s test the time it takes to synthesize an app on a bare TypeScript Lambda function.

```sh
time npm run cdk synth
```

The command sent this output:

```sh
real 0m49.314s
user 0m58.874s
sys 0m3.818s
```

It took 58 seconds to synthesize a CDK app.

Now let’s test with the SWC transpiler. (The command assumes the steps to add the SWC transpiler were already done.)

```sh
time npm run cdk synth
```

The command sent this output:

```sh
real 0m18.985s
user 0m18.625s
sys 0m2.186s
```

It took 18 seconds to synthesize a CDK app when using SWC. That’s a 67% improvement.

## Conclusion

Consider adding the SWC transpiler for ts-node to your AWS CDK apps. The time results will vary depending on your setup.

## Before you go

Here are other posts you might enjoy.

[AWS CDK Serverless Cookbook: A Step-by-step Guide to Build a Serverless App in the Cloud](https://medium.com/chapter-by-chapter/aws-cdk-serverless-cookbook-ebook-1d4d4e0488c)

[Stop Installing Node.js and Global Npm Packages, Use Docker Instead: Protect your system from vulnerabilities](https://betterprogramming.pub/stop-installing-node-js-and-global-npm-packages-use-docker-instead-42597990db13)

[Cloudflare v. AWS: A Comparison of Their Serverless Offerings: They both have their benefits and downsides](https://aws.plainenglish.io/cloudflare-v-aws-a-comparison-of-their-serverless-offerings-a32e2a000735)

[How I Caused an Amazon API Gateway Denial of Service: The DoS attack was caused by a Lambda function](https://aws.plainenglish.io/how-i-caused-an-amazon-api-gateway-denial-of-service-cba93810e18f)

---

Photo by PAUL SMITH on Unsplash