Skip to content

Commit 22e6ce8

Browse files
fix(ec2): passing keypair to instance unexpectedly does nothing (#28482)
Fixes by Specifying key pair reference in cfnInstance. This will change behavior if both `keyName` and `keyPair` is set on an existing resource, since we will use `keyPair.keyPairName` instead of `keyName` now. However, there is no correct use case for specifying both `keyPair` and `keyName`, and given `keyName` is deprecated, this PR is introducing the correct behavior. Closes #28478. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 0042e53 commit 22e6ce8

File tree

14 files changed

+194
-165
lines changed

14 files changed

+194
-165
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.key-pair.js.snapshot/KeyPairTestDefaultTestDeployAssertF103D058.assets.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.key-pair.js.snapshot/asset.4554b47be6f57b68c6c7a7391dcc73894866d2377fe174883351e7639097f292/__entrypoint__.js

Lines changed: 147 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.key-pair.js.snapshot/asset.a099fdfc61c84ffc56cef4fb2c9472483623ac865ce5d8fca88c89cf60d48d03/__entrypoint__.js

Lines changed: 0 additions & 147 deletions
This file was deleted.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.key-pair.js.snapshot/aws-cdk-ec2-key-pair-1.assets.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.key-pair.js.snapshot/aws-cdk-ec2-key-pair-1.template.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@
489489
"S3Bucket": {
490490
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
491491
},
492-
"S3Key": "a099fdfc61c84ffc56cef4fb2c9472483623ac865ce5d8fca88c89cf60d48d03.zip"
492+
"S3Key": "4554b47be6f57b68c6c7a7391dcc73894866d2377fe174883351e7639097f292.zip"
493493
},
494494
"Timeout": 900,
495495
"MemorySize": 128,
@@ -588,6 +588,9 @@
588588
"Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestal2023amikernel61x8664C96584B6F00A464EAD1953AFF4B05118Parameter"
589589
},
590590
"InstanceType": "t3.micro",
591+
"KeyName": {
592+
"Ref": "TestKeyPair38B6CD21"
593+
},
591594
"SecurityGroupIds": [
592595
{
593596
"Fn::GetAtt": [

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.key-pair.js.snapshot/cdk.out

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.key-pair.js.snapshot/integ.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.key-pair.js.snapshot/manifest.json

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.key-pair.js.snapshot/tree.json

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk-lib/aws-ec2/lib/instance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export interface InstanceProps {
7777
* Name of SSH keypair to grant access to instance
7878
*
7979
* @default - No SSH access will be possible.
80-
* @deprecated - Use {@link keyPair} instead
80+
* @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
8181
*/
8282
readonly keyName?: string;
8383

@@ -437,7 +437,7 @@ export class Instance extends Resource implements IInstance {
437437
// there is no need to configure them on the instance level
438438
this.instance = new CfnInstance(this, 'Resource', {
439439
imageId: imageConfig.imageId,
440-
keyName: props.keyName,
440+
keyName: props.keyPair?.keyPairName ?? props?.keyName,
441441
instanceType: props.instanceType.toString(),
442442
subnetId: networkInterfaces ? undefined : subnet.subnetId,
443443
securityGroupIds: networkInterfaces ? undefined : securityGroupsToken,

packages/aws-cdk-lib/aws-ec2/lib/launch-template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ export interface LaunchTemplateProps {
333333
* Name of SSH keypair to grant access to instance
334334
*
335335
* @default - No SSH access will be possible.
336-
* @deprecated - Use `keyPair` instead.
336+
* @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
337337
*/
338338
readonly keyName?: string;
339339

packages/aws-cdk-lib/aws-ec2/lib/nat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export interface NatInstanceProps {
172172
* Name of SSH keypair to grant access to instance
173173
*
174174
* @default - No SSH access will be possible.
175-
* @deprecated - Use `keyPair` instead.
175+
* @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
176176
*/
177177
readonly keyName?: string;
178178

packages/aws-cdk-lib/aws-ec2/test/instance.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,26 @@ describe('instance', () => {
520520
})).toThrow('Cannot specify both of \'keyName\' and \'keyPair\'; prefer \'keyPair\'');
521521
});
522522

523+
it('correctly associates a key pair', () => {
524+
// GIVEN
525+
const keyPair = new KeyPair(stack, 'KeyPair', {
526+
keyPairName: 'test-key-pair',
527+
});
528+
529+
// WHEN
530+
new Instance(stack, 'Instance', {
531+
vpc,
532+
instanceType: new InstanceType('t2.micro'),
533+
machineImage: new AmazonLinuxImage(),
534+
keyPair,
535+
});
536+
537+
// THEN
538+
Template.fromStack(stack).hasResourceProperties('AWS::EC2::Instance', {
539+
KeyName: stack.resolve(keyPair.keyPairName),
540+
});
541+
});
542+
523543
describe('Detailed Monitoring', () => {
524544
test('instance with Detailed Monitoring enabled', () => {
525545
// WHEN

0 commit comments

Comments
 (0)