Skip to content

aws_ec2: KeyPair static methods do not retrieve existing key pairs #28569

@ghost

Description

Describe the bug

using the static methods

KeyPair.fromKeyPairName()
KeyPair.fromKeyPairAttributes()

and the the keyPair property on the InstanceProps interface does not set the public key on the instance at instance deployment

{
     keyPair:IKeyPair
}

### Expected Behavior

using the methods:

```typescript
    const keyPair = KeyPair.fromKeyPairAttributes(this, 'key-pair', {
      keyPairName: 'myExistingKeyName',
      type: KeyPairType.RSA
    });
   const instance = new ec2.Instance(this, 'instance', {
      vpc: this.props.vpc,
      securityGroup: this.props.applicationSecurityGroup,
      vpcSubnets: {
        subnetType: ec2.SubnetType.PUBLIC
      },
      instanceName: 'host',
      instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MEDIUM),
      machineImage: ec2.MachineImage.latestAmazonLinux2023(),
      keyPair: keyPair,
      userDataCausesReplacement: true
    });

Should result in the public key being set on in the ~/.ssh/authorized_keys file of the instance.

Current Behavior

No keys are set when that property / method is used.

the "keyName" property marked as obsolete DOES work however.

Reproduction Steps

using the methods:

    const keyPair = KeyPair.fromKeyPairAttributes(this, 'key-pair', {
      keyPairName: 'myExistingKeyName',
      type: KeyPairType.RSA
    });
   const instance = new ec2.Instance(this, 'instance', {
      vpc: this.props.vpc,
      securityGroup: this.props.applicationSecurityGroup,
      vpcSubnets: {
        subnetType: ec2.SubnetType.PUBLIC
      },
      instanceName: 'host',
      instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MEDIUM),
      machineImage: ec2.MachineImage.latestAmazonLinux2023(),
      keyPair: keyPair,
      userDataCausesReplacement: true
    });

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.117.0 (build 59d9b23)

Framework Version

No response

Node.js Version

v18.16.0

OS

macOS sonoma 14.1.2 (23B92)

Language

TypeScript

Language Version

5.3.3

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-ec2Related to Amazon Elastic Compute CloudbugThis issue is a bug.closed-for-stalenessThis issue was automatically closed because it hadn't received any attention in a while.effort/mediumMedium work item – several days of effortp2response-requestedWaiting on additional info and feedback. Will move to "closing-soon" in 7 days.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions