Manage SSH keys

See also: Juju | SSH key

Add an SSH key

To add a public ssh key to a model, in your Terraform plan create a resource of the juju_ssh_key type, specifying the name of the model and the payload (here, the SSH key itself). For example:

resource "juju_ssh_key" "mykey" {
  model_uuid = juju_model.development.uuid
  payload    = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC1I8QDP79MaHEIAlfh933zqcE8LyUt9doytF3YySBUDWippk8MAaKAJJtNb+Qsi+Kx/RsSY02VxMy9xRTp9d/Vr+U5BctKqhqf3ZkJdTIcy+z4hYpFS8A4bECJFHOnKIekIHD9glHkqzS5Vm6E4g/KMNkKylHKlDXOafhNZAiJ1ynxaZIuedrceFJNC47HnocQEtusPKpR09HGXXYhKMEubgF5tsTO4ks6pplMPvbdjxYcVOg4Wv0N/LJ4ffAucG9edMcKOTnKqZycqqZPE6KsTpSZMJi2Kl3mBrJE7JbR1YMlNwG6NlUIdIqVoTLZgLsTEkHqWi6OExykbVTqFuoWJJY3BmRAcP9H3FdLYbqcajfWshwvPM2AmYb8V3zBvzEKL1rpvG26fd3kGhk3Vu07qAUhHLMi3P0McEky4cLiEWgI7UyHFLI2yMRZgz23UUtxhRSkvCJagRlVG/s4yoylzBQJir8G3qmb36WjBXxpqAGHfLxw05EQI1JGV3ReYOs= user@somewhere"
}
Example: Add an SSH key from GitHub
terraform {
  required_providers {
    juju = {
      source  = "juju/juju"
    }
    github = {
       source = "integrations/github"
      version = "6.6.0"
    }
  }
}

provider "github" {
  owner = "<name>"
}

provider "juju" {}

resource "juju_model" "test" {
  name = "test-target"
}

data "github_ssh_keys" "name" {}

resource "juju_ssh_key" "name" {
  for_each = toset(data.github_ssh_keys.name.keys)
  model    = juju_model.test.name
  payload = each.key
}
Example: Add an SSH key from Launchpad
terraform {
  required_providers {
     juju = {
      source  = "juju/juju"
    }
    http = {
      source = "hashicorp/http"
      version = "3.5.0"
    }
  }
}

provider "http" {
  # Configuration options
}

provider "juju" {
}

resource "juju_model" "test" {
  name = "test-target"
}

data "http" "launchpad_keys" {
  url = "https://launchpad.net/~<username>/+sshkeys"
}

# Split the keys into a list by line
locals {
  ssh_keys = [for k in split("\n", trimspace(data.http.launchpad_keys.response_body)) : k if length(trimspace(k)) > 0]
}

# Create the resource for each key
resource "juju_ssh_key" "name" {
  for_each = toset(local.ssh_keys)
  model    = juju_model.test.name
  payload  = each.key
}

Remove an SSH key

To remove an SSH key, remove its resource definition from your Terraform plan.