Skip to main content

CDK Construct for managing EC2 key pairs

Project description

CDK EC2 Key Pair

Source Test GitHub Docs

npm package PyPI package NuGet package

Downloads npm PyPI NuGet

AWS CDK L3 construct for managing EC2 Key Pairs.

CloudFormation doesn't directly support creation of EC2 Key Pairs. This construct provides an easy interface for creating Key Pairs through a custom CloudFormation resource. The private key is stored in AWS Secrets Manager.


# Example automatically generated without compilation. See
import aws_cdk.core as cdk
import aws_cdk.aws_ec2 as ec2
from cdk_ec2_key_pair import KeyPair

# Create the Key Pair
key = KeyPair(self, "A-Key-Pair",
    description="This is a Key Pair"

# Grant read access to the private key to a role or user

# Use Key Pair on an EC2 instance
ec2.Instance(self, "An-Instance", {

The private key will be stored in AWS Secrets Manager. The secret name by default is prefixed with ec2-private-key/, so in this example it will be saved as ec2-private-key/a-key-pair.

To download the private key via AWS cli you can run:

aws secretsmanager get-secret-value \
  --secret-id ec2-private-key/a-key-pair \
  --query SecretString \
  --output text


  • Name should be optional

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for cdk-ec2-key-pair, version 1.5.0
Filename, size File type Python version Upload date Hashes
Filename, size cdk_ec2_key_pair-1.5.0-py3-none-any.whl (51.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size cdk-ec2-key-pair-1.5.0.tar.gz (52.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page