Open In App

How To Create Key Pair In AWS Using Terraform ?

In cloud infrastructure management, secure admittance to instances is central. While working with Amazon Web Services (AWS) utilizing Terraform, creating key pairs is fundamental for secure access to EC2 instances. Key pairs comprise of a public key and a private key, where the public key is utilized to encrypt data, and the private key is utilized to decrypt it.

This guide gives a step-by-step instructional exercise on the most proficient method to create key pairs in AWS utilizing Terraform. Terraform, infrastructure as a code tool, considers the declarative configuration of resources, making it ideal for managing cloud infrastructure in a reliable, scalable, and repeatable manner.



Primary Terminologies:

What is meant by Key Pair?

A key pair is a set of cryptographic keys utilized for secure communication between systems. It normally comprises two parts: a public key and a private key.

Key pairs are normally utilized in different security protocols, like SSH (Secure Shell), SSL/TLS (Secure Socket Layer/Transport Layer Security), and digital signatures. With regards to AWS, key pairs are utilized for safely getting to EC2 instances, where the public key is utilized to encrypt login credentials, and the private key is utilized to decrypt them.



Step-by-step process to create key pair in AWS using Terraform

Step 1: Launch an Instance

Launch EC2 instance with Amazon Linux2 Kernel 5.10(AMI) along with port numbers set SSH – 22, HTTP 8o, select storage t2.micro and select existing security group.

Now connect with git bash or any terminal like Putty, PowerShell, visual studio code, and so on.

Step 2: Install Terraform

Now install terraform to our local machine by using following commands or go to official page of terraform and copy commands

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform

Step 3: Create a file to create key pair

Create a file with ” .tf “ extension like <filename.tf>. Inside this file we are configuring our infrastructure with script

Provider section

In this provider section we are defining which region to mention

# Specify provider
provider "aws" {
region = "eu-west-1" # Change this to your desired region
}

Keypair Resource

In this Keypair resource section we defining keypair name and key pair path to store our key

# Define key pair resource
resource "aws_key_pair" "my_key_pair" {
key_name = "my-key-pair" # Name for your key pair
public_key = file("~/.ssh/id_rsa.pub") # Path to your public key file
}

Step 4: Generate Keys

In this step we are generating keys generation because our terraform infrastructure keypair need a path to store key ID

Default default path of public key is /home/ec2-user/.ssh/id_rsa.pub

Generate key_generation by using following command

ssh-keygen

with out this step we cannot create a key pair while executing terraform validate we face a error. Like shown in below figure

Step 5: Execute Terraform Commands

Now initialize terraform by using following command. When we execute this it automatically install some packages required to terraform

terraform init

Now execute terraform execution flow commands when we execute this commands terraform build a our infrastructure

terraform fmt
terraform validate
terraform plan

Now execute terraform apply command

terraform apply --auto-approve

Resources created after successfully terraform apply completed

Conclusion

By understanding how to create key pairs in AWS using Terraform is crucial for keeping up with secure access to cloud resources. By following the steps framed in this guide, users can successfully oversee cryptographic keys and ensure secure correspondence between systems inside their AWS infrastructure.

Key Pairs assume an imperative part in securing down access to EC2 instances, and Terraform gives a convenient method for automate the creation and the management of these key pairs. By utilizing Terraforms infrastructure as code capacities, users can define key pair resources in a declarative way, ensuring consistency and unwavering quality across their infrastructure deployments.

Besides, by integrating key pair creation into their Terraform work processes, clients can smooth out their infrastructure management processes and authorize security best practices reliably across their AWS environments.

Create Key Pair In AWS Using Terraform – FAQ’s

Could I at any point reuse a current key pair in AWS with Terraform?

Yes, you can reference a current key pair in your Terraform setup by determining its name.

Is it possible to rotate key pairs consequently utilizing Terraform?

No, Terraform doesn’t locally support automatic key pair turn. Key pair turn commonly requires manual mediation or custom scripting.

Could I at any point determine a custom key pair name while making it with Terraform?

Yes, you can determine the ideal name for the key pair in your Terraform configuration.

How would I manage key pair security in Terraform?

It’s fundamental to follow security best practices, for example, putting away private keys safely and limiting access to them. Try not to uncover private keys in variant controlled Terraform setups.

What occurs in the event that I lose the private key related with an AWS key pair?

Assuming the private key is lost, you might lose access to instances related with that key pair. It’s crucial to securely backup private keys to prevent this situation.


Article Tags :