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: passing keyPair to Instance does nothing #28478

Closed
SamuraiPrinciple opened this issue Dec 23, 2023 · 8 comments · Fixed by #28482 · May be fixed by stack-spot/app-handler-functions-template#2, stack-spot/eks-env-ts-template#2 or stack-spot/web-react-deploy#4
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

@SamuraiPrinciple
Copy link

SamuraiPrinciple commented Dec 23, 2023

Describe the bug

With keyName property being deprecated (#28138), creating aws_ec2.KeyPair and passing it to aws_ec2.Instance does nothing.

Expected Behavior

Make sure the keypair is associated with the instance when passed through keyPair property.

Current Behavior

keypair does not get associated with the instance.

Reproduction Steps

const keyPair = new ec2.KeyPair(this, 'KeyPair', {});
 this.instance = new ec2.Instance(this, 'EC2Instance', {
      vpc: props.vpc,
      vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
      securityGroup,
      instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MICRO),
      machineImage: ec2.MachineImage.latestAmazonLinux2023(),
      keyPair,
      userDataCausesReplacement: true,
      ssmSessionPermissions: true,
    });

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.116.1 (build 222de71)

Framework Version

No response

Node.js Version

20.10.0

OS

Linux

Language

TypeScript

Language Version

No response

Other information

No response

@SamuraiPrinciple SamuraiPrinciple added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 23, 2023
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Dec 23, 2023
@tam0ri
Copy link
Contributor

tam0ri commented Dec 23, 2023

props.keyPair is never referred as property of CfnInstance.

this.instance = new CfnInstance(this, 'Resource', {
imageId: imageConfig.imageId,
keyName: props.keyName,
instanceType: props.instanceType.toString(),
subnetId: networkInterfaces ? undefined : subnet.subnetId,
securityGroupIds: networkInterfaces ? undefined : securityGroupsToken,
networkInterfaces,
iamInstanceProfile: iamProfile.ref,
userData: userDataToken,
availabilityZone: subnet.availabilityZone,
sourceDestCheck: props.sourceDestCheck,
blockDeviceMappings: props.blockDevices !== undefined ? instanceBlockDeviceMappings(this, props.blockDevices) : undefined,
privateIpAddress: networkInterfaces ? undefined : props.privateIpAddress,
propagateTagsToVolumeOnCreation: props.propagateTagsToVolumeOnCreation,
monitoring: props.detailedMonitoring,
});

Specifying props.keyPair?.keyPairName for keyName property as same as LaunchTemplate will solve the issue.

keyName: props.keyPair?.keyPairName ?? props?.keyName,

It seems to be easy to fix and suitable for good first issue. I can take this, but if anyone is interested in submitting PR, please feel free to try it :)

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Dec 24, 2023
@pahud
Copy link
Contributor

pahud commented Dec 24, 2023

Awesome! Thank you for the PR!

@fire015

This comment was marked as duplicate.

2 similar comments
@yuki549

This comment was marked as duplicate.

@GuillaumeEscande

This comment was marked as duplicate.

@mrgrain
Copy link
Contributor

mrgrain commented Jan 8, 2024

Please use the 👍🏻 reaction on the original post if you have the same issue. Plus there already is a PR with potential fix open: #28482

@mergify mergify bot closed this as completed in #28482 Jan 8, 2024
@mergify mergify bot closed this as completed in 22e6ce8 Jan 8, 2024
Copy link

github-actions bot commented Jan 8, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

1 similar comment
Copy link

github-actions bot commented Jan 8, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment