DEV Community

Cover image for Speed up AWS CDK deploys up to 80%
Miguel A. Calles
Miguel A. Calles

Posted on • Originally published at aws.plainenglish.io

Speed up AWS CDK deploys up to 80%

TL;DR

The ts-node package needed the speed boost


Support me by reading this post on Medium.


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.

npm i -D @swc/core @swc/helpers regenerator-runtime
Enter fullscreen mode Exit fullscreen mode

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.

{
  "ts-node": {
    "swc": true
  }
}
Enter fullscreen mode Exit fullscreen mode

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:

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

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

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

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 rather than installing Node.js directly onto my machine.

cd ~
mkdir cdkapps
Enter fullscreen mode Exit fullscreen mode

The first test

We will create a new CDK app.

cd ~/cdkapps
mkdir test1-cdk
cd test1-cdk
npx cdk init app --language typescript
Enter fullscreen mode Exit fullscreen mode

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

time npm run cdk synth
Enter fullscreen mode Exit fullscreen mode

The command sent this output:

real 0m38.681s
user 0m46.195s
sys 0m1.769s
Enter fullscreen mode Exit fullscreen mode

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.)

time npm run cdk synth
Enter fullscreen mode Exit fullscreen mode

The command sent this output:

real 0m9.562s
user 0m9.105s
sys 0m0.703s
Enter fullscreen mode Exit fullscreen mode

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.

cd ~/cdkapps
mkdir test2-cdk
cd test2-cdk
npx cdk init app --language typescript
Enter fullscreen mode Exit fullscreen mode

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

npm i -D @types/aws-lambda
Enter fullscreen mode Exit fullscreen mode

Let’s create the Lambda function.

touch function.ts
Enter fullscreen mode Exit fullscreen mode

Let’s create the function code.

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

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

Let’s create the function resource.

/* ~/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'),
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

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

time npm run cdk synth
Enter fullscreen mode Exit fullscreen mode

The command sent this output:

real 0m49.314s
user 0m58.874s
sys 0m3.818s
Enter fullscreen mode Exit fullscreen mode

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.)

time npm run cdk synth
Enter fullscreen mode Exit fullscreen mode

The command sent this output:

real 0m18.985s
user 0m18.625s
sys 0m2.186s
Enter fullscreen mode Exit fullscreen mode

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

Stop Installing Node.js and Global Npm Packages, Use Docker Instead: Protect your system from vulnerabilities

Cloudflare v. AWS: A Comparison of Their Serverless Offerings: They both have their benefits and downsides

How I Caused an Amazon API Gateway Denial of Service: The DoS attack was caused by a Lambda function


Photo by PAUL SMITH on Unsplash

Top comments (0)