The below code will generate a new key pair with a random name in the region defined by the variables.
MAIN.tf
provider "aws" {
region = "${var.TOPregion}"
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
}
resource "random_string" "wp_id" {
length = 10
special = false
}
resource "aws_key_pair" "Lightsail" {
key_name = random_string.wp_id.result
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAtAaMGlO0yMvkAKLv1F6WgDb/qDMSj4A8ao+8uGFyq/6xq/sG270mnBPYTAgDuDiB9CNL0DH/OQVKxfsxE3Opgqshnq/DO6EvcKwPv9FVjckYhV2CoFs1oXcChvMj5h0O+a7yjgA2ugKiss1DBCZSmSJ3Uzc6BPo3xVd/vtLs8L425RDTpg0EDBleEDBraFYz3enGC5eL6Z6anadB6Iuak9jZt44IGHHJnFwnT3Q== rsa-key-20200206"
}
OUTPUTS.tf
output "this_key_pair_name" {
value = aws_key_pair.Lightsail.key_name
}
output "this_key_pair_fingerprint" {
value = aws_key_pair.Lightsail.fingerprint
}
VARIABLES.tf
variable "TOPregion" {
default = "eu-west-2"
}
### If left blank {} use "terraform plan" to be prompted
# n.b. these are the key details for the terraform access, not the key
# name being generated.
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "aws_key_name" {}