Skip to content

(aws_s3_assets): unable to define bundling with local (python) #17928

@andrewjroth

Description

@andrewjroth

link to reference doc page

https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_s3_assets/README.html#asset-bundling

Describe your issue?

The documentation has an example which is not syntactically correct for Python:

assets.Asset(self, "BundledAsset",
    path="/path/to/asset",
    bundling={
        "local": {
            def try_bundle(self, output_dir, options):
                if can_run_locally:
                    # perform local bundling here
                    return True
                return False
        },
        # Docker bundling fallback
        "image": DockerImage.from_registry("alpine"),
        "entrypoint": ["/bin/sh", "-c"],
        "command": ["bundle"]
    }
)

The value for 'bundling' isn't correct Python code. You can't define a function in the middle of a dictionary.

I've attempted to determine how to correctly define this value, but haven't been able to come up with the correct syntax. Based on the documentation, specific types are expected, so I tried constructing those types ahead of time:

class JinjaLocalBundling(ILocalBundling):
    def try_bundle(self, output_dir, options, *args, **kwargs) -> bool:
        print("Local Jinja2 Render:", self, output_dir, options, args, kwargs)
        return False

JinjaBundling = BundlingOptions(
    local=JinjaLocalBundling,
    image=DockerImage.from_registry("python3"),
    command=["python3 -m pip install jinja2"]
)

Then, including it in the Asset creation:

Asset(self, "BundleTest", path=os.path.join(dirname, "configure.jinja"),
      bundling=JinjaBundling)

This results in an error:

TypeError: Don't know how to convert object to JSON: <class 'test_app.test_stack.JinjaLocalBundling'>

There is very little documentation on how to do this correctly. Can someone demonstrate the correct way to use the bundling option for Asset definitions?

Overall Goal: I want to render a script to be executed as part of the EC2 User Data by replacing variables in the script with values from the stack. For example, I have a script which syncs data from an S3 bucket, but the S3 bucket name needs to be injected into the script. Something like this to replace {{ bucketname }} with the bucket defined by the CDK app:

aws s3 sync s3://{{ bucketname }}/path/ /tmp/path/

If there is an easier way to do this, please let me know. (but also fix the doc since it really doesn't work)

Thanks!!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions