Create AWS key-pair using terraform key-pair module
In this guide, we'll walk through the steps to create an AWS key pair using Terraform. This allows you to manage your AWS infrastructure as code.
Step 1: Project Setup
Create a directory named "module-key-pair-project" for the project.
Inside "module-key-pair-project," create two more directories:
"aws-key-pair"
"module"
Step 2: Module Directory
Navigate to the "module" directory.
Create a subdirectory named "key-pair" within the "module" directory.
Step 3: Key Pair Module
Inside the "key-pair" directory, create two files:
Step 4: Configure main.tf
- Add the following code to the
main.tffile in the "key-pair" directory:
resource "aws_key_pair" "key_pairs" {
count = length(var.key_pairs)
key_name = var.key_pairs[count.index].key_name
public_key = tls_private_key.key_pairs[count.index].public_key_openssh
}
resource "tls_private_key" "key_pairs" {
count = length(var.key_pairs)
algorithm = "RSA"
rsa_bits = var.key_pairs[count.index].rsa_bits
}
resource "local_file" "private_key" {
count = length(var.key_pairs)
content = tls_private_key.key_pairs[count.index].private_key_pem
filename = "${var.key_pairs[count.index].key_name}.pem"
}
resource "local_file" "putty_key" {
count = length(var.key_pairs)
content = tls_private_key.key_pairs[count.index].private_key_pem
filename = "${var.key_pairs[count.index].key_name}.ppk"
provisioner "local-exec" {
command = "puttygen ${var.key_pairs[count.index].key_name}.pem -o ${var.key_pairs[count.index].key_name}.ppk"
}
provisioner "local-exec" {
command = "cp -r ${var.key_pairs[count.index].key_name}.pem /home/anil/aws/"
}
}
Step 5: Configure variables.tf
- Add the necessary variables and their descriptions to the
variables.tffile.
variable "key_pairs" {
type = list(object({
key_name = string
rsa_bits = number
}))
}
Step 6: AWS Key Pair Directory
Navigate to the "aws-key-pair" directory.
Create three files:
terraform.tfvars
Step 7: Configure main.tf for AWS Key Pair
- Add the following code to the
main.tffile in the "aws-key-pair" directory:
# configure aws provider
provider "aws" {
region = var.region
}
module "aws_key_pair" {
source = "../modules/key-pair"
key_pairs = var.key_pairs
}
Step 8: Configure variables.tf for AWS Key Pair
- Add the necessary variables and their descriptions to the
variables.tffile in the "aws-key-pair" directory.
variable "region" {}
######################################################3
variable "key_pairs" {
type = list(object({
key_name = string
rsa_bits = number
}))
}
Step 9: Configure terraform.tfvars for AWS Key Pair
- Add your specific variable values to the
terraform.tfvarsfile in the "aws-key-pair" directory.
region="ap-south-1"
key_pairs = [
{
key_name = "aws_key_pair01"
rsa_bits = 4096
},
{
key_name = "aws_key_pair02"
rsa_bits = 4096
},
{
key_name = "aws_key_pair03"
rsa_bits = 4096
}
]
Step 10: Terraform Commands
Open a command prompt or terminal and navigate to the "aws-key-pair" directory.
Run the following commands:
terraform init: Initialize Terraform.terraform validate: Validate your Terraform configuration.terraform plan: Preview the changes Terraform will make.terraform apply --auto-approve: Apply the Terraform configuration, creating the AWS key pair.
Step 11: Conclusion
- After executing these commands, you will receive output confirming the successful creation of the AWS key pair using Terraform.

Congratulations! You've successfully created an AWS key pair using Terraform's modular approach.