Creating AWS Key Pairs Using Terraform

Creating AWS Key Pairs Using Terraform

Introduction

AWS Key Pairs are essential for securely accessing your EC2 instances via SSH. This documentation provides a step-by-step guide to creating and managing AWS Key Pairs using Terraform.

Prerequisites

  • An AWS account with necessary permissions to create EC2 instances and key pairs.

  • Terraform installed on your local machine. You can download it from Terraform's official website.

  • Basic knowledge of Terraform and AWS.

Step-by-Step Guide

Step 1: Install Terraform

  1. Download and install Terraform based on your operating system from the official Terraform website.

  2. Verify the installation by running the following command in your terminal:

     terraform version
    

Step 2: Define the Terraform Configuration

Create a new directory for your Terraform configuration files and create a file named main.tf. In this file, define the following configuration to create a key pair:

provider "aws" {
  region = "us-east-1"  # Set your desired region
}

# Generate a new private key
resource "tls_private_key" "my_key_pair" {
  algorithm = "RSA"
  rsa_bits  = 2048
}

# Create a Key Pair
resource "aws_key_pair" "my_key" {
  key_name   = "my-key-pair"  # Set your desired key pair name
  public_key = tls_private_key.my_key_pair.public_key_openssh
}

# Output the private key (sensitive)
output "private_key" {
  value     = tls_private_key.my_key_pair.private_key_pem
  sensitive = true  # Mark the output as sensitive
}

Step 3: Initialize Terraform

Navigate to the directory containing your main.tf file and run:

terraform init

This command initializes the Terraform workspace and downloads necessary provider plugins.

Step 4: Validate the Configuration

To ensure that your configuration is correct, run:

terraform validate

Step 5: Plan Terraform Execution

Preview the changes that will be made by running:

terraform plan

Step 6: Apply the Configuration

To create the key pair and generate the private key, run:

terraform apply

When prompted, type yes to confirm. The private key will be outputted in the terminal (as sensitive data).

Step 7: Access the Private Key

Since the private key is marked as sensitive, you can access it by using the Terraform console:

  1. Start the Terraform console:

     terraform console
    
  2. Retrieve the private key:

     output.private_key
    

Managing Key Pairs in AWS Console

To view the key pairs in the AWS Management Console:

  1. Log in to AWS Management Console.

  2. Navigate to the EC2 service.

  3. In the left pane, under Network & Security, click on Key Pairs.

  4. You will see the key pair you created (e.g., my-key-pair).

Best Practices

  • Secure Storage: Store your private key securely. Do not share it or leave it exposed.

  • Use IAM Roles: Assign appropriate IAM roles to control access to your resources.

  • Rotate Keys Regularly: Periodically rotate your keys to enhance security.

Conclusion

This documentation outlines how to create and manage AWS Key Pairs using Terraform. By following these steps, you can securely access your EC2 instances. For further information, consult the AWS documentation on Key Pairs and the Terraform documentation.