Last active
September 20, 2021 21:12
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################## | |
# 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource-group-name = "development" | |
virtual-machine-name = "my-test-machine" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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