diff --git a/README.md b/README.md index 4c678249..4bd82ff0 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,8 @@ [AWS CDK] L3 construct for managing [EC2 Key Pairs]. -> ⚠️ 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 the reason why this custom construct is still in existence: +> [!NOTE] +> 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: > > - Instead of SSM Parameter Store, keys are stored in [AWS Secrets Manager] > - 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 @@ -27,14 +28,9 @@ This package has peer dependencies, which need to be installed along in the expe For TypeScript/NodeJS, add these to your `dependencies` in `package.json`. For Python, add these to your `requirements.txt`: - cdk-ec2-key-pair -- aws-cdk-lib (^2.0.0) +- aws-cdk-lib (^2.116.0) - constructs (^10.0.0) -## CDK compatibility - -- Version 3.x is compatible with the CDK v2. -- Version 2.x is compatible with the CDK v1. There won't be updates for this. - ## Usage ```typescript @@ -161,11 +157,11 @@ const trustedKeyGroupForCF = new cloudfront.KeyGroup( ); ``` - [AWS CDK]: https://aws.amazon.com/cdk/ - [EC2 Key Pairs]: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html - [AWS Secrets Manager]: https://aws.amazon.com/secrets-manager/ - [npm]: https://www.npmjs.com/package/cdk-ec2-key-pair - [PyPI]: https://pypi.org/project/cdk-ec2-key-pair/ - [docs]: https://constructs.dev/packages/cdk-ec2-key-pair - [source]: https://github.com/udondan/cdk-ec2-key-pair - [license]: https://github.com/udondan/cdk-ec2-key-pair/blob/main/LICENSE +[AWS CDK]: https://aws.amazon.com/cdk/ +[EC2 Key Pairs]: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html +[AWS Secrets Manager]: https://aws.amazon.com/secrets-manager/ +[npm]: https://www.npmjs.com/package/cdk-ec2-key-pair +[PyPI]: https://pypi.org/project/cdk-ec2-key-pair/ +[docs]: https://constructs.dev/packages/cdk-ec2-key-pair +[source]: https://github.com/udondan/cdk-ec2-key-pair +[license]: https://github.com/udondan/cdk-ec2-key-pair/blob/main/LICENSE diff --git a/lib/index.ts b/lib/index.ts index a0f28407..b32537a9 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -7,11 +7,13 @@ import { Duration, ITaggable, Lazy, + Resource, ResourceProps, Stack, TagManager, TagType, } from 'aws-cdk-lib'; +import { IKeyPair, OperatingSystemType } from 'aws-cdk-lib/aws-ec2'; import { Construct } from 'constructs'; import * as path from 'path'; import { PublicKeyFormat, ResourceProperties } from './types'; @@ -143,7 +145,7 @@ export interface KeyPairProps extends ResourceProps { /** * An EC2 Key Pair */ -export class KeyPair extends Construct implements ITaggable { +export class KeyPair extends Resource implements ITaggable, IKeyPair { /** * The lambda function that is created */ @@ -410,4 +412,13 @@ export class KeyPair extends Construct implements ITaggable { }); return result; } + + /** + * Used internally to determine whether the key pair is compatible with an OS type. + * + * @internal + */ + public _isOsCompatible(_osType: OperatingSystemType): boolean { + return true; // as we currently only support OpenSSH, we are compatible with all OS types + } } diff --git a/package.json b/package.json index 8c0e3b71..fa85cbbd 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "typescript": "5.4.3" }, "peerDependencies": { - "aws-cdk-lib": "^2.0.0", + "aws-cdk-lib": "^2.116.0", "constructs": "^10.0.0" } } diff --git a/test/lib/test-stack.ts b/test/lib/test-stack.ts index f092eed0..dc0e992a 100644 --- a/test/lib/test-stack.ts +++ b/test/lib/test-stack.ts @@ -1,4 +1,11 @@ -import { Tags, StackProps, Stack, CfnOutput, aws_iam } from 'aws-cdk-lib'; +import { + Tags, + StackProps, + Stack, + CfnOutput, + aws_iam, + aws_ec2, +} from 'aws-cdk-lib'; import cloudfront = require('aws-cdk-lib/aws-cloudfront'); import { Construct } from 'constructs'; import { PublicKeyFormat } from '../../lambda/types'; @@ -41,6 +48,20 @@ export class TestStack extends Stack { publicKey: keyPair.publicKeyValue, }); + if (process.env.with_ec2 === 'true') { + new aws_ec2.Instance(this, 'Test-Instance', { + vpc: aws_ec2.Vpc.fromLookup(this, 'VPC', { + vpcName: 'default', + }), + instanceType: aws_ec2.InstanceType.of( + aws_ec2.InstanceClass.T2, + aws_ec2.InstanceSize.MICRO, + ), + machineImage: aws_ec2.MachineImage.latestAmazonLinux2(), + keyPair: keyPairImport, + }); + } + new CfnOutput(this, 'Test-Public-Key-Import', { exportName: 'TestPublicKeyImport', value: keyPairImport.publicKeyValue,