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

Add ability to add EC2 key-pair #5252

Closed
2 tasks
porn opened this issue Nov 29, 2019 · 25 comments · Fixed by #28138
Closed
2 tasks

Add ability to add EC2 key-pair #5252

porn opened this issue Nov 29, 2019 · 25 comments · Fixed by #28138
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud effort/large Large work item – several weeks of effort feature-request A feature should be added or improved. needs-cfn This issue is waiting on changes to CloudFormation before it can be addressed. p1

Comments

@porn
Copy link

porn commented Nov 29, 2019

Please add the ability to create EC2 key pairs via CDK.

Use Case

I would like not to have to interact with AWS Console at all.
Want my keys git-versioned and manageable from CDK.

Proposed Solution

key = aws_ec2.KeyPair(
    key_name="autoscaled",
    public_key="ssh-rsa AAAABBBBBCCC...",
)

Other

Couldn't find a way how to do that in documentation. I believe this is not doable with CDK right now.

Seems the only way is to manually add key-pair via web Console and use key_name string parameter for the EC2 machine:
https://stackoverflow.com/questions/57572065/how-can-i-access-an-ec2-instance-created-by-cdk

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change
@porn porn added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Nov 29, 2019
@Lightning303
Copy link

Hey,
you can do this right now using a custom resource.

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-cloudformation-readme.html
https://docs.aws.amazon.com/cdk/api/latest/docs/custom-resources-readme.html

@SomayaB SomayaB added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Nov 29, 2019
@SomayaB SomayaB assigned rix0rrr and SomayaB and unassigned rix0rrr and SomayaB Nov 29, 2019
@udondan
Copy link
Contributor

udondan commented Dec 13, 2019

CloudFormation doesn't support keypair generation/import.

Here's something that might be interesting: https://binx.io/blog/2017/10/25/deploying-private-key-pairs-with-aws-cloudformation/

@rix0rrr rix0rrr added the needs-cfn This issue is waiting on changes to CloudFormation before it can be addressed. label Dec 13, 2019
@SomayaB SomayaB removed the needs-triage This issue or PR still needs to be triaged. label Dec 13, 2019
@netroy
Copy link
Contributor

netroy commented Jan 10, 2020

created a ticket for the Cloudformation team
aws-cloudformation/cloudformation-coverage-roadmap#335

@udondan
Copy link
Contributor

udondan commented Jan 10, 2020

I don't think this is going to be implemented. Because there is no way to download a key after it has been created. So since you cannot download it, and you certainly do not want to have the key in the CFN outputs either, there is no secure way to create a key with CFN.

Though I created a custom resource based on the link above. The key is stored encrypted in SSM parameter store.

https://github.com/udondan/cdk-secrets

There is an example: https://github.com/udondan/cdk-secrets/blob/master/example/lib/key-pair.ts

@rix0rrr rix0rrr added the effort/large Large work item – several weeks of effort label Jan 23, 2020
@richardhboyd
Copy link
Contributor

richardhboyd commented Jan 30, 2020

there is no secure way to create a key with CFN.

Lambda Custom Resource that puts the key pair into secrets manager and exports the name of the secret. This is done in several AWS Quickstarts. This is definitely doable.

@udondan
Copy link
Contributor

udondan commented Jan 31, 2020

The sentence you quoted was in reply to a feature request on the CFN repo. While you of course can do it with a custom CFN resource, this cannot be implemented in core CFN functionality. The resource type is missing for a reason.

Right after the quoted sentence, I wrote about custom resources, even with a link to a ready-to-use cdk construct. 😉

@logemann
Copy link

There is an external NPM which tries to solve it via SecretsManager but somehow this doesnt convince me either ;-)

https://www.npmjs.com/package/cdk-ec2-key-pair

@udondan
Copy link
Contributor

udondan commented May 28, 2020

That's my package. Where else would you like it to be stored?

@logemann
Copy link

perhaps i got this package wrong... where is the actual key value? In description attribute? Or is it just a reference to a manually placed private key in SecretsManager?

@udondan
Copy link
Contributor

udondan commented May 29, 2020

The private key is stored as a secret in the SecretsManager. If you provide a KMS CMK it also will be encrypted. So you got to control who can access it even if you got roles/users who are allowed to do everything.

You could also go and reset the value of the secret, after you have retrieved the key.

@russd2357
Copy link

In your package, you grant read access to 'someRole'. If I am using your package to generate key pairs for an EC2 instance, which role would you advise?

@russd2357
Copy link

I re-read the thread and I think I got it. You're suggesting to create a role that is allowed to access the key. Only users with that role can access the key. Yes?

@udondan
Copy link
Contributor

udondan commented Jun 13, 2020

That's just an example on how to grant access. If you want to grant access or to which role is up to you and the use case. If you just want to create the key-pair and download the private key, you can just download it with the same role/user you create it with.

PS: Such things are better asked in a new issue in the packages repo. :)

@inickles-grapl
Copy link

CloudFormation doesn't support keypair generation/import.

Seems like CDK + CloudFormation should support the "Import key pair" functionality of the console UI*, which not a great wording - it's not a pair, it imports only a pubkey. The scenario is I create a new keypair locally on my machine, I create a new EC2 instance with CDK, I now want to authenticate to this instance with that keypair I made. The private key is nowhere in AWS. To do this now I need to use the console, breaking automation.

Propose solution looks the same as OP, since those are the only inputs to the existing functionality in the console UI.

@sjortiz
Copy link

sjortiz commented Feb 25, 2021

I also need this, a newly created instance that has my public key added so I can ssh, no need to return me my key that I already have in ~/.ssh/id_rsa.pub

@UsernameAlvarez
Copy link

Also I need this feature when I create a newly instance so I don't need to interact with the AWS Console. It would be great!!!

@honey-malviya
Copy link

we need this feature too. please add it asap.

@darshan-yadav
Copy link

This is something i was also looking for. Please provide this feature.

@danil-smirnov
Copy link

Would be great to have

@ilikepi63
Copy link

Would be really nice to have

@udondan
Copy link
Contributor

udondan commented Jan 29, 2022

FYI My construct cdk-ec2-key-pair does support importing public keys since version 3.2.0 (only for CDK v2)

new KeyPair(this, 'Test-Key-Pair', {
  name: 'imported-key-pair',
  publicKey: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuMmbK...'
});

https://constructs.dev/packages/cdk-ec2-key-pair/v/3.2.0?lang=typescript#importing-public-key

@0ni0nrings
Copy link

@udondan thanks for sharing your construct.

Is there a Python example available? I am getting error when doing 👇

bastion_key = KeyPair(self, 'bastion-key',
            name = 'bastion-key',
            description = 'keypair for bastion host',
        ),

@github-actions github-actions bot added p1 and removed p2 labels Jan 29, 2023
@github-actions
Copy link

This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.

@aaroncowie
Copy link

Given that AWS::EC2::KeyPair is now available in cloudformation can this be revisited?

It seems as though the tags "effort/large" and "needs-cfn" may no longer apply.

@mergify mergify bot closed this as completed in #28138 Dec 20, 2023
mergify bot pushed a commit that referenced this issue Dec 20, 2023
This adds support for creating `AWS::EC2::KeyPair` resources. These are added as a property to `Instance`, `LaunchTemplate`, and `NatInstance` and the older `keyName` prop is deprecated in favor of the new `keyPair: IKeyPair` property.

A getter is added to retrieve the SSM parameter that hold the private key for non-imported keys and checks are added to make sure that ED25519 keys are not used with a Windows instance.

Closes #5252.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Copy link

⚠️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.

paulhcsun pushed a commit to paulhcsun/aws-cdk that referenced this issue Jan 5, 2024
This adds support for creating `AWS::EC2::KeyPair` resources. These are added as a property to `Instance`, `LaunchTemplate`, and `NatInstance` and the older `keyName` prop is deprecated in favor of the new `keyPair: IKeyPair` property.

A getter is added to retrieve the SSM parameter that hold the private key for non-imported keys and checks are added to make sure that ED25519 keys are not used with a Windows instance.

Closes aws#5252.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
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 effort/large Large work item – several weeks of effort feature-request A feature should be added or improved. needs-cfn This issue is waiting on changes to CloudFormation before it can be addressed. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.