Skip to content

Instantly share code, notes, and snippets.

@jhanley-com
Last active September 20, 2021 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhanley-com/dd6623172adf3031b192655a8bf78e36 to your computer and use it in GitHub Desktop.
Save jhanley-com/dd6623172adf3031b192655a8bf78e36 to your computer and use it in GitHub Desktop.
Terraform files for my article, Azure - Lock a VM to Prevent Deletion: https://www.jhanley.com/azure-lock-a-vm-to-prevent-deletion/
##############################################################################
# Date Created: 2021-09-17
# Last Update: 2021-09-17
# https://www.jhanley.com - Google Cloud
# Copyright (c) 2021, John J. Hanley
# Author: John J. Hanley
# License: MIT
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
# Begin Providers section
# Configure the Azure provider
# Version numbers are the latest on 2021-09-19
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.77"
}
}
required_version = ">= 1.0.7"
}
provider "azurerm" {
features {}
}
# Begin Resource section
# Declare the Virtual Machine Resource
data "azurerm_virtual_machine" "vm" {
name = var.virtual-machine-name
resource_group_name = var.resource-group-name
}
# Declare the Lock Resource
resource "azurerm_management_lock" "vm" {
name = var.lock-name
lock_level = var.lock-level
notes = var.lock-notes
scope = data.azurerm_virtual_machine.vm.id
}
# Begin Outputs section
output "virtual-machine-id" {
value = data.azurerm_virtual_machine.vm.id
}
output "vm-lock-id" {
value = azurerm_management_lock.vm.id
}
resource-group-name = "development"
virtual-machine-name = "my-test-machine"
variable "resource-group-name" {
description = "Azure Resource Group Name"
type = string
default = ""
}
variable "virtual-machine-name" {
description = "Azure Virtual Machine Name"
type = string
default = ""
}
variable "lock-name" {
description = "Azure Virtual Machine Name"
type = string
default = "Prevent-VM-Delete-Lock"
}
variable "lock-level" {
description = "Azure Lock Level"
type = string
default = "CanNotDelete"
}
variable "lock-notes" {
description = "Azure Virtual Machine Name"
type = string
default = "Lock created by Terraform"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment