Create AWS key-pair using terraform key-pair module

Anil MitkariAnil Mitkari
3 min read

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

  1. Create a directory named "module-key-pair-project" for the project.

  2. Inside "module-key-pair-project," create two more directories:

    • "aws-key-pair"

    • "module"

Step 2: Module Directory

  1. Navigate to the "module" directory.

  2. Create a subdirectory named "key-pair" within the "module" directory.

Step 3: Key Pair Module

  1. Inside the "key-pair" directory, create two files:

Step 4: Configure main.tf

  1. Add the following code to the main.tf file 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

  1. Add the necessary variables and their descriptions to the variables.tf file.
variable "key_pairs" {
  type = list(object({
    key_name  = string
    rsa_bits  = number
  }))
}

Step 6: AWS Key Pair Directory

  1. Navigate to the "aws-key-pair" directory.

  2. Create three files:

Step 7: Configure main.tf for AWS Key Pair

  1. Add the following code to the main.tf file 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

  1. Add the necessary variables and their descriptions to the variables.tf file 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

  1. Add your specific variable values to the terraform.tfvars file 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

  1. Open a command prompt or terminal and navigate to the "aws-key-pair" directory.

  2. 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

  1. 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.


0
Subscribe to my newsletter

Read articles from Anil Mitkari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anil Mitkari
Anil Mitkari