AWS CDK Asset Node.js Proxy Agent v5

2.0.166 · active · verified Thu Apr 16

The `aws-cdk-asset-node-proxy-agent-v5` library provides an AWS CDK Construct that configures Node.js-based assets (e.g., Lambda functions) to use an HTTP/HTTPS proxy during their build and bundling phases. This is critical for CDK deployments in corporate environments where internet access requires going through a proxy, leveraging the `proxy-agent` Node.js library. It is an experimental construct from `cdklabs` and is currently at version 2.0.166, aligning with AWS CDK v2 releases, with updates tied to CDK's release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `NodeProxyAgent` into your CDK application. It sets up an `NodeProxyAgent` instance to configure proxy settings for Node.js assets during synthesis, using environment variables for proxy URLs. A sample `NodejsFunction` is included, which will then use these proxy settings during its bundling process.

import os
from aws_cdk import App, Stack, Environment
from aws_cdk.aws_lambda_nodejs import NodejsFunction
from aws_cdk_asset_node_proxy_agent_v5 import NodeProxyAgent


class MyProxyStack(Stack):
    def __init__(self_stack, scope, id, **kwargs):
        super().__init__(scope, id, **kwargs)

        # Define proxy configuration - ensure these env vars are set in your build environment
        http_proxy = os.environ.get('HTTP_PROXY', '')
        https_proxy = os.environ.get('HTTPS_PROXY', '')
        no_proxy = os.environ.get('NO_PROXY', '')

        if http_proxy or https_proxy:
            # Instantiate the NodeProxyAgent construct
            NodeProxyAgent(self_stack, "NodeProxyAgent",
                           http_proxy=http_proxy,
                           https_proxy=https_proxy,
                           no_proxy=no_proxy
            )
            print(f"NodeProxyAgent configured with HTTP_PROXY: {http_proxy} and HTTPS_PROXY: {https_proxy}")
        else:
            print("No proxy environment variables found. NodeProxyAgent not configured.")

        # Example Node.js Lambda function, its bundling will use the configured proxy
        NodejsFunction(
            self_stack, "MyProxyEnabledFunction",
            entry="./lambda/index.ts", # Replace with your Node.js Lambda entry point
            handler="handler",
            memory_size=128
        )


app = App()
MyProxyStack(app, "MyProxyEnabledStack",
             env=Environment(account=os.environ.get('CDK_DEFAULT_ACCOUNT', '123456789012'),
                             region=os.environ.get('CDK_DEFAULT_REGION', 'us-east-1'))
)
app.synth()

view raw JSON →