(aws_s3_assets): unable to define bundling with local (python)
See original GitHub issuelink 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!!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
@rix0rrr the
aws_s3_assets
package overview documentation for Python generates the following signature fortry_bundle
method:Per my test and TypeScript documentation, it should be:
Should I open a separate issue, or would you like to reopen this one?
Full working example:
cc: @heitorlessa
⚠️COMMENT VISIBILITY WARNING⚠️
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.