AWS Amazon EC2 Key Pair

This page shows how to write Terraform for Amazon EC2 Key Pair and write them securely.

aws_key_pair (Terraform)

The Key Pair in Amazon EC2 can be configured in Terraform with the resource name aws_key_pair. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub
resource "aws_key_pair" "tokyo" {
  provider   =
  key_name   = "salawu-live-demo"
  public_key = file(var.public_key_path)
resource "aws_key_pair" "privatestorage" {
    key_name = "PrivateStorage"
    public_key = file("/home/test/.ssh/")

resource "aws_key_pair" "privatedb" {
resource "aws_key_pair" "kpAvi" {
  key_name = "kpAvi"
  public_key = file(var.public_key_path)

resource "aws_key_pair" "kpJump" {
resource "aws_key_pair" "mykey" {
  key_name   = "mykeypair"
  public_key = file("scripts/")

//resource "aws_key_pair" "mykey" {
resource "aws_key_pair" "acsKeyPair" {
    key_name = "acsKeyPair"
    public_key = file(var.ACS_PUB_KEY)

resource "aws_key_pair" "centosKeyPair" {

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).


Explanation in Terraform Registry

Provides an EC2 key pair resource. A key pair is used to control login access to EC2 instances. Currently this resource requires an existing user-supplied key pair. This key pair's public key will be registered with AWS to allow logging-in to EC2 instances. When importing an existing key pair the public key material may be in any format supported by AWS. Supported formats (per the AWS documentation) are:

  • OpenSSH public key format (the format in ~/.ssh/authorized_keys)
  • Base64 encoded DER format
  • SSH public key file format as specified in RFC4716

Tips: Best Practices for The Other AWS Amazon EC2 Resources

In addition to the aws_default_vpc, AWS Amazon EC2 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.



Ensure to avoid using default VPC

It is better to define the own VPC and use it.



Ensure your network ACL rule blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.



Ensure to use a customer-managed key for EBS volume encryption

It is better to use a customer-managed key for EBS volume encryption. It can be gain more control over the encryption by using customer-managed keys (CMK).



Ensure to avoid storing AWS access keys in user data

It is better to avoid storing AWS access keys in user data. `aws_iam_instance_profile` could be used instead.



Ensure your security group blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

Review your AWS Amazon EC2 settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

CloudFormation Example

CloudFormation code does not have the related resource.

Frequently asked questions

What is AWS Amazon EC2 Key Pair?

AWS Amazon EC2 Key Pair is a resource for Amazon EC2 of Amazon Web Service. Settings can be wrote in Terraform.

Where can I find the example code for the AWS Amazon EC2 Key Pair?

For Terraform, the kaysal/cloud-networking, hhhjjj96/Terraform-project and tacobayle/aviAws source code examples are useful. See the Terraform Example section for further details.


Automate config file reviews on your commits

Fix issues in your infrastructure as code with auto-generated patches.