From 307c7ebbc93058fb6248eff661148fde9879621a Mon Sep 17 00:00:00 2001 From: dveldhoen <4952924+dveldhoen@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:52:00 +0100 Subject: [PATCH] fix(cli): reuse HTTPS connections for AWS SDK calls The CLI's ProxyAgent replaces the AWS SDK's default agent, which keeps connections alive, with one that does not, so every SDK call opened a new TLS connection. Pass keepAlive: true to the ProxyAgent. --- packages/aws-cdk/lib/cli/proxy-agent.ts | 4 +++ packages/aws-cdk/test/cli/proxy-agent.test.ts | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/packages/aws-cdk/lib/cli/proxy-agent.ts b/packages/aws-cdk/lib/cli/proxy-agent.ts index bc390ed9a..35cef9551 100644 --- a/packages/aws-cdk/lib/cli/proxy-agent.ts +++ b/packages/aws-cdk/lib/cli/proxy-agent.ts @@ -138,6 +138,10 @@ export class ProxyAgentProvider { agent: new ProxyAgent({ ca: await this.tryReadCABundle(caBundlePath), getProxyForUrl, + // This agent replaces the AWS SDK's default one, which keeps connections alive. Without + // keep-alive, every SDK call opens a new TLS connection, which makes commands that send + // many calls, such as `cdk gc`, very slow. + keepAlive: true, }), caBundlePath, }; diff --git a/packages/aws-cdk/test/cli/proxy-agent.test.ts b/packages/aws-cdk/test/cli/proxy-agent.test.ts index d3e32d679..598c26c2a 100644 --- a/packages/aws-cdk/test/cli/proxy-agent.test.ts +++ b/packages/aws-cdk/test/cli/proxy-agent.test.ts @@ -1,3 +1,5 @@ +import * as http from 'node:http'; +import type { AddressInfo } from 'node:net'; import * as os from 'node:os'; import * as path from 'node:path'; import * as fs from 'fs-extra'; @@ -49,6 +51,29 @@ describe('ProxyAgentProvider', () => { const provider = new ProxyAgentProvider(ioHost.asHelper('deploy')); await expect(provider.create({ proxyAddress })).resolves.toBeDefined(); }); + + test('create() returns an agent that reuses connections', async () => { + let connections = 0; + const server = http.createServer((_req, res) => res.end('ok')); + server.on('connection', () => connections++); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + + try { + // An empty proxy address connects directly, whatever the proxy environment variables say. + const { agent } = await new ProxyAgentProvider(ioHost.asHelper('deploy')).create({ proxyAddress: '' }); + const url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`; + for (let i = 0; i < 2; i++) { + await new Promise((resolve, reject) => { + http.get(url, { agent }, (res) => res.resume().on('end', resolve)).on('error', reject); + }); + } + + expect(connections).toBe(1); + } finally { + server.closeAllConnections(); + await new Promise((resolve) => server.close(resolve)); + } + }); }); describe('CA bundle resolution', () => {