/terraform-azure-linux-vm

Azure Linux Virtual Machine (VM) with Cloud-Init

Primary LanguageHCL

terraform-azure-linux-vm

Azure Linux Virtual Machine (VM) with Cloud-Init. Ubuntu 16.04-LTS. Made for the Provision Infrastructure with Cloud-Init tutorial. Azure version. This module is also on Terraform Registry.

Module usage example

  • For Azure CLI authentication login with az cli and set and export the ARM_TENANT_ID env var.

  • For service principal authentication: export Azure subscription and service principal details as environment vars:

# client_id = appId of service principal
# client_secret = service principal secret
# tenant_id = directory id
export ARM_CLIENT_ID=
export ARM_CLIENT_SECRET=
export ARM_SUBSCRIPTION_ID=
export ARM_TENANT_ID=

Optionally create a .env file and source the vars: source .env.

Login using azure cli:

az login --service-principal -u $ARM_CLIENT_ID -p $ARM_CLIENT_SECRET --tenant $ARM_TENANT_ID

Create a main.tf file, example:

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "~>3.21.1"
    }
  }
}

provider "azurerm" {
  # Configuration options
  features {}
}

module "vm" {
  source      = "github.com/Eimert/terraform-azure-linux-vm?ref=main"
  location    = "westeurope"
  rg_name     = "rg-instance"
  vm_size     = "Standard_B1s"
  cloud_init_script_location = "./scripts/add-ssh-web-app.yaml"
}

output "admin_username" {
  value = module.vm.admin_username
}

output "public_ip" {
  value = module.vm.public_ip
}

Copy the add-ssh-web-app.yaml file locally and set the cloud_init_script_location accordingly in main.tf.

For the rest; follow the Hashicorp Learn tutorial, see Add your public ssh key to your cloud init script

terraform init
terraform apply -auto-approve
# ssh using private key authentication getting the username and VM ip using terraform output
ssh $(terraform output -raw admin_username)@$(terraform output -raw public_ip)
# default admin_username: terraform
terraform destroy -auto-approve