-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
fix(ec2): userData in launchTemplate is created automatically when machineImege is provided #23593
Conversation
hmm this change will require many downstream snapshots to be updated 🤔 edit) It turns out that only two modules (autoscaling and gamelift) requires snapshot updates. |
@rix0rrr @ddneilson Hi, what do you think of this change? I'd like to know if I'm missing anything about why the userData was not created in the original PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This stack:
export class Ec2UserdataStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const launchTemplate = new ec2.LaunchTemplate(this, 'LaunchTemplate', {
instanceType: new ec2.InstanceType('t3.small'),
machineImage: new ec2.AmazonLinuxImage({
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
}),
});
// this will cause an error
launchTemplate.userData!.addCommands("yum -y update")
results in an error.
We can either update the docstring to reflect the current behavior or change the default so that userData
is created by default. Why would we want userData
to be created by default? I'd prefer we leave the current behavior and just update the docstring
} | ||
|
||
// priority: prop.userData -> userData from machineImage -> undefined | ||
this.userData = props.userData ?? imageConfig?.userData; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't we do
this.userData = props.userData ?? imageConfig?.userData; | |
if (props.userData || imageConfig?.userData) { | |
this.userData = props.userData ?? imageConfig?.userData; | |
} |
and avoid the change to the templates? I understand that those won't require replacement or interruption, but we try to avoid changes to your diff caused only by upgrading. We'd have to put this behind a feature flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi thank you for the review!
I believe they behave in the same way. The original code just assigns undefined
to this.userData
if both props.userData
and imageConfig?.userData
are undefined
. And no code before this line assigns any value to this.userData
either, so this.userData
remains unchanged when a developer does not specify userData or machineImage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As for adding a feature flag, I'm not yet really convinced to add it. As far as I researched, it will not be a breaking change even without a feature flag. (Not 100% sure this change will not break anything as it is difficult to guarantee, but we can fix/revert it as soon as someone reports it.)
With that being said, if you CDK team wants to go more carefully, I'm willing to add this change behind a feature flag :) What do you think? @comcalvi
The reasons I'd like to add this feature are:
// previously
// more code, duplicated knowledge about the instance OS
const userData = ec2.UserData.forLinux();
const launchTemplate = new ec2.LaunchTemplate(this, 'LaunchTemplate', {
instanceType: new ec2.InstanceType('t3.small'),
machineImage: new ec2.AmazonLinuxImage({
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
}),
userData,
});
launchTemplate.userData!.addCommands("yum -y update")
// becomes
const launchTemplate = new ec2.LaunchTemplate(this, 'LaunchTemplate', {
instanceType: new ec2.InstanceType('t3.small'),
machineImage: new ec2.AmazonLinuxImage({
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
}),
});
launchTemplate.userData!.addCommands("yum -y update")
// we can do this
const instance = new ec2.Instance(this, 'Instance', {
vpc,
instanceType: new ec2.InstanceType('t3.small'),
machineImage: new ec2.AmazonLinuxImage({
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
}),
});
instance.userData!.addCommands("yum -y update") |
hey @tmokmss, so I agree that making Reading through that discussion though I see some mention about the concept of "empty" user data not being easily defined. In the autoscaling snapshots we see the addition of "UserData": {
"Fn::Base64": "#!/bin/bash"
} Does a Linux |
This PR has been in the CHANGES REQUESTED state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week. |
Pull request has been modified.
Hi @MrArnoldPalmer, thank you for your comment! OK, I agree with the possibility of breaking someone's code, so I'll add a feature flag for this. As for an empty user data, yes, a shebang will appear also in an ec2.Instance template without user data defined. aws-cdk/packages/@aws-cdk/aws-globalaccelerator-endpoints/test/integ.globalaccelerator.ts Lines 29 to 33 in 5a0c627
will be synthesized to this: Lines 578 to 580 in 5a0c627
It might be cleaner in terms of CFn just to set undefined instead of a shebang, but apparently no one have complained about this behavior for three years. |
@comcalvi @MrArnoldPalmer I added a feature flag named |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, leaving dnm so @MrArnoldPalmer can take a look.
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
…chineImege is provided (aws#23593) closes aws#23592 Reading through the discussion in PR aws#12385, which introduced the original code, I could not find any reason not to create userData when machineImage is provided. They also agreed with the design [here](aws#12385 (comment)), but it seems it accidentally became out of scope at the time. This change should not be considered as a breaking change because we are just adding empty userData to launchTemplates whose userData is not specified explicitly, and it will not have any effect on the existing behavior. * Users who already sets a userData explicitly: * will not see any change to their synthesized template, as this PR only modifies userData when it is not set explicitly. * Users who is not using userData: * will see a userData is added to their template. But the userData is empty and does nothing. It should not have any effect on the previous behavior. ---- ### All Submissions: * [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [X] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [X] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
…chineImege is provided (aws#23593) closes aws#23592 Reading through the discussion in PR aws#12385, which introduced the original code, I could not find any reason not to create userData when machineImage is provided. They also agreed with the design [here](aws#12385 (comment)), but it seems it accidentally became out of scope at the time. This change should not be considered as a breaking change because we are just adding empty userData to launchTemplates whose userData is not specified explicitly, and it will not have any effect on the existing behavior. * Users who already sets a userData explicitly: * will not see any change to their synthesized template, as this PR only modifies userData when it is not set explicitly. * Users who is not using userData: * will see a userData is added to their template. But the userData is empty and does nothing. It should not have any effect on the previous behavior. ---- ### All Submissions: * [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [X] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [X] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
…chineImege is provided (aws#23593) closes aws#23592 Reading through the discussion in PR aws#12385, which introduced the original code, I could not find any reason not to create userData when machineImage is provided. They also agreed with the design [here](aws#12385 (comment)), but it seems it accidentally became out of scope at the time. This change should not be considered as a breaking change because we are just adding empty userData to launchTemplates whose userData is not specified explicitly, and it will not have any effect on the existing behavior. * Users who already sets a userData explicitly: * will not see any change to their synthesized template, as this PR only modifies userData when it is not set explicitly. * Users who is not using userData: * will see a userData is added to their template. But the userData is empty and does nothing. It should not have any effect on the previous behavior. ---- ### All Submissions: * [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [X] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [X] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
closes #23592
Reading through the discussion in PR #12385, which introduced the original code, I could not find any reason not to create userData when machineImage is provided. They also agreed with the design here, but it seems it accidentally became out of scope at the time.
This change should not be considered as a breaking change because we are just adding empty userData to launchTemplates whose userData is not specified explicitly, and it will not have any effect on the existing behavior.
All Submissions:
Adding new Construct Runtime Dependencies:
New Features
yarn integ
to deploy the infrastructure and generate the snapshot (i.e.yarn integ
without--dry-run
)?By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license