{"id":13602646,"url":"https://github.com/udondan/cdk-ec2-key-pair","last_synced_at":"2025-05-16T05:04:13.938Z","repository":{"id":37799341,"uuid":"247445650","full_name":"udondan/cdk-ec2-key-pair","owner":"udondan","description":"AWS CDK L3 construct for managing EC2 Key Pairs","archived":false,"fork":false,"pushed_at":"2025-05-16T02:57:31.000Z","size":2616,"stargazers_count":100,"open_issues_count":9,"forks_count":22,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-05-16T05:04:00.194Z","etag":null,"topics":["aws","aws-cdk","aws-cdk-construct","cdk","cloudformation","cloudformation-custom-resource","ec2","ec2-key-pair","hacktoberfest"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/udondan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-03-15T10:37:26.000Z","updated_at":"2025-05-16T02:57:35.000Z","dependencies_parsed_at":"2023-02-04T06:32:22.318Z","dependency_job_id":"9f84e741-8633-4530-a6c2-1ec832dd3579","html_url":"https://github.com/udondan/cdk-ec2-key-pair","commit_stats":{"total_commits":602,"total_committers":11,"mean_commits":54.72727272727273,"dds":0.2740863787375415,"last_synced_commit":"ef1ce22b270782e9fc298d2debe727274c124920"},"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udondan%2Fcdk-ec2-key-pair","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udondan%2Fcdk-ec2-key-pair/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udondan%2Fcdk-ec2-key-pair/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/udondan%2Fcdk-ec2-key-pair/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/udondan","download_url":"https://codeload.github.com/udondan/cdk-ec2-key-pair/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254471061,"owners_count":22076585,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-cdk","aws-cdk-construct","cdk","cloudformation","cloudformation-custom-resource","ec2","ec2-key-pair","hacktoberfest"],"created_at":"2024-08-01T18:01:32.534Z","updated_at":"2025-05-16T05:04:13.912Z","avatar_url":"https://github.com/udondan.png","language":"TypeScript","funding_links":[],"categories":["aws"],"sub_categories":[],"readme":"# CDK EC2 Key Pair\n\n[![Source](https://img.shields.io/badge/Source-GitHub-blue?logo=github)][source]\n[![Test](https://github.com/udondan/cdk-ec2-key-pair/workflows/Test/badge.svg)](https://github.com/udondan/cdk-ec2-key-pair/actions?query=workflow%3ATest)\n[![GitHub](https://img.shields.io/github/license/udondan/cdk-ec2-key-pair)][license]\n[![Docs](https://img.shields.io/badge/Construct%20Hub-cdk--ec2--key--pair-orange)][docs]\n\n[![npm package](https://img.shields.io/npm/v/cdk-ec2-key-pair?color=brightgreen)][npm]\n[![PyPI package](https://img.shields.io/pypi/v/cdk-ec2-key-pair?color=brightgreen)][PyPI]\n\n![Downloads](https://img.shields.io/badge/-DOWNLOADS:-brightgreen?color=gray)\n[![npm](https://img.shields.io/npm/dt/cdk-ec2-key-pair?label=npm\u0026color=blueviolet)][npm]\n[![PyPI](https://img.shields.io/pypi/dm/cdk-ec2-key-pair?label=pypi\u0026color=blueviolet)][PyPI]\n\n[AWS CDK] L3 construct for managing [EC2 Key Pairs].\n\nManages RSA and ED25519 Key Pairs in EC2 through a Lambda function.\n\nSupport for public key format in:\n\n- OpenSSH\n- ssh\n- PEM\n- PKCS#1\n- PKCS#8\n- RFC4253 (Base64 encoded)\n- PuTTY ppk\n\n\u003e [!NOTE]\n\u003e Please be aware, CloudFormation now natively supports creating EC2 Key Pairs via [AWS::EC2::KeyPair](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-keypair.html), so you can generally use [CDK's own KeyPair construct](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.KeyPair.html). There are a few differences, though, and this is why the custom construct remains valuable:\n\u003e\n\u003e - Instead of SSM Parameter Store, keys are stored in [AWS Secrets Manager]\n\u003e - Secrets can be **KMS encrypted** - even different KMS keys for the private and public keys. Of course, SSM parameters _can_ be encrypted too, CloudFormation just doesn't do it\n\u003e - Optionally, this construct can store and expose the public key, enabling the user to directly use it as input for other resources, e.g. for CloudFront signed urls\n\n## Installation\n\nThis package has peer dependencies, which need to be installed along in the expected version.\n\nFor TypeScript/NodeJS, add these to your `dependencies` in `package.json`. For Python, add these to your `requirements.txt`:\n\n- cdk-ec2-key-pair\n- aws-cdk-lib (^2.116.0)\n- constructs (^10.0.0)\n\n## Usage\n\n```typescript\nimport cdk = require('aws-cdk-lib');\nimport { Construct } from 'constructs';\nimport { KeyPair } from 'cdk-ec2-key-pair';\n\n// ...\n\n// Create the Key Pair\nconst key = new KeyPair(this, 'A-Key-Pair', {\n  keyPairName: 'a-key-pair',\n  description: 'This is a Key Pair',\n  storePublicKey: true, // by default the public key will not be stored in Secrets Manager\n});\n\n// Grant read access to the private key to a role or user\nkey.grantReadOnPrivateKey(someRole);\n\n// Grant read access to the public key to another role or user\nkey.grantReadOnPublicKey(anotherRole);\n\n// Use Key Pair on an EC2 instance\nnew ec2.Instance(this, 'An-Instance', {\n  keyPair: key,\n  // ...\n});\n```\n\nThe private (and optionally the public) key will be stored in AWS Secrets Manager. The secret names by default are prefixed with `ec2-ssh-key/`. The private key is suffixed with `/private`, the public key is suffixed with `/public`. So in this example they will be stored as `ec2-ssh-key/a-key-pair/private` and `ec2-ssh-key/a-key-pair/public`.\n\nTo download the private key via AWS cli you can run:\n\n```bash\naws secretsmanager get-secret-value \\\n  --secret-id ec2-ssh-key/a-key-pair/private \\\n  --query SecretString \\\n  --output text\n```\n\n### Tag support\n\nThe construct supports tagging:\n\n```typescript\ncdk.Tags.of(key).add('someTag', 'some value');\n```\n\nWe also use tags to restrict update/delete actions to those, the construct created itself. The Lambda function, which backs the custom CFN resource, is not able to manipulate other keys/secrets. The tag we use for identifying these resources is `CreatedByCfnCustomResource` with value `CFN::Resource::Custom::EC2-Key-Pair`.\n\n### Updates\n\nSince an EC2 KeyPair cannot be updated, you cannot change any property related to the KeyPair. The code has checks in place which will prevent any attempt to do so. If you try, the stack will end in a failed state. In that case you can safely continue the rollback in the AWS console and ignore the key resource.\n\nYou can, however, change properties that only relate to the secrets. These are the KMS keys used for encryption, the `secretPrefix`, `description` and `removeKeySecretsAfterDays`.\n\n### Encryption\n\nSecrets in the AWS Secrets Manager by default are encrypted with the key `alias/aws/secretsmanager`.\n\nTo use a custom KMS key you can pass it to the Key Pair:\n\n```typescript\nconst kmsKey = new kms.Key(this, 'KMS-key');\n\nconst keyPair = new KeyPair(this, 'A-Key-Pair', {\n  keyPairName: 'a-key-pair',\n  kms: kmsKey,\n});\n```\n\nThis KMS key needs to be created in the same stack. You cannot use a key imported via ARN, because the keys access policy will need to be modified.\n\nTo use different KMS keys for the private and public key, use the `kmsPrivateKey` and `kmsPublicKey` instead:\n\n```typescript\nconst kmsKeyPrivate = new kms.Key(this, 'KMS-key-private');\nconst kmsKeyPublic = new kms.Key(this, 'KMS-key-public');\n\nconst keyPair = new KeyPair(this, 'A-Key-Pair', {\n  keyPairName: 'a-key-pair',\n  kmsPrivateKey: kmsKeyPrivate,\n  kmsPublicKey: kmsKeyPublic,\n});\n```\n\n### Importing public key\n\nYou can create a key pair by importing the public key. Obviously, in this case the private key won't be available in secrets manager.\n\nThe public key has to be in OpenSSH format.\n\n```typescript\nnew KeyPair(this, 'Test-Key-Pair', {\n  keyPairName: 'imported-key-pair',\n  publicKey: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuMmbK...',\n});\n```\n\n### Using the key pair for CloudFront signed url/cookies\n\nYou can use this library for generating keys for CloudFront signed url/cookies.\n\nMake sure to set `publicKeyFormat` to `PublicKeyFormat.PEM` as that is the format required for CloudFront.\nYou also have to set `exposePublicKey` to `true` so you can actually get the public key.\n\n```typescript\nconst key = new KeyPair(this, 'Signing-Key-Pair', {\n  keyPairName: 'CFN-signing-key',\n  exposePublicKey: true,\n  storePublicKey: true,\n  publicKeyFormat: PublicKeyFormat.PEM,\n});\n\nconst pubKey = new cloudfront.PublicKey(this, 'Signing-Public-Key', {\n  encodedKey: key.publicKeyValue,\n});\nconst trustedKeyGroupForCF = new cloudfront.KeyGroup(\n  this,\n  'Signing-Key-Group',\n  {\n    items: [pubKey],\n  },\n);\n```\n\n[AWS CDK]: https://aws.amazon.com/cdk/\n[EC2 Key Pairs]: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html\n[AWS Secrets Manager]: https://aws.amazon.com/secrets-manager/\n[npm]: https://www.npmjs.com/package/cdk-ec2-key-pair\n[PyPI]: https://pypi.org/project/cdk-ec2-key-pair/\n[docs]: https://constructs.dev/packages/cdk-ec2-key-pair\n[source]: https://github.com/udondan/cdk-ec2-key-pair\n[license]: https://github.com/udondan/cdk-ec2-key-pair/blob/main/LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fudondan%2Fcdk-ec2-key-pair","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fudondan%2Fcdk-ec2-key-pair","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fudondan%2Fcdk-ec2-key-pair/lists"}