Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

link to reference doc page

Describe your issue?

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

assets.Asset(self, "BundledAsset",
        "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(
    command=["python3 -m pip install jinja2"]

Then, including it in the Asset creation:

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

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)


Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

alexpulvercommented, Sep 5, 2022

@rix0rrr the aws_s3_assets package overview documentation for Python generates the following signature for try_bundle method:

def try_bundle(self, output_dir, *, image, entrypoint=None, command=None, volumes=None, environment=None, workingDirectory=None, user=None, local=None, outputType=None, securityOpt=None, network=None):

Per my test and TypeScript documentation, it should be:

def try_bundle(self, output_dir, options):

Should I open a separate issue, or would you like to reopen this one?

Full working example:

import aws_cdk.aws_s3_assets as assets
import jsii
from aws_cdk import BundlingOptions, DockerImage, ILocalBundling

class MyBundle:
    def try_bundle(self, output_dir, options):
        can_run_locally = True  # replace with actual logic
        if can_run_locally:
            # perform local bundling here
            return True
        return False

    self, "BundledAsset",
        # Docker bundling fallback
        entrypoint=["/bin/sh", "-c"],

cc: @heitorlessa

github-actions[bot]commented, Dec 31, 2021


Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

aws-cdk/aws-s3-assets module - AWS Documentation
Use local to specify a local bundling provider. The provider implements a method tryBundle() which should return true if local bundling was performed....
Read more >
AWS CDK - Fullstack Polyglot with Asset Bundling
If local bundling fails, now we at least have a meaningful error message to the user. Of course the above code will fail...
Read more >
@aws-cdk/aws-s3-assets | Yarn - Package Manager
Assets are local files or directories which are needed by a CDK app. A common example is a directory which contains the handler...
Read more >
Lessons Learned from State Bridge Bundling Programs
Twenty-five local bridges will be replaced at a cost of $39.6 million, ... NDOR defines bridges bundling as one contractor from a single...
Read more >
CDK Unit test using jest and NodeJsFunction - Stack Overflow
... but a diagnostic/workaround: pass bundling: {forceDockerBundling: true } in the lambda props to use Docker instead of esbuild for local ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found