Bootstrap FreeKB - Amazon Web Services (AWS) - Create SSH Key using Terraform
Amazon Web Services (AWS) - Create SSH Key using Terraform


The aws_key_pair resource can be used to create or update your EC2 Key Pairs, which are used to make an SSH connection onto your EC2 Instances.

Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── iam (directory)
│   ├── keys.tf
│   ├── provider.tf

 

required_providers.tf will almost always have this.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

 

Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.

provider "aws" {
  alias   = "default"
  profile = "default"
  region  = "default"
}

 

And keys.tf has the following, to create an SSH Key Pair. In this example, the SSH Key will contain the content of one of your public SSH certificates such as /home/john.doe/.ssh/id_rsa.pub.

resource "aws_key_pair" "my_ssh_key" {
  key_name   = "my-ssh-key"
  public_key = "ssh-rsa AAAAB3N...Hm92sw== webproc@TLDEPPV001.thrivent.com"
}

 

You may need to issue the terraform init command.

terraform init

 

The terraform plan command can be used to see what Terraform will try to do.

terraform plan

 

The terraform apply command can be used to create or update the SSH Key.

terraform apply -auto-approve

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter e213f9 in the box below so that we can be sure you are a human.