Azure Compute SSH Public Key

This page shows how to write Terraform and Azure Resource Manager for Compute SSH Public Key and write them securely.

azurerm_ssh_public_key (Terraform)

The SSH Public Key in Compute can be configured in Terraform with the resource name azurerm_ssh_public_key. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

main.tf#L20
resource "azurerm_ssh_public_key" "ssh_key" {
  name                = "terraform_ho02_ssh_key"
  resource_group_name = "terraform_ho02"
  location            = "westeurope"
  public_key          = file("id_rsa.pub")
}
main.tf#L6
resource "azurerm_ssh_public_key" "ssh" {
  name                = var.name
  resource_group_name = var.resource_group
  location            = var.location
  public_key          = var.key
  tags = {
ssh_keys.tf#L5
resource "azurerm_ssh_public_key" "ssh_key_HaProxy" {
  name                = "ssh_key_HaProxy"
  resource_group_name = azurerm_resource_group.Ressource_Paris_VM.name
  location            = var.azure_location_Paris
  public_key          = "YOUR_OWN_SSH-KEY"
}
main.tf#L29
resource "azurerm_ssh_public_key" "RedTeam_SSH_jump" {
  name                = "RedTeam-SSH-jump"
  resource_group_name = azurerm_resource_group.RedTeam_RG.name
  location            = azurerm_resource_group.RedTeam_RG.location
  public_key          = file("C:/users/aRustyDev.ARUSTY/.ssh/id_rsa.pub")
}
main.tf#L1
resource "azurerm_ssh_public_key" "example" {
  name                = "example"
  resource_group_name = "example"
  location            = "West Europe"
  public_key          = file("~/.ssh/id_rsa.pub")
main.tf#L1
resource "azurerm_ssh_public_key" "example" {
  name                = var.name
  resource_group_name = var.resource_group
  location            = var.location
  public_key          = file(var.public_key)
main.tf#L7
resource "azurerm_ssh_public_key" "example"{
  name = "AZKey"
  resource_group_name = azurerm_resource_group.rg.name
  location = azurerm_resource_group.rg.location
  public_key = file("key_pub/AZKey.txt")
}
main.tf#L6
resource "azurerm_ssh_public_key" "ssh" {
  name                = var.name
  resource_group_name = var.resource_group
  location            = var.location
  public_key          = var.key
  tags                = var.tags
security.tf#L2
resource "azurerm_ssh_public_key" "mysshkey" {
  location              = var.location
  name                  = var.name
  public_key            = var.SSH_KEY
  resource_group_name   = var.rg_name
main.tf#L32
resource "azurerm_ssh_public_key" "pub_key" {
  name                = "Key_for_vm"
  resource_group_name = azurerm_resource_group.arg.name
  location            = azurerm_resource_group.arg.location
  public_key          = file("~/.ssh/id_rsa.pub")
}

Review your Terraform file for Azure best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Manages a SSH Public Key.

Tips: Best Practices for The Other Azure Compute Resources

In addition to the azurerm_linux_virtual_machine, Azure Compute has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

azurerm_linux_virtual_machine

Ensure to use SSH authentication for virtual machines

It is better to use SSH authentication for virtual machines instead of password authentication to enforce more secure ways.

risk-label

azurerm_managed_disk

Ensure to enable the encryption on managed disks

It is better to enable the encryption on managed disks.

risk-label

azurerm_virtual_machine

Ensure to use SSH authentication for virtual machines

It is better to use SSH authentication for virtual machines instead of password authentication to enforce more secure ways.

Review your Azure Compute settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

Microsoft.Compute/SshPublicKeys (Azure Resource Manager)

The SshPublicKeys in Microsoft.Compute can be configured in Azure Resource Manager with the resource name Microsoft.Compute/SshPublicKeys. The following sections describe how to use the resource and its parameters.

Example Usage from GitHub

sshkey.json
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "sshPublicKeys_name": {
upload_sshkey.json
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "sshKeyName": {
ssh.json
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
ssh.json
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
azuredeploy.json
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
azuredeploy.json
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
template.json
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "virtualMachines_tomerykserver1_name": {
azuredeploy.json
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "sshName": {
template.json
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "sshPublicKeys_vm_name": {
vm-and-docker-instances-template.json
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "registries_proj2_name": {

Frequently asked questions

What is Azure Compute SSH Public Key?

Azure Compute SSH Public Key is a resource for Compute of Microsoft Azure. Settings can be wrote in Terraform.

Where can I find the example code for the Azure Compute SSH Public Key?

For Terraform, the emberiris/terraform-workshop, ahelal/cdf and etienne-plagnieux/Logskills_Terraform_Azure source code examples are useful. See the Terraform Example section for further details.

For Azure Resource Manager, the aryamo/azurejson, dohughes-msft/sap and ahelal/cdf source code examples are useful. See the Azure Resource Manager Example section for further details.