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

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

Open
ghost opened this issue Jan 3, 2024 · 4 comments · Fixed by #28482
Open

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

ghost opened this issue Jan 3, 2024 · 4 comments · Fixed by #28482
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@ghost
Copy link

ghost commented Jan 3, 2024

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

@ghost ghost added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 3, 2024
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Jan 3, 2024
@pahud
Copy link
Contributor

pahud commented Jan 4, 2024

Can you share your ec2 instance resource template from the synthesized cloudformation template?

Is the KeyName from the synthesized template of the AWS::EC2::Instance correct?

And, did you check the ec2 console for the KeyName? Is it correct?

If both the synthesized template and console are correct, I think it should work as expected.

@pahud pahud added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jan 4, 2024
@laurelmay
Copy link
Contributor

laurelmay commented Jan 4, 2024

I believe this is a duplicate of #28478 presented in a different form. There is an open PR (#28482) to resolve this that needs review for an exemption request before it can proceed.

Thank you for reporting this! Hopefully the PR with the fix will be able to be merged quickly.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jan 4, 2024
@fire015
Copy link

fire015 commented Jan 4, 2024

+1 having the same problem

@ayush-shah-1501
Copy link
Contributor

We can close this as PR #28482 is merged now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants