-
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
feat(asg): support keypair functionality for asg #29679
Conversation
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.
The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.
A comment requesting an exemption should contain the text Exemption Request
. Additionally, if clarification is needed add Clarification Request
to a comment.
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
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.
Thanks 👍
Left some comments for adjustments.
Also, the new property and all added validations should have proper unit test coverage here.
*/ | ||
readonly keyName?: string; |
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.
*/ | |
readonly keyName?: string; | |
* @deprecated - Use `keyPair` instead - https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2-readme.html#using-an-existing-ec2-key-pair | |
*/ | |
readonly keyName?: string; |
We should mark the property as deprecated.
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.
Fixed in 4940cd1
* `keyName`, `launchTemplate` and `mixedInstancesPolicy` must not be specified | ||
* when this property is specified | ||
* |
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.
* `keyName`, `launchTemplate` and `mixedInstancesPolicy` must not be specified | |
* when this property is specified | |
* | |
* `launchTemplate` and `mixedInstancesPolicy` must not be specified | |
* when this property is specified. | |
* | |
* You can either specify `keyPair` or `keyName`, not both. |
Suggestion for different wording. Can you please update keyName
documentation as well?
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.
Fixed in 4940cd1
/** | ||
* The SSH keypair to grant access to the instance. | ||
* | ||
* `keyName`, `launchTemplate` and `mixedInstancesPolicy` must not be specified |
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.
We're missing this validation for keyPair
.
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.
Fixed in 73e2e99
@@ -1390,6 +1411,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements | |||
spotOptions: props.spotPrice !== undefined ? { maxPrice: parseFloat(props.spotPrice) } : undefined, | |||
blockDevices: props.blockDevices, | |||
instanceProfile, | |||
...keyUsageType, |
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.
...keyUsageType, | |
keyName: props.keyPair?.keyPairName ?? props?.keyName, |
Given the above validation, we can simplify this assignment.
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.
@lpizzinidev The idea of creating the keyUsageType
was to send just one of them (keyPair or keyName) to the LaunchTemplate, to avoid sending the keyName
if keyPair
is set
let keyUsageType: { keyPair?: ec2.IKeyPair; keyName?: string } = {};
if (props.keyPair) {
keyUsageType = { keyPair: props.keyPair };
} else {
keyUsageType = { keyName: props.keyName };
}
It makes sense ?
@@ -1398,6 +1420,10 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements | |||
this._connections = new ec2.Connections({ securityGroups: [this.securityGroup] }); | |||
this.securityGroups = [this.securityGroup]; | |||
|
|||
if (props.keyPair) { | |||
throw new Error('Can only use \'keyPair\' when feature flag \'AUTOSCALING_GENERATE_LAUNCH_TEMPLATE\' is set'); |
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.
Do we need this check?
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.
I added this validation because if AUTOSCALING_GENERATE_LAUNCH_TEMPLATE
is disabled, a CfnLaunchConfiguration
will be created instead LaunchTemplate
.. and doesn't have support for the keyPair
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.
I see, thanks for clarifying! Can you please add it to the documentation of the variable?
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.
Done! f7a3896
@@ -24,6 +24,7 @@ new autoscaling.AutoScalingGroup(this, 'ASG', { | |||
Creating an `AutoScalingGroup` from a Launch Configuration has been deprecated. All new accounts created after December 31, 2023 will no longer be able to create Launch Configurations. With the `@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig` feature flag set to true, `AutoScalingGroup` properties used to create a Launch Configuration will now be used to create a `LaunchTemplate` using a [Launch Configuration to `LaunchTemplate` mapping](https://docs.aws.amazon.com/autoscaling/ec2/userguide/migrate-launch-configurations-with-cloudformation.html#launch-configuration-mapping-reference). Specifically, the following `AutoScalingGroup` properties will be used to generate a `LaunchTemplate`: | |||
* machineImage | |||
* keyName |
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.
* keyName | |
* keyName (deprecated, prefer keyPair) |
Could you please add an example of keyPair
usage in the code block below?
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.
Done in b88ea01
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.
Thanks for implementing the changes and adding the comments 👍
Left a couple of suggestions.
Plus, we're still missing coverage on unit tests (#29679 (review)).
@@ -1390,6 +1415,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements | |||
spotOptions: props.spotPrice !== undefined ? { maxPrice: parseFloat(props.spotPrice) } : undefined, | |||
blockDevices: props.blockDevices, | |||
instanceProfile, | |||
...keyUsageType, |
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.
...keyUsageType, | |
keyPair: props.keyPair, | |
keyName: props.keyName, |
The idea of creating the keyUsageType was to send just one of them (keyPair or keyName) to the LaunchTemplate, to avoid sending the keyName if keyPair is set
Makes sense, thanks for clarifying!
However, we could simply specify it like this then if I'm not mistaken.
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.
I tried the way you mentioned, but I believe that because of the LaunchTemplate
, when sending the keyName in your constructor, it will display the warning on the screen, even if the user did not define it when creating the ASG 😵💫
[WARNING] aws-cdk-lib.aws_ec2.LaunchTemplateProps#keyName is deprecated.
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.
Well, that's annoying 🫠
...keyUsageType, | |
keyPair: props.keyPair, | |
...{ props.keyName ? { keyName: props.keyName } : {} }, |
I guess this should work then.
Your implementation is correct of course, but I'd like to keep it more concise if possible.
Thanks for following up on this 👍
@@ -1398,6 +1420,10 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements | |||
this._connections = new ec2.Connections({ securityGroups: [this.securityGroup] }); | |||
this.securityGroups = [this.securityGroup]; | |||
|
|||
if (props.keyPair) { | |||
throw new Error('Can only use \'keyPair\' when feature flag \'AUTOSCALING_GENERATE_LAUNCH_TEMPLATE\' is set'); |
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.
I see, thanks for clarifying! Can you please add it to the documentation of the variable?
Unit tests add: 51fda7f |
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.
Thanks 👍
@@ -1390,6 +1415,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements | |||
spotOptions: props.spotPrice !== undefined ? { maxPrice: parseFloat(props.spotPrice) } : undefined, | |||
blockDevices: props.blockDevices, | |||
instanceProfile, | |||
...keyUsageType, |
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.
Well, that's annoying 🫠
...keyUsageType, | |
keyPair: props.keyPair, | |
...{ props.keyName ? { keyName: props.keyName } : {} }, |
I guess this should work then.
Your implementation is correct of course, but I'd like to keep it more concise if possible.
Thanks for following up on this 👍
@shikha372 I believe I updated the branch by mistake, sorry 😵 |
yeah, manual merge not recommended with mergifyio, i'll see it from here |
@Mergifyio update |
❌ Mergify doesn't have permission to updateFor security reasons, Mergify can't update this pull request. Try updating locally. |
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). |
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). |
Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one. |
Issue
When creating an instance directly through the asg, it is not possible to define the
keyPair
, in addition to bringing a warning message that thekeyName
will be removedThis configuration allows sending the
keyPair
to the asg since the LaunchTemplate allows its integrationWarning:
Closes #29237
Reason for this change
I'm working directly with CDK and needed to implement a way to use my
keyPair
and avoid warning thatkeyName
will be removed soon when i'm creating my ASGDescription of changes
keyPair
to CommonAutoScalingGroupProps interfacekeyPair
andkeyName
from being set at the same timekeyPair
when creating LaunchTemplate if flagAUTOSCALING_GENERATE_LAUNCH_TEMPLATE
is enabledkeyPair
if the flagAUTOSCALING_GENERATE_LAUNCH_TEMPLATE
is disabledChecklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license