Skip to content

Commit

Permalink
add creation of ssh key pair
Browse files Browse the repository at this point in the history
  • Loading branch information
caroldelwing committed Feb 22, 2024
1 parent 6b436a3 commit dd7651d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
6 changes: 3 additions & 3 deletions terraform/vmware-cluster-deployment-tf/inputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ variable "tags" {

# ToDo
# Export the variable below as an environment variable named "TF_VAR_ssh_key".
# The value should include the public key for accessing the cluster nodes.
# The value should include the public key for accessing the cluster nodes. If not provided, a new key pair will be generated.
variable "ssh_key" {
type = string
description = "Specify the public key that will be used to access the cluster nodes. If not provided, a new key pair will be generated. Press enter to generate a new key pair."
sensitive = true
default = ""
description = "Export the public key that will be used to access the cluster nodes as an environment variable named TF_VAR_ssh_key. If not provided, a new key pair will be generated."
}

# ToDo
Expand Down
19 changes: 16 additions & 3 deletions terraform/vmware-cluster-deployment-tf/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@ output "Advisory" {
We recommend waiting a few minutes before clicking on the service URL to prevent the browser from caching an unresolved DNS request.
EOT
EOT
}

output "profile_id" {
value = spectrocloud_cluster_profile.profile.id
output "ssh_key_location" {
description = "Location of the private SSH key file"
value = length(tls_private_key.tutorial_ssh_key) > 0 ? "This is the location of your private SSH key file: ${local_sensitive_file.private_key_file[0].filename}." : null
}

output "ssh_public_key_location" {
description = "Location of the public SSH key file"
value = length(tls_private_key.tutorial_ssh_key) > 0 ? "This is the location of your public SSH key file: ${local_file.public_key_file[0].filename}." : null
}

output "ssh_connection_command" {
description = "Command to use the SSH key to connect to a VM"
value = length(tls_private_key.tutorial_ssh_key) > 0 ? "To access your nodes, use the following command, replacing <username> with your username and <hostname> with your hostname: ssh -i ${local_sensitive_file.private_key_file[0].filename} <username>@<hostname>" : null
}


5 changes: 5 additions & 0 deletions terraform/vmware-cluster-deployment-tf/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ terraform {
source = "hashicorp/tls"
version = "4.0.4"
}

local = {
source = "hashicorp/local"
version = "2.4.1"
}
}
}

Expand Down
23 changes: 22 additions & 1 deletion terraform/vmware-cluster-deployment-tf/ssh-key.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,25 @@ resource "tls_private_key" "tutorial_ssh_key" {

locals {
ssh_public_key = var.ssh_key != "" ? var.ssh_key : tls_private_key.tutorial_ssh_key[0].public_key_openssh
}
}

resource "local_sensitive_file" "private_key_file" {
count = length(tls_private_key.tutorial_ssh_key) > 0 ? 1 : 0
content = tls_private_key.tutorial_ssh_key[0].private_key_openssh
filename = "${path.module}/tutorial_ssh_key"
}

resource "local_file" "public_key_file" {
count = length(tls_private_key.tutorial_ssh_key) > 0 ? 1 : 0
content = tls_private_key.tutorial_ssh_key[0].public_key_openssh
filename = "${path.module}/tutorial_ssh_key.pub"
}

resource "terraform_data" "chmod" {
count = length(tls_private_key.tutorial_ssh_key) > 0 ? 1 : 0
provisioner "local-exec" {
command = "chmod 600 ${local_sensitive_file.private_key_file[0].filename}"
}
}


0 comments on commit dd7651d

Please sign in to comment.