AWS-CDK behind a proxy

Introduction

As is, AWS-CDK can not provide a proper way to work with both the proxy and certificates configuration, so in case we are behind a corporate proxy, we need to hack it.

Setup

  1. Install AWS-CDK as a global package, and check it was properly installed.

    1
    2
    npm install aws-cdk -g
    cdk --version
  2. You will need to go to the node_modules folder (example on an NVM installation: C:\NVM_1.1.7\v14.16.1\node_modules\aws-cdk\lib\api\aws-auth\sdk-provider.js):

  3. You will need to make the following changes, on switching the function parseHttpOptions on line 263:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    function parseHttpOptions(options) {
    var _a;
    const config = {};
    config.httpOptions = {};
    let userAgent = options.userAgent;
    if (userAgent == null) {
    // Find the package.json from the main toolkit
    const pkg = JSON.parse((_a = readIfPossible(path.join(
    __dirname, '..', '..', '..', 'package.json'))
    ) !== null && _a !== void 0 ? _a : '{}');
    userAgent = `${pkg.name}/${pkg.version}`;
    }
    config.customUserAgent = userAgent;
    const proxyAddress = options.proxyAddress || httpsProxyFromEnvironment();
    const caBundlePath = options.caBundlePath || caBundlePathFromEnvironment();

    if (proxyAddress && caBundlePath) {
    logging_1.debug('Using proxy server: %s', proxyAddress);
    logging_1.debug('Using CA bundle path: %s', caBundlePath);
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const ProxyAgent = require('proxy-agent');

    //some extra processing for splitting proxyAddress
    _p_address = proxyAddress.replace('//', "").split(":");
    _protocol = 'http';
    _address = 'localhost';
    _port = '8080';

    if (_p_address.legth < 0 || _p_address.length > 4){
    throw new Error('Invalid proxy address');
    }

    if (_p_address.legth === 1){
    _address = _p_address[0];
    }

    if (_p_address.legth === 2){
    if (_p_address[0].startsWith('http')){
    _protocol = _p_address[0];
    _address = _p_address[1];
    } else{
    _address = _p_address[0];
    _port = _p_address[1];
    }
    }

    if (_p_address.legth === 3){
    _protocol = _p_address[0];
    _address = _p_address[1];
    _port = _p_address[2];
    }

    // and set it
    config.httpOptions.agent = new ProxyAgent({
    protocol: _protocol + ':',
    slashes: true,
    auth: null,
    host: _address + ':' + _port,
    port: _port,
    hostname: _address_,
    hash: null,
    search: null,
    query: null,
    pathname: '/',
    path: '/',
    href: proxyAddress,
    ca: readIfPossible(caBundlePath),
    keepAlive: true,
    });
    } else {
    if (proxyAddress) { // Ignore empty string on purpose
    logging_1.debug('Using proxy server: %s', proxyAddress);
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const ProxyAgent = require('proxy-agent');
    config.httpOptions.agent = new ProxyAgent(proxyAddress);
    }
    if (caBundlePath) {
    logging_1.debug('Using CA bundle path: %s', caBundlePath);
    config.httpOptions.agent = new https.Agent({
    ca: readIfPossible(caBundlePath),
    keepAlive: true,
    });
    }
    }
    return config;
    }

Run it

Terminal configuration on Windows CMD

  1. You will need to work on CMD (stock). Add the NodeJS variable, then you can run AWS-CDK commands.

    1
    SET PATH=C:\NVM_1.1.7\v14.16.1;%PATH%
  2. Launch a CDK command.

    1
    cdk init sample-app --language=typescript

Terminal configuration via wrapper

  1. Your PATH must contain the following variables.

    1
    PATH=C:\NVM_1.1.7;C:\NVM_1.1.7\v14.16.1;C:\Program Files\Amazon\AWSCLI\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\OpePATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW
  2. Launch a CDK command.

    1
    cdk init sample-app --language=typescript

Terminal configuration on code workspaces

  1. If you are using a Visual Studio Code code-workspace, then you should specify it.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    {
    "folders": [
    {
    "path": ".."
    }
    ],
    "settings": {
    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
    "terminal.integrated.env.windows": {
    "LC_ALL": "C.UTF-8",
    "PATH": "C:/NVM_1.1.7;C:/NVM_1.1.7/v14.16.1;C:/Program Files/Amazon/AWSCLI/bin;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;C:/WINDOWS/System32/OpePATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW"
    },
    "terminal.integrated.minimumContrastRatio": 7,
    }
    }
  2. Launch a CDK command.

    1
    cdk init sample-app --language=typescript