-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(aws-ec2): InitFile construct IDs are interpolated from target_filename
when using fromAsset() or fromSource() which can cause naming collisions
#16891
Comments
target_filename
which can cause naming collisions target_filename
which can cause naming collisions
target_filename
which can cause naming collisions target_filename
when using fromAsset() or fromSource() which can cause naming collisions
Thanks for the bug report and the detailed write-up! |
This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled. |
Any progress here? |
…multiple instances in the same stack (#27468) Closes #16891 If a user creates more than one EC2 instance in the same stack and defines an InitConfig for each instance using `ec2.InitFile.fromAsset()`, the user will get an error if they pass in the same `targetFilePath` or `targetDirectory`. This bug is due to [how the asset is uploaded to S3](https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-ec2/lib/cfn-init-elements.ts#L427). This PR fixes this issue by adding a hash to the `targetFileName` before being uploaded as an s3 asset. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
|
What is the problem?
TLDR: Naming collision of InitFile constructs for multiple ec2 instances defined in the same stack definition when using the fromAsset or fromSource methods. Affected file: https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-ec2/lib/cfn-init-elements.ts. Cause is most likely due to the underlying InitFile fromAsset and fromSource methods creating an s3 asset with interpolated id of
${targetFileName}Asset
.The Scenario
I'm deploying a fleet of instances, each requiring a configuration file (unique to each instance) to be placed in the same directory in each instance. For example instance-a has a config file that has a config file that is different to that of instance-b. In both instances, the file is placed in the path /etc/gitlab-runner/config.toml.
I've leveraged cfn-init and the cdk aws-ec2.InitFile.fromAsset() construct and method to create the file on each instance.
When synthesizing, the following error is returned:
jsii.errors.JSIIError: There is already a Construct with name '--etc--gitlab-runner--config.tomlAsset' in Stack
Reproduction Steps
What did you expect to happen?
I expected each InitFile to have a uniquely interpolated construct Id, and for there not to be a collision in names.
What actually happened?
Both constructs for InitFile were named
--etc--gitlab-runner--config.tomlAsset
resulting in the synth errorjsii.errors.JSIIError: There is already a Construct with name '--etc--gitlab-runner--config.tomlAsset' in Stack [...]
The construct Id for Init file is interpolated from the target_file_name, which will cause an issue if multiple EC2 instances are defined in the same stack, each which use the InitFile construct to place files in the same folder in their respective instances.
CDK CLI Version
1.24.0
Framework Version
1.24.0
Node.js Version
v16.3.0
OS
MacOS Catalina 10.15.7
Language
Python
Language Version
Python 3.9.7
Other information
Workaround
In an InitConfig:
Example:
Possible Solution
Looking at https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-ec2/lib/cfn-init-elements.ts there are two places where an s3 asset is used as a staging location for files before being placed on an instance. This s3 asset construct's ID is causing the naming collision in cdk.
Classes InitFile and InitSource both use the same naming convention for the s3_asset construct id:
A fix might be to interpolate the s3_assets.Asset id as a combination of the filename and a unique hash that wouldn't change on each synth.
The text was updated successfully, but these errors were encountered: