Skip to content
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): passing keypair to instance unexpectedly does nothing #28482

Merged
merged 10 commits into from
Jan 8, 2024

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -489,7 +489,7 @@
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "a099fdfc61c84ffc56cef4fb2c9472483623ac865ce5d8fca88c89cf60d48d03.zip"
"S3Key": "4554b47be6f57b68c6c7a7391dcc73894866d2377fe174883351e7639097f292.zip"
},
"Timeout": 900,
"MemorySize": 128,
@@ -588,6 +588,9 @@
"Ref": "SsmParameterValueawsserviceamiamazonlinuxlatestal2023amikernel61x8664C96584B6F00A464EAD1953AFF4B05118Parameter"
},
"InstanceType": "t3.micro",
"KeyName": {
"Ref": "TestKeyPair38B6CD21"
},
"SecurityGroupIds": [
{
"Fn::GetAtt": [

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-ec2/lib/instance.ts
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ export interface InstanceProps {
* Name of SSH keypair to grant access to instance
*
* @default - No SSH access will be possible.
* @deprecated - Use {@link keyPair} instead
* @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;

@@ -437,7 +437,7 @@ export class Instance extends Resource implements IInstance {
// there is no need to configure them on the instance level
this.instance = new CfnInstance(this, 'Resource', {
imageId: imageConfig.imageId,
keyName: props.keyName,
keyName: props.keyPair?.keyPairName ?? props?.keyName,
instanceType: props.instanceType.toString(),
subnetId: networkInterfaces ? undefined : subnet.subnetId,
securityGroupIds: networkInterfaces ? undefined : securityGroupsToken,
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-ec2/lib/launch-template.ts
Original file line number Diff line number Diff line change
@@ -333,7 +333,7 @@ export interface LaunchTemplateProps {
* Name of SSH keypair to grant access to instance
*
* @default - No SSH access will be possible.
* @deprecated - Use `keyPair` instead.
* @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;

2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-ec2/lib/nat.ts
Original file line number Diff line number Diff line change
@@ -172,7 +172,7 @@ export interface NatInstanceProps {
* Name of SSH keypair to grant access to instance
*
* @default - No SSH access will be possible.
* @deprecated - Use `keyPair` instead.
* @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;

20 changes: 20 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/test/instance.test.ts
Original file line number Diff line number Diff line change
@@ -520,6 +520,26 @@ describe('instance', () => {
})).toThrow('Cannot specify both of \'keyName\' and \'keyPair\'; prefer \'keyPair\'');
});

it('correctly associates a key pair', () => {
// GIVEN
const keyPair = new KeyPair(stack, 'KeyPair', {
keyPairName: 'test-key-pair',
});

// WHEN
new Instance(stack, 'Instance', {
vpc,
instanceType: new InstanceType('t2.micro'),
machineImage: new AmazonLinuxImage(),
keyPair,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::EC2::Instance', {
KeyName: stack.resolve(keyPair.keyPairName),
});
});

describe('Detailed Monitoring', () => {
test('instance with Detailed Monitoring enabled', () => {
// WHEN