Skip to content

(code): (asset bundling fails when using new "network" option for BundlingOptions) #21687

@patrickmryan

Description

@patrickmryan

Describe the bug

When using the BundlingOptions class, there is a new option "network", which should propagate as a command-line parameter for "docker run". That value is not being propagated at all.

Expected Behavior

Using <network="host"> in my code should result in "--network=host" being one of the options to docker run.

Current Behavior

Below is output from a test. For display purposes, I have a local script called "docker", which in turn echoes the arguments before executing /usr/local/bin/docker $*. Note that --network=host is not present.

Bundling asset LayertestStack/skyfield/Code/Stage...
docker run --rm -u 503:20 -v /Users/pmryan/ec/projects/galactica/layertest/layers/skyfield:/asset-input:delegated -v /Users/pmryan/ec/projects/galactica/layertest/cdk.out/asset.3b5d0f5daa611ef1c24e17a1b492d90aa153110e44c36b92c09d3aff135129dc:/asset-output:delegated -v /Users/pmryan/ec/projects/galactica/layertest/layers/skyfield:/install:delegated -w /asset-input python:3.9-slim pip install --disable-pip-version-check --no-cache-dir -r /install/requirements.txt -t /asset-output/python
Collecting skyfield
  Downloading skyfield-1.43.1-py3-none-any.whl (439 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 439.1/439.1 KB 6.3 MB/s eta 0:00:00
Collecting certifi>=2017.4.17

Reproduction Steps

Complete code can be found at https://github.com/patrickmryan/layertest
The output above was generated from a CDK stack. The key code is below.

        opts = "--disable-pip-version-check --no-cache-dir"
        build_image = DockerImage("python:3.9-slim")

        layer_code = _lambda.Code.from_asset(
            layer_dir,
            bundling=BundlingOptions(
                image=build_image,
                volumes=[
                    DockerVolume(
                        container_path=container_dir,
                        host_path=os.path.join(os.getcwd(), layer_dir),
                    )
                ],
                command=f"pip install {opts} -r {container_dir}/requirements.txt -t /asset-output/python".split(),
                network="host",
            ),
        )

Possible Solution

I found the bug. It's in aws-cdk/packages/@aws-cdk/core/lib/asset-staging.ts around line 456. This is the code that's apparently executed when bundling an asset for a function or layer. The code seems a bit weird as it copies values out of "options" and then tells "options.image" to run. Could be fixed in one line by propagating options.network but really this needs a bit of refactoring.

Additional Information/Context

This option is required when running in a secure network.

CDK CLI Version

2.38.1 (build a5ced21)

Framework Version

No response

Node.js Version

v16.13.1

OS

Mac OS X, Linux

Language

Python

Language Version

3.9

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.effort/smallSmall work item – less than a day of effortp1package/toolsRelated to AWS CDK Tools or CLI

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions