How To Create An SSH key In Terraform ?
Last Updated :
02 Apr, 2024
AWS SSH keys are private secret keys used for various access related things in AWS. These can be used for resources such as EC2 instances, IAM accounts. etc. Terraform can be used for easy and direct creation of SSH key in AWS. Let's see how we can create SSH Key using Terraform.
Primary Components Of SSH Key with Terraform
- Terraform: It is Infrastructure as an infrastructure-as-a-service tool that allows the deployment of resources to multiple cloud providers through code.
- AWS SSH Key: It is a set of public and private key used for accessing a resource in AWS.
- IaaC: Infrastructure as a Code allows to representation of cloud infrastructure in the form of code.
Steps To Create SSH Key Using Terraform
Step 1: Set Up Terraform
- Download the Terraform zip from the installation page of the Terraform website.
- Extract and paste the terraform folder to the required location and add the path to runnable in environment variables.
Step 2: Set Up AWS CLI
- Download the AWS CLI setup from official website.
- Run the installer and follow the steps to install.
Step 3: Configure AWS CLI
- Copy or create an AWS access key from the AWS console it is required in the next step.
- Open the terminal or cmd and run the below command to configure the credentials of AWS as it will be required for Terraform.
aws configure
- Provide access keys copied in the previous step and also give default region if different.
.png)
Step 4: Create Terraform Code
- Goto your project folder and create main.tf file.
- Add terraform block to code with aws as required provider with latest version. You can find the latest version at hashicorp registry.
.png)
- Terraform block should look like below. You can add required version to avoid invalidation.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.37.0"
}
}
required_version = ">= 1.2.0"
}
- Now add provider as aws like below. Specify other details as required.
provider "aws" {
region = "us-east-1"
}
- Add configuration for SSH Key. For this article we will create AWS IAM user SSH key. Similar procedure can be followed for other keys.
resource "aws_iam_user_ssh_key" "user1_ssh" {
username = <YOUR USERNAME>
encoding = "SSH"
public_key = <YOUR SSH PUBLIC KEY>
}
- We have specified username for IAM user. We have added encoding as SSH.
- add your own PUBLIC SSH KEY at the place.
- The complete code will look like below.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.37.0"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "us-east-1"
}
resource "aws_iam_user_ssh_key" "user1_ssh" {
username = <YOUR USERNAME>
encoding = "SSH"
public_key = <YOUR PUBLIC SSH KEY>
}
Step 5: Apply The Terraform Code
- Once the code is ready you can apply it.
- First init the terraform by running below command in project folder where main.tf is present.
terraform init
.png)
- After successful output apply the changes using below command.
terrraform apply
- After verifying type "yes" to confirm and apply.
.png)
- Terraform will start creating ssh keys.
.png)
- You can also verify creation of ssh key on users overview page under ssh keys.
.png)
Conclusion
We have successfully ssh key for iam user with the help of terraform in this article. The configuration described can be further modified to make changes to create other keys in aws. This is how terraform allows reusable and modifiable configuration of infrastructure.
Q. Why do I need an SSH key pair in AWS?
An SSH key pair is required to securely access and authenticate with AWS EC2 instances. The public key is added to the EC2 instance, while the private key is used for authentication when connecting to the instance.
Q. How can I create an SSH key pair in AWS using Terraform?
You can use Terraform's aws_key_pair
resource to create an SSH key pair in AWS. This resource generates the key pair and allows you to specify the key name.
Q. What are the required parameters for creating an SSH key pair in Terraform?
When creating an SSH key pair with Terraform, you typically need to specify the key_name
parameter, which is the name of the key pair. Optionally, you can also specify the public_key
parameter if you want to use a specific public key.
Q. Can I delete an SSH key pair created with Terraform?
Yes, you can delete an SSH key pair using Terraform by removing the corresponding aws_key_pair
resource from your Terraform configuration and applying the changes.
Q. Can I automate the creation of SSH key pairs for multiple environments with Terraform?
Yes, Terraform allows you to automate the creation of SSH key pairs for multiple environments by defining the necessary resources in your Terraform configuration files and using variables to customize the key names.
Similar Reads
How To Create SQS In AWS Using Terraform ?
Amazon Simple Queue Service (SQS) is a completely managed message queuing service provided by AWS, offering scalable and reliable message conveyance between distributed parts of an application. It fills in as a fundamental building block for building event driven, microservice-based models, empoweri
8 min read
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 utilize
6 min read
How To Create AWS IAM Roles Using Terraform?
Terraform is an IAAC tool which is used provision infrastructure . Here in this guide i will first discuss what is terraform . Then i will discuss what is IAM Role and in which scenarios we should use IAM Role . Then i will walk you through the different steps to create an IAM Role for an EC2 servic
5 min read
How to Create S3 Storage Life using Terraform?
S3 stands for Amazon Simple Storage Service, foundation in cloud storage arrangements, offering unrivaled versatility, security, and durability. It fills in as a urgent part inside Amazon Web Services (AWS), taking care of the different storage needs of designers and endeavors around the world. At i
7 min read
How to Create Vnet in Azure using Terraform ?
Azure Vnet also called Azure Virtual Network is a network that provides various network-related services in Azure. It connects groups of resources and isolates them from outside access in azure cloud. In this article let's see how we can set up Azure Virtual Network using Terraform. Understanding Of
4 min read
How To Create ECR Repository In AWS Using Terraform ?
In a cloud computing and microservice world, we must concentrate on the involvement of the infrastructure resources properly. With Terraform, an open source IaC (infrastructure as code) tool that is widely used for provisioning and managing cloud resources across many cloud service providers such as
9 min read
How To Create Elastic IP In AWS Using Terraform ?
Using Terraform, creating an Elastic IP (EIP) on AWS entails specifying the resources required in a Terraform configuration file in order to create the EIP. A static IPv4 address intended for dynamic cloud computing is called an elastic IP. It is helpful in situations when you require a public IP ad
4 min read
How to Create App Service in Azure using Terraform
Azure App Service is a service that provides a managed platform for deploying applications in the Azure cloud. It supports multiple language applications. App service allows building, deploying and scaling of applications in Azure. Setting up an app service is a complicated process. let's see how we
5 min read
How to Create Load Balancer in GCP using Terraform ?
As traffic controls, load balancers on the Google Cloud Platform (GCP) distribute incoming requests among multiple instances of your application. Consider them as traffic engineers that direct customers to the busiest locations to deliver a dependable and responsive service, especially during period
5 min read
How To Create AWS VPC Using Terraform ?
Terraform is an IAAC tool used to automate programmatic infrastructure provisioning. Here in this guide, I will first discuss what is AWS VPC. Then I will discuss terraform. After this, I will walk you through the different steps to write a code using Terraform to create a custom AWS VPC using subne
6 min read