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

feat!: implements IKeyPair interface #279

Merged
merged 8 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

[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:
> ⚠️ 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
Expand Down
13 changes: 12 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
}
}
23 changes: 22 additions & 1 deletion test/lib/test-stack.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down